How to kick other root users logged in from different shells?

7,822

Solution 1

The cleanest way of killing a terminal login session is sending a SIGHUP to all processes – the "hangup" signal, which would also be sent upon closing a terminal window or a SSH connection.

pkill -HUP -t pts/1

Using pgrep or pkill is easier than ps|grep|grep|grep|grep|grep|grep.

If you want to script this:

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

Solution 2

Disclaimer: If you kick out a root user, they could be performing an important recovery task or have a long run process running that is important, blah blah, so proceed at your own risk etc.

So, you can get the process ID for all those sessions with

ps aux | grep pts.

An uglier command that produces better output though (for me on Debian) is

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

Now you can sudo kill 1234 where 1234 is the PID of the PTS sessions.

Share:
7,822

Related videos on Youtube

RajSanpui
Author by

RajSanpui

Updated on September 18, 2022

Comments

  • RajSanpui
    RajSanpui over 1 year

    I logged into a machine, and entered:

    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
    

    I wish to kill others except the user logged in from 160.110.5.210. How to do that?

  • bot47
    bot47 almost 11 years
    If you want to forcefully log off a user you might also run killall -9 -u $USER.
  • bot47
    bot47 almost 11 years
    ...which might be a really really bad idea if you do it for root...