loginctl

最後更新: 2023-12-22

介紹

loginctl used to introspect and control the state of the systemd-logind.service(systemd login manager)

 


Commands

 

它一共有分 3 類 commands

  • Session Commands
  • User Commands
  • Seat Commands

 


User Commands

 

list-users

 UID USER
   0 root
1001 mr

2 users listed.

user-status [username]

mr (1001)
           Since: Fri 2023-12-15 12:13:31 HKT; 1 weeks 5 days ago
           State: lingering
          Linger: yes
            Unit: user-1001.slice
                  └─[email protected]
                    ├─dbus.service
                    │ └─3375854 /usr/bin/dbus-daemon --session --address=system>
                    ├─init.scope
                    │ ├─3375786 /usr/lib/systemd/systemd --user
                    │ └─3375789 (sd-pam)
                    ...

loginctl show-user [username]

UID=1001
GID=1001
Name=mr
Timestamp=Fri 2023-12-15 12:13:31 HKT
TimestampMonotonic=2150808985189
RuntimePath=/run/user/1001
[email protected]
Slice=user-1001.slice
State=lingering
Sessions=
IdleHint=yes
IdleSinceHint=0
IdleSinceHintMonotonic=0
Linger=yes

enable-linger [USER]

terminate-user [USER]

kill-user [USER]

 


Session Commands

 

list-sessions

SESSION UID USER SEAT TTY
    127   0 root      pts/0

1 sessions listed.

session-status [ID…]

show-session [ID…]

EnableWallMessages=no
...
IdleAction=ignore
IdleActionUSec=30min
...
SessionsMax=8192
NCurrentSessions=1
...

activate [ID]

lock-session [ID…], unlock-session [ID…]

lock-sessions, unlock-sessions

terminate-session ID

kill-session ID

 


Seat Commands

 

 

list-seats             # List currently available seats on the local system.

SEAT
seat0

1 seats listed.

seat-status [NAME…]

seat0
         Devices:
                  ├─/sys/devices/LNXSYSTM:00/LNXPWRBN:00/input/input0
                  │ input:input0 "Power Button"
                  ├─/sys/devices/pci0000:00/0000:00:0f.0/drm/card0
                  │ [MASTER] drm:card0
                  ...

show-seat seat0

Id=seat0
CanMultiSession=yes
CanTTY=yes
CanGraphical=yes
Sessions=
IdleHint=yes
IdleSinceHint=0
IdleSinceHintMonotonic=0

attach NAME DEVICE

flush-devices

terminate-seat NAME        # Terminates all sessions on a seat.
 


Enabled linger

 

lingering

If enabled for a specific user, a user manager is spawned for the user at boot and kept around after logouts.
(TTY session)

應用

  • Running docker after exit ssh

Checking

loginctl show-user USERNAME

ie.

loginctl show-user openai-proxy

output 1:

Failed to get user: User ID 1001 is not logged in or lingering

output 2: ssh 連線後

UID=1001
GID=1001
Name=openai-proxy
Timestamp=Fri 2023-12-01 16:50:32 HKT
TimestampMonotonic=198206343
RuntimePath=/run/user/1001
[email protected]
Slice=user-1001.slice
Display=5
State=active
Sessions=5
IdleHint=no
IdleSinceHint=1701420632938903
IdleSinceHintMonotonic=198346556
Linger=no

output 3: 當 ssh 斷後

...
State=closing
...

Enable

loginctl enable-linger openai-proxy    # 用 user 自身的身份執行也可

loginctl show-user openai-proxy

State=lingering
Sessions=
IdleHint=yes
IdleSinceHint=0
IdleSinceHintMonotonic=0
Linger=yes

 

Creative Commons license icon Creative Commons license icon