2

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?

2 Answers 2

5

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
2
  • If you want to forcefully log off a user you might also run killall -9 -u $USER.
    – bot47
    Jul 30, 2013 at 11:57
  • 3
    ...which might be a really really bad idea if you do it for root...
    – bot47
    Jul 30, 2013 at 11:58
3

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .