Difference between revisions of "Start X at login"

From ArchWiki
Jump to navigation Jump to search
m (added a systemd service.)
m (→‎Using systemd: added a hint about debugging.)
Line 79: Line 79:
 
</nowiki>}}
 
</nowiki>}}
  
Make sure {{ic|~.xinitrc}} exists and properly setup.
+
Make sure {{ic|~.xinitrc}} exists and is properly setup.
  
 
Then enable the service {{ic|sudo systemctl enable xinit@your-user-name}} / run it {{ic|sudo systemctl start xinit@your-user-name}}.
 
Then enable the service {{ic|sudo systemctl enable xinit@your-user-name}} / run it {{ic|sudo systemctl start xinit@your-user-name}}.
 +
 +
*hint* remove {{ic|>/dev/null 2>&1}} for debug.
  
 
== See also ==
 
== See also ==
 
[http://unix.stackexchange.com/questions/62722/start-x-after-automatic-login Start X after automatic login]
 
[http://unix.stackexchange.com/questions/62722/start-x-after-automatic-login Start X after automatic login]

Revision as of 19:56, 8 November 2014

zh-CN:Start X at Login zh-TW:Start X at Login

This article explains how to have the X server start automatically right after logging in at a virtual terminal. This is achieved by running the startx command, whose behaviour can be customized as described in the xinitrc article, for example for choosing what window manager to launch. Alternatively, a display manager can be used to start X automatically and provide a graphical login screen.

Shell profile files

Note: These solutions run X on the same tty used to login, which is required in order to maintain the login session.

For Bash, add the following to the bottom of ~/.bash_profile. If the file does not exist, copy a skeleton version from /etc/skel/.bash_profile. For Zsh, add it to ~/.zlogin (or ~/.zprofile) instead.

[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && exec startx
Note:
  • You can replace the -eq 1 comparison with one like -le 3 (for vt1 to vt3) if you want to use graphical logins on more than one VT.
  • X must always be run on the same tty where the login occurred, to preserve the logind session. This is handled by the default /etc/X11/xinit/xserverrc.
  • xinit may be faster than startx, but needs additional parameter such as -nolisten tcp.

For Fish, add the following to the bottom of your ~/.config/fish/config.fish.

# start X at login
if status --is-login
    if test -z "$DISPLAY" -a $XDG_VTNR = 1
        exec startx
    end
end

Tips and tricks

  • This method can be combined with automatic login to virtual console. When doing this you have to set correct dependencies for the autologin systemd service to ensure that dbus is started before ~/.xinitrc is read and hence pulseaudio started (see: BBS#155416)
  • If you would like to remain logged in when the X session ends, remove exec.
  • To redirect the output of the X session to a file, create an alias:
alias startx='startx &> ~/.xlog'
Note: Redirecting stderr (startx &>) breaks rootless startup, see Xorg#Rootless Xorg (v1.16).

Using systemd

Create a service file (for example /etc/systemd/system/xinit@.service):

[Unit]
Description=startx for user %i
After=x@vt7.service systemd-user-sessions.service
Wants=x@vt7.service
Conflicts=getty@tty7.service

[Service]
User=%i
TTYPath=/dev/tty7
PAMName=login
Environment=DISPLAY=:0
WorkingDirectory=/home/%I
Nice=0
ExecStart=/bin/bash -l -c "cd; startx >/dev/null 2>&1"

[Install]
WantedBy=graphical.target

Make sure ~.xinitrc exists and is properly setup.

Then enable the service sudo systemctl enable xinit@your-user-name / run it sudo systemctl start xinit@your-user-name.

  • hint* remove >/dev/null 2>&1 for debug.

See also

Start X after automatic login