如何踢出从不同 shell 登录的其他 root 用户?

如何踢出从不同 shell 登录的其他 root 用户?

我登录到一台机器并输入:

insite1@POC-Messaging1:/opt/insiteone/log> last -a | grep "logged"
insite1  pts/6        Tue Jul 30 03:59   still logged in    160.110.5.210
root     pts/5        Tue Jul 30 02:28   still logged in    160.110.154.231
root     pts/4        Tue Jul 30 02:26   still logged in    160.110.154.231
root     pts/0        Tue Jul 30 02:18   still logged in    160.110.5.210
root     pts/3        Tue Jul 30 02:13   still logged in    160.110.5.210
root     pts/2        Tue Jul 30 01:00   still logged in    160.110.154.231
root     pts/1        Tue Jul 30 00:47   still logged in    160.110.154.231

我想杀死除从 160.110.5.210 登录的用户之外的其他用户。该怎么做?

答案1

终止终端登录会话的最干净的方法是向所有进程发送 SIGHUP - “挂断”信号,该信号也会在关闭终端窗口或 SSH 连接时发送。

pkill -HUP -t pts/1

使用pgreppkill比 更容易ps|grep|grep|grep|grep|grep|grep

如果你想编写这样的脚本:

for tty in $(who | awk '$1 == "root" {print $2}'); do
    test $tty = ${thistty=$(tty)} || pkill -HUP -t $tty
done

答案2

免责声明:如果您踢出 root 用户,他们可能正在执行重要的恢复任务,或者正在运行重要的长期运行进程等等,因此请自行承担风险等。

因此,你可以使用以下命令获取所有这些会话的进程 ID:

ps aux | grep pts

不过,有一个更丑陋但输出更好的命令(对于我在 Debian 上来说)是

ps aux | grep sshd | grep pts | grep -v grep

现在您可以sudo kill 1234知道 1234 是 PTS 会话的 PID。

相关内容