On multi-user Linux servers and desktop workstations, managing active graphical seats, orphaned SSH sessions, and user-level background daemons can become challenging. Traditional process management tools like ps or who reveal who is logged in, but they lack the ability to isolate systemd user slices or control session lifecycles cleanly. The loginctl utility is the official command-line interface for systemd-logind, giving system administrators precise control over user logins, seats, and lingering background processes.
Core Architecture of systemd-logind and loginctl
Under modern Linux distributions (such as Ubuntu, Debian, Fedora, and Arch Linux), systemd-logind organizes user activity into three distinct hierarchical primitives:
- Sessions: An individual login instance, such as an interactive SSH terminal connection, a Wayland desktop session, or a local TTY console.
- Users: The user account associated with one or more active sessions, managed inside a dedicated cgroup slice (
user-1000.slice). - Seats: A collection of physical hardware peripherals (displays, keyboards, mice, and sound cards) assigned to a single interactive workstation.
Inspecting Active User Sessions
To inspect who is currently logged in and view their session identifiers:
loginctl list-sessions
The output displays the numeric SESSION ID, the target UID, the USER name, and the assigned SEAT (e.g., seat0 for local displays).
To inspect detailed metadata for a specific session (such as session 2):
loginctl session-status 2
This command outputs the session state (active or idle), the display server type (Wayland or X11), the remote IP address, and the complete process cgroup tree running inside the session.
Locking, Terminating, and Killing User Sessions
When troubleshooting an unresponsive graphical session or terminating an orphaned SSH login:
1. Locking a Screen Remotely
To lock the active desktop screen for a specific user session:
loginctl lock-session 2
2. Terminating a Specific Session Cleanly
To gracefully log out a user session and terminate all its associated child processes:
loginctl terminate-session 2
3. Terminating All Sessions for a Specific User
If a remote developer logged into multiple terminals and left background tasks running:
sudo loginctl terminate-user developer
Enabling User Lingering for Persistent Background Services
By default, systemd terminates all user processes as soon as the user logs out of their last active session. If you deploy self-hosted Docker containers, tmux workspaces, or user-level systemd timer services (systemctl --user), they will be killed immediately upon SSH disconnect. You can prevent this behaviour by enabling user lingering:
sudo loginctl enable-linger username
When user lingering is enabled, systemd automatically spawns the user’s manager daemon at system boot and keeps all user services alive indefinitely, even when the user is not actively logged in.
To verify which users have lingering enabled:
ls /var/lib/systemd/linger
To disable lingering when it is no longer required:
sudo loginctl disable-linger username