This page looks best with JavaScript enabled

Killing SSH connections

 ·  🎃 kr0m

Sometimes we accidentally leave SSH sessions open that are accessing resources such as files, disks, or devices. This can be a problem if we need to perform some operation on them, such as unmounting a disk. In this article, we will learn how to kill those conflicting SSH connections.

We check the active connections:

w

 8:54AM  up 1 day, 14:27, 4 users, load averages: 0.53, 0.38, 0.24
USER       TTY      FROM                                     LOGIN@  IDLE WHAT
kr0m       pts/3    X.X.X.X                                  8:50AM     - -su (bash)
kr0m       pts/4    X.X.X.Y                                  8:37AM     - w
kr0m       pts/0    X.X.Y.Y                                  8:50AM     3 -su (bash)
kr0m       pts/2    X.Y.Y.Y                                  7:39AM    32 -su (bash)

In my case, the conflicting connection came from X.X.X.X, we note the TTY: pts/3


Depending on the operating system, the commands will vary slightly:

FreeBSD:

ps dax
63292  -  Is      0:00.02 |-- /usr/sbin/sshd
 6791  -  Is      0:00.02 | |-- sshd: kr0m [priv] (sshd)
 7487  -  I       0:00.08 | | `-- sshd: kr0m@pts/0 (sshd)
 7921  0  Is      0:00.02 | |   `-- -bash (bash)
18773  0  I       0:00.01 | |     `-- su -l
19045  0  I+      0:00.04 | |       `-- -su (bash)
12622  -  Is      0:00.03 | |-- sshd: kr0m [priv] (sshd)
13186  -  I       0:00.02 | | `-- sshd: kr0m@pts/2 (sshd)
13346  2  Is      0:00.02 | |   `-- -bash (bash)
25039  2  I       0:00.01 | |     `-- su -l
25090  2  I+      0:00.03 | |       `-- -su (bash)
33172  -  Is      0:00.03 | |-- sshd: kr0m [priv] (sshd)
34366  -  S       0:00.03 | | `-- sshd: kr0m@pts/3 (sshd)
35112  3  Is      0:00.02 | |   `-- -bash (bash)
48424  3  I       0:00.01 | |     `-- su -l
49057  3  S       0:00.03 | |       `-- -su (bash)
68625  3  R+      0:00.00 | |         `-- ps dax
97911  -  Is      0:00.03 | `-- sshd: kr0m [priv] (sshd)
98719  -  I       0:00.05 |   `-- sshd: kr0m@pts/4 (sshd)
98755  4  Is      0:00.02 |     `-- -bash (bash)
 9245  4  I       0:00.01 |       `-- su -l
 9751  4  I+      0:00.07 |         `-- -su (bash)

Linux:

ps fax
63292  -  Is      0:00.02 |-- /usr/sbin/sshd
 6791  -  Is      0:00.02 | |-- sshd: kr0m [priv] (sshd)
 7487  -  I       0:00.08 | | `-- sshd: kr0m@pts/0 (sshd)
 7921  0  Is      0:00.02 | |   `-- -bash (bash)
18773  0  I       0:00.01 | |     `-- su -l
19045  0  I+      0:00.04 | |       `-- -su (bash)
12622  -  Is      0:00.03 | |-- sshd: kr0m [priv] (sshd)
13186  -  I       0:00.02 | | `-- sshd: kr0m@pts/2 (sshd)
13346  2  Is      0:00.02 | |   `-- -bash (bash)
25039  2  I       0:00.01 | |     `-- su -l
25090  2  I+      0:00.03 | |       `-- -su (bash)
33172  -  Is      0:00.03 | |-- sshd: kr0m [priv] (sshd)
34366  -  S       0:00.03 | | `-- sshd: kr0m@pts/3 (sshd)
35112  3  Is      0:00.02 | |   `-- -bash (bash)
48424  3  I       0:00.01 | |     `-- su -l
49057  3  S       0:00.03 | |       `-- -su (bash)
68625  3  R+      0:00.00 | |         `-- ps fax
97911  -  Is      0:00.03 | `-- sshd: kr0m [priv] (sshd)
98719  -  I       0:00.05 |   `-- sshd: kr0m@pts/4 (sshd)
98755  4  Is      0:00.02 |     `-- -bash (bash)
 9245  4  I       0:00.01 |       `-- su -l
 9751  4  I+      0:00.07 |         `-- -su (bash)

We kill the process associated with the SSH connection:

kill 34366

We can verify that the connection no longer exists:

w

 8:54AM  up 1 day, 14:27, 2 users, load averages: 0.84, 0.46, 0.28
USER       TTY      FROM                                     LOGIN@  IDLE WHAT
kr0m       pts/4    X.X.X.Y                                  8:37AM     - w
kr0m       pts/0    X.X.Y.Y                                  8:50AM     3 -su (bash)
kr0m       pts/2    X.Y.Y.Y                                  7:39AM    32 -su (bash)
If you liked the article, you can treat me to a RedBull here