Start X Server on Login with Systemd

18,839

Solution 1

Booting direcly into X11 using startx, with a free choice of the active user, see https://vincent.bernat.ch/en/blog/2021-startx-systemd

Comparing to Archlinux guide, auto login to TTYx is not needed anymore, this is direct graphical-target startup.

For an embedded/kiosk/set-top-box system, I changed the service to restart automatically with a small delay

[Unit]
Description=X11 session for bernat
After=graphical.target systemd-user-sessions.service

[Service]
User=bernat
WorkingDirectory=~

PAMName=login
Environment=XDG_SESSION_TYPE=x11
TTYPath=/dev/tty8
StandardInput=tty
UnsetEnvironment=TERM

UtmpIdentifier=tty8
UtmpMode=user

StandardOutput=journal
ExecStartPre=/usr/bin/chvt 8
ExecStart=/usr/bin/startx -- vt8 -keeptty -verbose 3 -logfile /dev/null
#Restart=no
Restart=always
RestartSec=3

[Install]
WantedBy=graphical.target

Remember to activate the graphical-target with

sudo systemctl set-default graphical.target

I found this to be the best working solution when creating an embedded Linux, like a kiosk or set-top-box system with Debian. Fast boot, directly to graphical application.

Solution 2

This is a security restriction enforced by XOrg by default. You can diable it by editing /etc/X11/Xwrapper.config, and changing allowed_users from console to anybody.

I've tested this by executing startx from a systemd unit file, and it works fine.

Source: the same document mentioned by Lyle.

Share:
18,839
Nathan Lilienthal
Author by

Nathan Lilienthal

Updated on September 18, 2022

Comments

  • Nathan Lilienthal
    Nathan Lilienthal over 1 year

    I know that this is not the normal way people use X. Most setup instructions tell you to put something like startx in your shell's profile. However I want to have systemd manage things.

    Ideally I would have something like systemctl --user enable x.service which is run when my user is logged in, and sources my .xinitrc thus starting my WM.

    All my attempts to do this have given me the error:

    /usr/lib/xorg/Xorg.wrap: Only console users are allowed to run the X server
    

    There must be a way to do this. Any help would be greatly appreciated.

    • dirkt
      dirkt about 6 years
      I've no idea what setup instructions you read, but putting startx in the shell profile is utterly ridiculous (just think what happens when you log in remotely into several shells). The normal way to do this is to start a display manager (e.g. lightdm, or any you want), which in turn starts the X server. Starting the X server without a display manager will be a bit tricky; you'll have to setup all the stuff the display manager does for you (authorization cookies, etc.) yourself.
    • Nathan Lilienthal
      Nathan Lilienthal almost 6 years
      @dirkt while I agree (hence the question), it does seem to be a pretty common practice. See wiki.archlinux.org/index.php/Xinit#Autostart_X_at_login.
    • dirkt
      dirkt almost 6 years
      People do a lot of stupid things (and like to blog about it), but that's not a reason to copy it.
    • jasonwryan
      jasonwryan almost 6 years
    • Nathan Lilienthal
      Nathan Lilienthal almost 6 years
      @jasonwryan that link is promising, the second option seems ideal, but wrought with issues. I'll give it a shot when I have some free time and report back. P.S. I don't know how I missed that when searching this issue on my own.
    • gpanders
      gpanders over 5 years
      @NathanLilienthal Any success? I'd love to know if you were able to do this
    • Nathan Lilienthal
      Nathan Lilienthal almost 3 years
      @gpanders, I wasn't able to get it setup right in my last attempts. It's been a while now, I regret not remembering the exact issue. However, NixOS (despite being far from usable as a daily driver) has a compelling X setup. Perhaps I can just read how that was configured closely sometime. All this being said, I've been running Wayland for over a year with few issues.
  • jasonwryan
    jasonwryan almost 6 years
    The OP specifically wants this handled by systemd...
  • CenterOrbit
    CenterOrbit over 2 years
    Welcome bra'tac! This is an excellent answer (worked great for me). Would you please include the full x11 service file from that site (and not just your restart changes) along with the enable in your answer, as so there is not an external link dependency?