Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

loginctl

Show and control user logins (sessions), users and seats of systemd-logind — and turn on "linger" so a user's services run without a login.

Level
Everyday Linux
Group
Users and groups
Needs root
For changes
Package
systemd

What it does

Every login (console, SSH, desktop) is a session. loginctl lists sessions and users, shows their processes, locks or ends them, and controls linger: with linger on, systemd starts the user's own service manager at boot and keeps it running after logout. Rootless Podman containers and user timers need linger to run when the user is not logged in.

How to write it

loginctl [OPTION]... COMMAND [NAME]...

Main parts

Sessions

list-sessions
List the current sessions.
session-status [ID]...
Status and processes of a session.
show-session [ID]...
Properties of a session (for scripts).
activate [ID]
Bring a session to the front.
lock-session / unlock-session [ID]...
Lock or unlock the screen of a session.
lock-sessions / unlock-sessions
Lock or unlock all sessions.
terminate-session ID...
End a session (all its processes).
kill-session ID...
Send a signal to the processes of a session.

Users

list-users
List logged-in users (and users with linger).
user-status [USER]...
Status and processes of a user.
show-user [USER]...
Properties of a user (Linger=, State=).
enable-linger [USER]...
Keep the user's service manager running without a login.
disable-linger [USER]...
Turn linger off.
terminate-user USER...
End all sessions of a user.
kill-user USER...
Send a signal to all processes of a user.

Seats

list-seats / seat-status / show-seat
Seats (a screen, keyboard and mouse set).
attach NAME DEVICE...
Give a device to a seat.
flush-devices
Remove all device-to-seat assignments.
terminate-seat NAME...
End all sessions on a seat.

Options 13

Most used

Options

-p, --property=NAME
Show only this property (with show-*).
--value
Print only the value (with -p).
-a, --all
Show also empty properties.
-l, --full
Do not cut long lines.
--kill-whom=leader|all
With kill-*: the leader process or all processes.
-s, --signal=SIGNAL
The signal for kill-* (default SIGTERM).
-n, --lines=N
Journal lines in *-status.
-o, --output=MODE
Format of those journal lines.
-H, --host=HOST
Run on a remote host over SSH.
-M, --machine=NAME
Run in a container.
--no-ask-password
Do not ask for a password (polkit).
--no-pager / --no-legend
Plain output for scripts.
-h, --help / --version
Help / version.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Who is logged in?

    loginctl list-sessions
    SESSION  UID USER    SEAT  TTY   STATE  IDLE SINCE
          3 1000 student seat0 tty2  active no   -
         12 1001 anna    -     pts/1 active no   -
    loginctl list-users
  2. 2Look at one session and its processes

    loginctl session-status 12
  3. 3End the session of a user

    loginctl terminate-session 12
    loginctl terminate-user anna

    Ends all logins and processes of anna. Tell the user first.

  4. 4Let a user's containers and timers run without a login

    loginctl enable-linger student
    loginctl show-user student -p Linger --value
    yes
    ls /var/lib/systemd/linger/
    student
  5. 5Turn linger off again

    loginctl disable-linger student
  6. 6Lock all screens (for example before a desktop maintenance)

    loginctl lock-sessions
  7. 7Kill only the processes of a user with a signal

    loginctl kill-user omar --signal=SIGKILL
  8. 8Use a property in a script

    loginctl show-session $XDG_SESSION_ID -p Type --value
    wayland
  9. 9Check if a user's service manager runs

    loginctl user-status student | head -5
    systemctl status user@1000.service
  10. 10Show the idle time of every session

    loginctl list-sessions --no-legend | while read id uid user rest; do echo "$user $(loginctl show-session $id -p IdleHint --value)"; done

Watch out

  • Without linger, user services (Quadlet containers, user timers) stop at logout and do not start at boot.
  • terminate-user ends all processes of the user, also running jobs.

Files

/var/lib/systemd/linger/
One file per user with linger on.
/etc/systemd/logind.conf, /etc/systemd/logind.conf.d/
Settings of systemd-logind (idle actions, KillUserProcesses).

Tips to remember

  • loginctl enable-linger without a name works for your own user if polkit allows it.

Exit codes

0Success.
Not 0Error.