loginctl - Control the systemd login manager
Redhat/centos平台使用loginctl管理登录用户与session
loginctl [OPTIONS...] {COMMAND} [NAME...]
[root@blog ~]# whereis loginctl
loginctl: /usr/bin/loginctl /usr/share/man/man1/loginctl.1.gz
[root@blog ~]# rpm -qf /usr/bin/loginctl
systemd-239-18.el8_1.4.x86_64
[root@blog ~]# yum install systemd
[root@node-137 ~]# loginctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
[root@blog ~]# loginctl --help
loginctl在使用时需要systemd-logind.service开启
[root@redis local]# loginctl list-sessions
Failed to list-sessions: Unit systemd-logind.service is masked.
[root@redis local]# systemctl start systemd-logind.service
Failed to start systemd-logind.service: Unit systemd-logind.service is masked.
#如果服务被mask,需要先取消mask再start
[root@redis local]# systemctl unmask systemd-logind.service
Removed /etc/systemd/system/systemd-logind.service.
[root@redis local]# systemctl start systemd-logind.service
[root@node-137 ~]# loginctl
SESSION UID USER SEAT
31 1000 yurq
15 0 root
14 0 root
30 1000 yurq
4 sessions listed.
可以看到:同一个用户可以开启多个session
[root@node-137 ~]# loginctl show-session 30
Id=30
User=1000
Name=yurq
Timestamp=Fri 2023-11-24 13:16:25 CST
TimestampMonotonic=12800340928
VTNr=0
Remote=yes
RemoteHost=192.168.17.1
Service=sshd
Scope=session-30.scope
Leader=5648
Audit=30
Type=tty
Class=user
Active=yes
State=closing
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
LockedHint=no
[root@node-137 ~]# loginctl session-status 30
30 - yurq (1000)
Since: Fri 2023-11-24 13:16:25 CST; 1h 30min ago
Leader: 5648
Remote: 192.168.17.1
Service: sshd; type tty; class user
State: closing
Unit: session-30.scope
└─5739 podman
Nov 24 13:16:25 node-137 sshd[5648]: pam_unix(sshd:session): session opened for user yurq by (uid=0)
Nov 24 13:16:25 node-137 systemd[1]: Started Session 30 of user yurq.
Nov 24 14:41:58 node-137 sshd[5648]: pam_unix(sshd:session): session closed for user yurq
[root@node-137 ~]# loginctl terminate-session 30
[root@node-137 ~]# loginctl
SESSION UID USER SEAT
42 0 root
43 0 root
2 sessions listed.
[root@node-137 ~]# loginctl kill-session 46
如果指定SIGSTOP,则session没有退出,但用户的终端会停止响应
[root@node-137 ~]# loginctl kill-session 45 --signal=SIGSTOP
man参考:
-s, --signal=
When used with kill-session or kill-user, choose which signal to send to selected processes.
Must be one of the well known signal specifiers, such as SIGTERM,
SIGINT or SIGSTOP. If omitted, defaults to SIGTERM.
[root@node-137 ~]# tty
/dev/pts/0
[root@node-137 ~]# loginctl session-status 42
42 - root (0)
Since: Fri 2023-11-24 14:42:29 CST; 14min ago
Leader: 6526 (sshd)
Remote: 192.168.17.1
Service: sshd; type tty; class user
State: active
Unit: session-42.scope
├─6526 sshd: root@pts/0
├─6530 -bash
├─6765 loginctl session-status 42
└─6766 less
Nov 24 14:42:29 node-137 sshd[6526]: pam_unix(sshd:session): session opened for user root by (uid=0)
Nov 24 14:42:29 node-137 systemd[1]: Started Session 42 of user root.
[root@node-137 ~]# loginctl list-users
UID USER
0 root
1000 yurq
2 users listed.
[root@node-137 ~]# loginctl show-user yurq
UID=1000
GID=1000
Name=yurq
Timestamp=Fri 2023-11-24 13:16:25 CST
TimestampMonotonic=12800338419
RuntimePath=/run/user/1000
Slice=user-1000.slice
Display=45
State=active
Sessions=45
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
Linger=yes
[root@node-137 ~]# loginctl user-status yurq
yurq (1000)
Since: Fri 2023-11-24 13:16:25 CST; 1h 43min ago
State: active
Sessions: *45
Unit: user-1000.slice
└─session-45.scope
├─6641 sshd: yurq [priv]
├─6644 sshd: yurq@pts/1
└─6646 -bash
Nov 24 13:16:25 node-137 sshd[5648]: pam_unix(sshd:session): session opened for user yurq by (uid=0)
Nov 24 13:16:25 node-137 systemd[1]: Created slice User Slice of yurq.
...
Nov 24 14:52:37 node-137 sshd[6643]: pam_unix(sshd:session): session closed for user yurq
loginctl terminate-user yurq
[webop@blog ~]$ loginctl kill-user webop --signal=SIGSTOP
loginctl enable-linger [USER…], disable-linger [USER…]
启用/禁止用户逗留(相当于保持登录状态)。 如果指定了用户名或UID, 那么系统将会在启动时自动为这些用户派生出用户管理器, 并且在用户登出后继续保持运行。 这样就可以允许未登录的用户在后台运行持续时间很长的服务。 如果没有指定任何参数, 那么将作用于当前调用者的用户。