How to Kill Inactive or Idle SSH sessions in Linux

Suppose you are working on a remote Linux system via ssh, and for some reason your session is disconnected, let’s say due to internet connectivity or a power outage, or your local PC is restarted, etc,.

You may or may not log in to the server again for work, but you have left your previous ssh session open (without logging-out).

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 the inactive or idle or unresponsive hung ssh session’s with the help of ‘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(s) in Linux.

The following articles will help you explore the SSH:

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 our example scenario below, 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              LOGIN@   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 the SSH session’s parent process ID (PID)

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

You should 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”. And the other inactive session is sshd (10132).

3) How to Kill Inactive SSH session(s)

Till step (2) we got all the information about the inactive session(s). Now, I’m going to kill the inactive session using the kill command. Be sure to replace the idle ssh session PID with yours while executing the below command:

# 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. And I can see that in my case 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              LOGIN@   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 found this article helpful, please do share with your friends and spread the knowledge. Please feel free to comment below if you have any queries/concerns. We will get back to you as soon as we can. Happy learning!

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

2 Comments 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 *