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:
- Mosh (Mobile Shell) – Best Alternative for SSH to Connect Remote System
- How to Access Secure Shell (SSH) Servers Through Standard Web Browsers
- rtop – A Nifty Tool to Monitor Remote Server Over SSH
- Automatically Disconnect Idle or Inactive SSH Sessions (After Few Minutes Of Inactivity)
- How to Execute Commands on Remote Linux System over 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 220.127.116.11 10:34 28.00s 0.00s 0.00s -bash root pts/2 18.104.22.168 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(2023)-+-sshd(10132)---bash(10136) | `-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
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 22.214.171.124 10:36 0.00s 0.00s 0.00s w
Verify using the pstree command:
# pstree -p |-sshd(2023)---sshd(10199)---bash(10208)---pstree(10431)
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!