Start X at Boot
Summary |
---|
Covers bash_profile and inittab methods to start an X server during the boot process. |
Related |
Automatic login to virtual console |
Display Manager |
Xinitrc |
The majority of users use display manager to start X server. This article introduce two methods without 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.
1 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 # Could use xinit instead of startx #exec xinit -- /usr/bin/X -nolisten tcp vt7 fi
or with additional checking (if tty1 (Ctrl+Alt+F1) shows an error message):
~/.bash_profile
if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then exec startx fi
Or with a prompt:
~/.bash_profile
if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then while true; do read -p 'Do you want to start X? (y/n): ' case $REPLY in [Yy]) exec xinit -- /usr/bin/X -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.
2 inittab
Another way of circumventing display managers and booting straight into a preferred window manager or desktop environment involves editing /etc/inittab.
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'
- The - option invokes a "login shell" by prepending a dash (-) to its name.
- Because a command is specified with the -c option, the shell is also run in "non-interactive mode".
- Bash does not do the normal login process in non-interactive login mode unless it is forced with the -l option.
- The -- option ensures that the -l and -c options are passed to the shell rather than used by su itself. These workarounds are needed for the combination of GNU su and Bash; see "su 5.2.1 does not invoke bash as a login shell".
- The standard input must be redirected (</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 (>/dev/null 2>&1) to avoid outputting messages from X to the console.
- The field populated with once may be changed to respawn which will restart X if it exits.
- startx may be changed to any desired command or script. For example:
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 inserting a new entry in GRUB's /boot/grub/menu.lst:
# (0) Parabola GNU/Linux-Libre title Parabola GNU/Linux-Libre USER1 root (hd0,0) kernel /vmlinuz-linux-libre root=/dev/disk/by-label/Libre ro 4 initrd /initramfs-linux-libre.img # (1) Parabola GNU/Linux-Libre title Parabola GNU/Linux-Libre USER2 root (hd0,0) kernel /vmlinuz-linux-libre root=/dev/disk/by-label/Libre ro 5 initrd /initramfs-linux-libre.img