Difference between revisions of "Start X at login"

From ArchWiki
Jump to navigation Jump to search
m (stupid dot)
(fix double redirect)
 
(149 intermediate revisions by 46 users not shown)
Line 1: Line 1:
{{i18n|Start X at Boot}}
+
#REDIRECT [[Xinit#Autostart_X_at_login]]
[[Category:X Server (English)]]
 
[[Category:Boot process (English)]]
 
[[fr:Startx]]
 
{{Article summary start}}
 
{{Article summary text|Covers the various methods one can use to start an [[Xorg|X server]] during the boot process.}}
 
{{Article summary heading|Related}}
 
{{Article summary wiki|Automatic login to virtual console}}
 
{{Article summary wiki|Display Manager}}
 
{{Article summary wiki|Xinitrc}}
 
{{Article summary end}}
 
 
 
Typically, {{Filename|startx}} or {{Filename|xinit}} are used to manually start X. Both will execute {{Filename|~/.xinitrc}}, which may be customized to start the window manager of choice as described in the [[xinitrc]] article.
 
 
 
The majority of users wishing to start an X server during the boot process will want to install a [[Display Manager]] instead.
 
 
 
By using the [[#~/.bash_profile|~/.bash_profile]] method, X will be started once logged in from a tty. The [[#/etc/inittab|/etc/inittab]] way allows automatically starting X without supplying a password.
 
 
 
__TOC__
 
=={{Filename|~/.bash_profile}}==
 
{{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 work with ConsoleKit.}}
 
An alternative to a login manager is to add the following to the bottom of your {{Filename|~/.bash_profile}} (if {{Filename|~/.bash_profile}} does not yet exist, you can copy a skeleton version from {{Filename|/etc/skel/.bash_profile}}. If you use [[zsh]] as your preferred shell, add the following lines to your {{Filename|~/.zprofile}} instead.):
 
 
 
{{File
 
|name=~/.bash_profile
 
|content=<nowiki>
 
if [[ -z $DISPLAY && $(tty) = /dev/tty1 ]]; then
 
  exec startx
 
  # Could use xinit instead of startx
 
  #exec xinit -- /usr/bin/X -nolisten tcp vt7
 
fi
 
</nowiki>}}
 
 
 
or with additional checking (if tty1 ({{Keypress|Ctrl}} + {{Keypress|Alt}} + {{Keypress|F1}}) shows an error message):
 
 
 
{{File
 
|name=~/.bash_profile
 
|content=<nowiki>
 
if [[ -z $DISPLAY && ! -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
 
  exec startx
 
fi
 
</nowiki>}}
 
 
 
Or with a prompt:
 
 
 
{{File
 
|name=~/.bash_profile
 
|content=<nowiki>
 
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
 
</nowiki>}}
 
 
 
The user will be logged out when X is killed. In order to avoid this, remove the {{Codeline|exec}} part from the script.
 
 
 
=={{Filename|/etc/inittab}}==
 
{{Warning|This method will not use {{Filename|/bin/login}} or register your session, therefore no session will appear in `who` or `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 {{Codeline|sudo}} or {{Codeline|su}}.}}
 
Another way of circumventing display managers and booting straight into a preferred window manager or desktop environment involves editing {{Filename|/etc/inittab}}, changing:
 
 
 
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 {{Codeline|-}} option invokes a "login shell" by prepending a dash (-) to its name. Because a command is specified with the {{Codeline|-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 {{Codeline|-l}} option. The {{Codeline|--}} option ensures that the {{Codeline|-l}} and {{Codeline|-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 [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"].
 
 
 
The standard input must be redirected ({{Codeline|</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 ({{Codeline|>/dev/null 2>&1}}) to avoid outputting messages from X to the console.
 
 
 
The field populated with {{Codeline|once}} may be changed to {{Codeline|respawn}} which will restart X if it exits.
 
 
 
Additionally, {{Codeline|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 {{Filename|/boot/grub/menu.lst}}:
 
# (0) Arch Linux
 
title  Arch Linux USER1
 
root  (hd0,0)
 
kernel /vmlinuz-linux root=/dev/disk/by-label/Arch ro '''4'''
 
initrd /initramfs-linux.img
 
 
# (1) Arch Linux
 
title  Arch Linux USER2
 
root  (hd0,0)
 
kernel /vmlinuz-linux root=/dev/disk/by-label/Arch ro '''5'''
 
initrd /initramfs-linux.img
 
 
 
{{Note|1=If you have problems with writing non-ASCII letters within terminals in that new X, remove the {{Codeline|-l}} switch ([http://bugs.gentoo.org/show_bug.cgi?id=339182#c4 see here]).}}
 

Latest revision as of 10:37, 24 July 2016