Start X at login: Difference between revisions

From ArchWiki
(→‎bash_profile: the second method is useful because it can work at any console)
(→‎inittab: this method is broken in several ways, it doesn't belong outside of a user page and only works with initscripts anyway)
Line 57: Line 57:


{{Note|This method can be combined with [[automatic login to virtual console]] and act similar to the inittab method, but it will properly register your session and works with [[ConsoleKit]].}}
{{Note|This method can be combined with [[automatic login to virtual console]] and act similar to the inittab method, but it will properly register your session and works with [[ConsoleKit]].}}
==inittab==
Another way of circumventing display managers and booting straight into a preferred window manager or desktop environment involves editing {{ic|/etc/inittab}}.
{{Note|This method will not use {{ic|/bin/login}} or register your session, therefore no session will appear in {{ic|who}} or {{ic|w}}. Your session will also not be authorized as 'local' by [[ConsoleKit]], so you will be unable to shutdown/suspend/reboot or mount drives without using {{ic|sudo}} or {{ic|su}}.}}
Change:
id:3:initdefault:
[...]
x:5:respawn:/usr/bin/xdm -nodaemon
to:
id:5:initdefault:
[...]
x:5:once:/bin/su - -- ''PREFERRED_USER'' -l -c '/usr/bin/startx </dev/null >/dev/null 2>&1'
Two workarounds are needed for the combination of GNU [[su]] and [[Bash]] (see [http://linux.derkeiler.com/Newsgroups/comp.os.linux.misc/2006-09/msg00666.html "su 5.2.1 does not invoke bash as a login shell"]):
* Bash does not do the normal login process in non-interactive login mode unless it is forced with the {{ic|-l}} option. It is non-interactive because of the {{ic|-c}} option.
* The {{ic|--}} option ensures that the {{ic|-l}} and {{ic|-c}} options are passed to the shell rather than used by ''su'' itself.
Expanation of other elements:
* The {{ic|-}} option invokes a "login shell" by prepending a dash (-) to its name.
* The standard input must be redirected ({{ic|</dev/null}}) if [[Getty]] or some other program is still used on the console, otherwise there will be a conflict between multiple programs stealing console input from each other.
* The output can also be redirected ({{ic|>/dev/null 2>&1}}) to avoid outputting messages from X to the console.
The field populated with {{ic|once}} may be changed to {{ic|respawn}} which will restart X if it exits.
{{ic|startx}} may be changed to any desired command or script. For example:
{{bc|startx -- -nolisten tcp -br -deferglyphs 16}}
Also you can do this for multiple users using different runlevels:
x1:4:once:/bin/su - -- ''PREFERRED_USER1'' -l -c '/usr/bin/startx </dev/null >/dev/null 2>&1'
x2:5:once:/bin/su - -- ''PREFERRED_USER2'' -l -c '/usr/bin/startx </dev/null >/dev/null 2>&1'
and adding two entries in your bootloader's configuration with the parameters {{ic|4}} and {{ic|5}}. See [[Kernel parameters]] for more info.
{{Note|1=If you have problems with writing non-ASCII letters within terminals in that new X, remove the {{ic|-l}} switch ([https://bugs.gentoo.org/show_bug.cgi?id=339182#c4 see here]).}}

Revision as of 17:59, 3 September 2012

zh-CN:Start X at Boot Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end

The majority of users use a display manager to start the X server. This article introduce two methods without a display manager. The bash_profile method will start X once logged in from a tty. The inittab way allows automatically starting X without supplying a password.

To manually start X, startx or xinit are used. Both will execute ~/.xinitrc, which may be customized to start the window manager of choice as described in the xinitrc article.

bash_profile

An alternative to a login manager is to add the following to the bottom of your ~/.bash_profile (if ~/.bash_profile does not yet exist, you can copy a skeleton version from /etc/skel/.bash_profile. If you use zsh as your preferred shell, add the following lines to your ~/.zprofile instead.):

~/.bash_profile
if [[ -z $DISPLAY && $(tty) = /dev/tty1 ]]; then
  exec startx -- vt$(fgconsole)
fi

or to have it work at any console:

~/.bash_profile
if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]]; then
  exec startx -- vt$(fgconsole)
fi

Or with a prompt:

~/.bash_profile
if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]]; then
  while true; do
    read -p 'Do you want to start X? (y/n): '
    case $REPLY in
      [Yy]) exec xinit -- -nolisten tcp vt7 ;;
      [Nn]) break ;;
      *) printf '%s\n' 'Please answer y or n.' ;;
    esac
  done
fi

The user will be logged out when X is killed. In order to avoid this, remove the exec part from the script.

Note: This method can be combined with automatic login to virtual console and act similar to the inittab method, but it will properly register your session and works with ConsoleKit.