一,loginctl的用途:
控制 systemd 登录管理器
管理当前登录的用户和session
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,查看loginctl所属的包及安装
1,查看所属的rpm包
[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
2,如果找不到loginctl命令,可以用yum安装
[root@blog ~]# yum install systemd
三,查看loginctl的版本和帮助
1,查看版本
[root@blog ~]# loginctl --version systemd 239 +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy
2,查看帮助
[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
五,loginctl 使用例子之一:session操作
1,列出本机所有的session
# 只执行loginctl 和loginctl list-sessions效果一样,
# list-sessions是 loginctl的默认操作
[root@blog ~]# loginctl list-sessions SESSION UID USER SEAT TTY 974 1000 webop 975 1000 webop 2 sessions listed.
说明:可以看到:同一个用户可以开启多个session
2,显示一个session的详细信息
[root@blog ~]# loginctl show-session 974 Id=974 User=1000 Name=webop Timestamp=Thu 2020-04-09 14:14:21 CST TimestampMonotonic=7791035206379 VTNr=0 Remote=yes RemoteHost=43.243.12.116 Service=sshd Scope=session-974.scope Leader=31899 Audit=974 Type=tty Class=user Active=yes State=active IdleHint=no IdleSinceHint=0 IdleSinceHintMonotonic=0 LockedHint=no
3,显示一个session的状态:
#session-status: session状态,指定一个session id,
#可以看到这个session开启的进程等信息
[root@blog ~]# loginctl session-status 975
4,结束指定的session
即:杀死指定会话的所有进程、释放所有与此会话相关的资源
先列出所有的session
[root@blog ~]# loginctl list-sessions SESSION UID USER SEAT TTY 974 1000 webop 975 1000 webop
上面可以看到975这个session
#terminate-session 结束指定的session
[root@blog ~]# loginctl terminate-session 975
再次列出当前的session
[root@blog ~]# loginctl list-sessions SESSION UID USER SEAT TTY 974 1000 webop
已经看不到975了
此时我们从975这个session的终端可以看到:
[SSH] INFO: DISCONNECT
5,kill-session 杀死session
与terminate session基本相同,但可以指定发送的信号
如果不加参数,则signal为SIGTERM
[root@blog ~]# loginctl kill-session 976
976终端上的提示
[SSH] INFO: DISCONNECT
指定的session已经结束
如果指定SIGSTOP,则session没有退出,但用户的终端会停止响应
[root@blog ~]# loginctl kill-session 980 --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.
6,如何查看自己的tty?
[root@blog ~]$ tty /dev/pts/0
配合session-status即可找到自己的session id
[webop@blog ~]$ loginctl session-status 983 983 - webop (1000) Since: Thu 2020-04-09 16:22:44 CST; 1min 53s ago Leader: 821 (sshd) Remote: 43.243.12.116 Service: sshd; type tty; class user State: active Unit: session-983.scope ├─821 sshd: webop [priv] ├─823 sshd: webop@pts/0 ├─824 -bash ├─870 loginctl session-status 983 └─871 less Apr 09 16:22:44 blog systemd[1]: Started Session 983 of user webop.
可以看到自己的session id是983
六,loginctl 使用例子之二:user操作
1,列出所有用户
[root@blog ~]# loginctl list-users UID USER 1000 webop 1 users listed.
2,查看当前登录用户的详细信息:
[root@blog ~]# loginctl show-user webop UID=1000 GID=1000 Name=webop Timestamp=Thu 2020-04-09 13:00:09 CST TimestampMonotonic=7786582389261 RuntimePath=/run/user/1000 Service=user@1000.service Slice=user-1000.slice Display=974 State=active Sessions=983 974 IdleHint=no IdleSinceHint=0 IdleSinceHintMonotonic=0 Linger=no
3,查看登录用户的状态
[root@blog ~]# loginctl user-status webop webop (1000) Since: Thu 2020-04-09 13:00:09 CST; 3h 29min ago State: active Sessions: 983 *974 Linger: no Unit: user-1000.slice ├─session-974.scope │ ├─ 902 loginctl user-status webop │ ├─ 903 less │ ├─31899 sshd: webop [priv] │ ├─31902 sshd: webop@pts/1 │ ├─31904 -bash │ ├─31932 sudo -i │ └─31933 -bash ├─session-983.scope │ ├─821 sshd: webop [priv] │ ├─823 sshd: webop@pts/0 │ └─824 -bash └─user@1000.service └─init.scope ├─31746 /usr/lib/systemd/systemd --user └─31749 (sd-pam) Apr 09 14:14:29 blog sudo[31932]: pam_systemd(sudo-i:session): Cannot create session: Already running in a session or user slice Apr 09 14:14:29 blog sudo[31932]: pam_unix(sudo-i:session): session opened for user root by webop(uid=0) Apr 09 15:23:43 blog sshd[31740]: pam_unix(sshd:session): session closed for user webop Apr 09 15:37:43 blog sshd[32339]: pam_unix(sshd:session): session closed for user webop ...
4,结束指定用户的所有session
[root@blog ~]# loginctl terminate-user webop
5,停止用户的终端
[webop@blog ~]$ loginctl kill-user webop --signal=SIGSTOP
七,查看centos版本
[root@blog ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)