Linux踢出当前登录的指定终端

  Linux 下通常使用 pkill 掉终端来踢掉用户。

第一步

查看当前登录的所有终端:

1
2
3
4
5
6
7
8
[root@localhost ~]# w  
00:46:02 up 1 day, 23:21, 4 users, load average: 0.01, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 237月19 20:02 9.72s 9.72s -bash
root pts/0 192.168.31.1 15:43 6:51m 0.06s 0.06s -bash
root pts/1 192.168.31.1 00:43 2.00s 0.03s 0.01s w
root pts/2 192.168.31.1 00:26 8:42 0.03s 0.03s -bash
[root@localhost ~]#

查看当前使用的 TTY 名:

1
2
3
[root@localhost ~]# who am i 
root pts/1 2019-08-14 00:43 (192.168.31.1)
[root@localhost ~]#

第二步

这里想踢掉 pts/2,则可以直接执行:

1
pkill -kill -t pts/2  

再次使用 w 查看该 TTY 是否已经被踢掉,如果没有则可以添加 -9 参数:

1
pkill -9 -t pts/2  

强制踢掉。

重新连接到指定 PTS

ssh 连接退出后,经常出现会话还存在的情况,此时使用 w 命令依旧可以看到存在 pts 未退出。

1
2
3
apt-get install reptyr

reptyr -T [PID] # 即可恢复会话