Posts tagged with: pkill

How to kill a user’s ssh session

Ever have the need to kill an active ssh session?  It’s pretty simple with the pkill command.  The format is as follows –

pkill <ssh session>

Here’s an example:

First use the “w” command (or the “who” command if you prefer) to show all connected users –

root@server [~]# w
 23:00:16 up 4 days, 22:33,  4 users,  load average: 0.43, 0.42, 0.43
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    24.191.207.228   Mon15    2days  0.05s  0.05s -bash
root     pts/1    208.75.121.102   22:47    6:42   0.14s  0.04s mysql
root     pts/2    71.168.151.89    23:00    0.00s  0.01s  0.00s w
root     pts/3    10.1.80.100      21:47   59:05   0.05s  0.05s -bash

Let’s kill the last user on tty pts/3 –

root@server [~]# pkill pts/3
root@server [~]# w
 23:03:25 up 4 days, 22:36,  3 users,  load average: 0.47, 0.45, 0.44
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    24.191.207.228   Mon15    2days  0.05s  0.05s -bash
root     pts/1    208.75.121.102   22:47    9:51   0.15s  0.04s mysql
root     pts/2    71.168.151.89    23:00    0.00s  0.01s  0.00s w

As you can see, we’ve successfully killed the session for pts/3.