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.
- 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 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 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 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(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, so the other inactive session is
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 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 have any questions or feedback, feel free to comment below.