Strovent SystemsSign in
Reference

tmux killed on SSH logout: systemd KillUserProcesses and linger

tmux or screen gone after logout? systemd-logind ended your session scope. Set KillUserProcesses=no, or enable linger and start tmux with systemd-run.

On a Linux host with systemd, each login gets its own session scope. When KillUserProcesses=yes is in effect, systemd-logind ends that scope, and every process still in it, when you log out. A tmux or screen server you started from that login is in the scope, so it is killed too. The next login finds no server running on /tmp/tmux-1000/default or no sessions.

The logind.conf manual says this directly: KillUserProcesses=yes breaks screen and tmux unless they are moved out of the session scope.

Check that this is the cause

Ask logind for the value it is using:

busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses

b true means processes in a session are killed at logout, except for users excluded by KillExcludeUsers= (root, unless that option is set). The manual gives yes as the default, but the value on your host depends on how its systemd was built and configured. To see the main file and every drop-in that sets it, run systemd-analyze cat-config systemd/logind.conf. A package can install its own drop-in under /usr/lib/systemd/logind.conf.d/ that turns it on.

To see where a running tmux server lives, print its cgroup:

cat /proc/$(tmux display -p '#{pid}')/cgroup

A path ending in session-3.scope (any number) means the server is inside your login session and ends with it.

Fix 1: turn it off for the host or for your user

This needs root. Create a drop-in, for example /etc/systemd/logind.conf.d/60-keep-processes.conf:

[Login]
KillUserProcesses=no

To keep the setting on for others and exclude only some users, use KillExcludeUsers= instead. root is excluded by default only while KillExcludeUsers= is unset, so list it again:

[Login]
KillExcludeUsers=root user1

Drop-ins in /etc/systemd/logind.conf.d/ override the main logind.conf. When two drop-ins set the same option, the file whose name sorts last wins, so a two-digit prefix such as 60- puts yours after package drop-ins (the manual suggests 10–40 for packages). systemd-logind reads its configuration when it starts, so the change applies after a reboot or a restart of that service.

Fix 2: run tmux outside the session, and enable linger

Without changing the host setting, start tmux as a child of your systemd user manager instead of your login:

loginctl enable-linger
systemd-run --scope --user tmux new-session -A -s main
  • systemd-run --scope --user starts the command in its own scope under [email protected], so it is not part of the session scope. The systemd-run manual uses this example for screen.
  • The user manager itself stops when you log out of your last session, unless lingering is on. loginctl enable-linger starts your user manager at boot and keeps it after logout. Your host may require root or an administrator's approval for it.
  • Check it with loginctl show-user user1 -p Linger.

Lingering alone does not help: a tmux started the normal way is still in the session scope and is still killed.

mosh-server is affected the same way

Mosh starts mosh-server through a short SSH login and then closes that login. With KillUserProcesses=yes, the server is in that login's scope, so it can be ended as soon as the SSH part closes, and the client gets nothing back. Fix 1 is the reliable fix for Mosh. See Mosh "Nothing received from server on UDP port 60001" for the other causes of that message.

Sources

Spotted a mistake? Email [email protected].