How to Kill Inactive or Idle SSH sessions in Linux

Lets imagine that you are working on a remote Linux system via ssh, for some reason your session is disconnected, such as an internet problem or power outage or your local PC is restarted, etc,.

You may or mayn’t log in to the server again for work, but you have left the previous ssh session without logout.

If so, how to terminate them? To do so, you need to find the process id (PID) of the idle ssh session.

You can easily identify inactive or idle or unresponsive hung ssh session with the help of the ‘w command’.

Once you know the session information using the w command, use the pstree command to find the process id (PID) of the idle ssh session.

In this guide, we will show you how find and terminate inactive SSH session in Linux.

The following articles may help you to learn more about SSH related stuff.

1) Identifying Inactive SSH sessions

Verify how many users are currently logged in by logging into the system and issuing the ‘w’ command. If you find your own session, isolate the idle ssh session information.

In my cause, two users are currently logged in, one is my new ssh session, which is currently running the w command and the other is my inactive session.

# w

 10:36:39 up 26 days, 20:29,  2 users,  load average: 0.00, 0.02, 0.00
USER     TTY      FROM              [email protected]   IDLE   JCPU   PCPU WHAT
root     pts/0    10:34   28.00s  0.00s  0.00s -bash
root     pts/2    10:36    0.00s  0.00s  0.00s w

2) Finding SSH session parent process ID (PID)

In order to kill the idle ssh session, you need the parent process ID (PPID) of the idle session. To get it, run the pstree command to see a tree map of all processes.

You will also get an output like the one below. But the structure and PIDs of the tree can vary.

# pstree -p | grep sshd
        |            `-sshd(10199)---bash(10208)---pstree(10226)

From the above output, you can see the parent ID of the sshd process and their branches. The parent ID of the sshd process is sshd (2023) and the branches are ‘sshd (10132)’ and ‘sshd (10199)’.

As I said at the beginning of the article, one is my new session sshd (10199) because it shows the command I am currently running pstree, so the other inactive session is sshd (10132).

3) How to Kill Inactive SSH session

We got all the information about the inactive session. Now, I’m going to kill the inactive session using the kill command. Be sure to replace your idle ssh session PID instead ours.

# kill -9 10132

4) How to check if the Idle session was killed?

This can be verified using the w command and the pstree command. The other session was successfully killed as I could only see one session of my own.

# w

 10:40:18 up 26 days, 20:33,  1 user,  load average: 0.11, 0.04, 0.01
USER     TTY      FROM              [email protected]   IDLE   JCPU   PCPU WHAT
root     pts/2    10:36    0.00s  0.00s  0.00s w

Verify using the pstree command.

# pstree -p

Closing Notes

This article explained simple steps to kill or terminate unresponsive hung SSH session in Linux.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

One Comment on “How to Kill Inactive or Idle SSH sessions in Linux”

  1. Good article. One minor comment: better to avoid sending signal -9, in this case (and in most well behaved processes) the default signal will suffice, e.g. just using ‘kill ‘ will work. The -9 should be best used only as a last resort as it does not go gently with the processes ..

Leave a Reply

Your email address will not be published. Required fields are marked *