systemd-logind and loginctl: Sessions, Seats and Why Your Service Dies at Logout

systemd-logind and loginctl: Sessions, Seats and Why Your Service Dies at Logout

logind is the part of systemd that knows a human is at the machine. That sounds unimportant until you realise it decides who may use the GPU, who may reboot, what happens when you close the lid, and whether your background process survives logout.

What it tracks

Three concepts, and the distinctions matter.

A session is one login. A graphical login, an ssh connection, a TTY login, each is a session.

A user can hold several sessions at once.

A seat is a set of hardware at one physical workplace: a GPU, keyboard, mouse, audio. Almost every machine has exactly one, called seat0.

loginctl
loginctl list-sessions
loginctl list-users
loginctl list-seats

# detail on one session
loginctl show-session 2
loginctl session-status 2
SESSION  UID USER  SEAT  TTY   STATE  IDLE
      2 1000 colton seat0 tty2  active no
      5 1000 colton       pts/1 active no

The second has no seat, because an ssh session has no physical hardware attached. That absence is why a remote session cannot do several things a local one can.

Active versus online

loginctl show-session 2 -p Active -p State -p Type -p Class
StateMeaning
activeIn the foreground, holds the seat’s hardware
onlineLogged in, not in front
closingEnding, processes still exiting

Only one graphical session can be active per seat. Switch users and the first goes online while the second becomes active, which is what hands over control of the GPU and input devices.

This drives polkit decisions directly:

pkaction --verbose --action-id org.freedesktop.login1.reboot
implicit active:   yes
implicit inactive: auth_admin_keep
implicit any:      auth_admin_keep

So rebooting from the session in front of you is permitted outright, and the same request over ssh requires admin authentication. logind supplies the context that makes that distinction possible.

Device access

The mechanism most people never see and depend on constantly.

A Wayland compositor needs the GPU and input devices. Historically that meant running the display server as root. logind instead hands file descriptors for the seat’s devices to the active session, so the compositor runs unprivileged.

# what belongs to the seat
loginctl seat-status seat0 | head -30

# the session's take on it
loginctl show-session 2 -p Seat -p VTNr

That is why a compositor started from an ssh session usually fails: no seat, so no device access. Our display managers guide covers the session startup path.

The logout problem

The behaviour that catches everyone at least once.

ssh server
./long-running-job &
exit
# job is dead

logind removes a user’s slice when their last session ends, killing anything left in it. nohup and disown do not help, because this is not about SIGHUP.

# the setting responsible
grep KillUserProcesses /etc/systemd/logind.conf

The fixes, in order of preference

Lingering, which keeps the user’s slice alive without a session:

sudo loginctl enable-linger colton
loginctl show-user colton -p Linger

# undo
sudo loginctl disable-linger colton

Lingering is also what lets a systemd user service run at boot before anyone logs in, which is the usual reason to enable it.

A systemd user service, which is the properly durable answer:

# ~/.config/systemd/user/myjob.service
[Unit]
Description=My long job

[Service]
ExecStart=/home/colton/bin/long-job
Restart=on-failure

[Install]
WantedBy=default.target
systemctl --user daemon-reload
systemctl --user enable --now myjob
sudo loginctl enable-linger colton    # so it survives logout and starts at boot

A transient scope, for a one-off:

systemd-run --user --scope --unit=myjob ./long-running-job
# or outside the session's slice entirely
sudo systemd-run --unit=myjob ./long-running-job

A terminal multiplexer, which is what most people reach for:

tmux new -s work
# detach with Ctrl+b d

Note that tmux is also affected by KillUserProcesses unless lingering is enabled or the distribution has configured an exception. Our screen versus tmux guide covers the tool, and enable-linger is the part that makes detaching reliable.

Configuration

Use drop-ins rather than editing the main file, so updates do not overwrite your changes.

# /etc/systemd/logind.conf.d/10-local.conf
[Login]
HandleLidSwitch=suspend
HandleLidSwitchExternalPower=ignore
HandleLidSwitchDocked=ignore
HandlePowerKey=poweroff
HandleSuspendKey=suspend
IdleAction=suspend
IdleActionSec=30min
KillUserProcesses=no
RuntimeDirectorySize=20%
sudo systemctl restart systemd-logind
systemd-analyze cat-config systemd/logind.conf

Restarting logind can terminate your graphical session. It is documented behaviour rather than a bug: logind holds the session’s device descriptors, and restarting it invalidates them. Do it from a console, or reboot.

Our suspend guide covers the lid and idle options in context.

RuntimeDirectorySize controls XDG_RUNTIME_DIR, which is a tmpfs and therefore consumes RAM. Our XDG guide covers what lives there.

Inhibitors

How an application says “do not suspend right now”.

# what is currently blocking what
systemd-inhibit --list
WHO          UID  USER   PID  COMM    WHAT                     WHY
ModemManager 0    root   812  Model.. sleep                    ModemManager needs to...
mpv          1000 colton 4521 mpv     idle                     Playing video
# block sleep for the duration of a command
systemd-inhibit --what=idle:sleep --why="Running backup" \
  /usr/local/bin/backup.sh

systemd-inhibit wrapping a long job is the correct way to stop a laptop suspending mid-backup, and it is better than disabling suspend because it only applies while the job runs. Worth adding to the scripts in our automated backups guide and Btrfs replication guide.

When a machine refuses to suspend, systemd-inhibit --list is the first thing to check.

Other useful operations

# terminate a session, or everything a user has
sudo loginctl terminate-session 5
sudo loginctl terminate-user colton

# lock all sessions
loginctl lock-sessions

# switch to a session, so VT switching from the command line
sudo loginctl activate 2

# power operations, which go through polkit
loginctl poweroff
loginctl reboot
loginctl suspend

loginctl terminate-user is the clean way to end a stuck graphical session without rebooting, and it is tidier than hunting processes with our killing processes guide.

Sessions that are not people

loginctl list-sessions
loginctl show-session 9 -p Class -p Type -p Remote

Class values worth recognising:

  • user, a person
  • greeter, the display manager’s own session
  • background, cron jobs, systemd user services, automated logins

A background session with no seat is almost always one of those rather than someone logged in. Our cron guide covers why cron creates one: it authenticates through PAM, and PAM’s session stack registers it.

That chain is worth holding in mind, because it is how the pieces connect: PAM authenticates and creates the session, logind tracks it, polkit uses its state to make decisions, and the compositor receives its device access from logind. Four components, one login.

Frequently Asked Questions

What does systemd-logind do?

It tracks sessions, users and seats, decides which session is active, manages device access for the active session, handles lid and power key events, and provides the session information other software consults. It is the piece that knows a human is at the machine and which session they are in.

Why does my background process die when I log out of ssh?

Because logind cleans up a user slice when their last session ends, which kills remaining processes. Enable lingering for that user with loginctl enable-linger, or run the work as a systemd user service, which is the more durable arrangement than nohup.

What is a seat?

A set of hardware attached to one physical workplace: a graphics card, keyboard, mouse and audio. Nearly every machine has one seat named seat0. Multi-seat exists for one computer with two monitors, keyboards and mice supporting two independent users.

What is the difference between an active and an online session?

Active means the session is in the foreground with control of the seat hardware, and only one graphical session can be active per seat. Online means logged in but not in front, which is where a session goes when you switch users. The distinction drives polkit decisions about what is permitted.

How do I stop logind from suspending when I close the lid?

Set HandleLidSwitch to ignore in a drop-in file under logind.conf.d, then restart logind. Note that restarting logind can terminate your graphical session on some setups, so do it from a console or accept being logged out.

Why does loginctl show sessions that are not real?

Cron jobs, systemd user services and some automated logins create sessions through PAM too, so they appear in the list. A session with class background or no seat assigned is usually one of these rather than a person, and the class and seat columns are how you tell them apart.