How to Kill or Terminate an Inactive or Idle SSH Session on Linux
Lets imagine that you are working on a remote Linux machine 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 with out logout.
If so, how to kill them?
To do so, you need to find the process id (PID) of the idle ssh session.
You can easily identify inactive 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.
These are the steps you need to take to achieve this.
The following articles may help you to learn more about SSH related stuff.
- SSH Related Articles
- 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) How to Identify Idle SSH Session on Linux
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 situation, 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 18.104.22.168 10:34 28.00s 0.00s 0.00s -bash root pts/2 22.214.171.124 10:36 0.00s 0.00s 0.00s w
2) How to Get an 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
As I said at the beginning of the article, one is my new session
shd (10199) because it shows the command I am currently running
pstree, so the other inactive session is
3) How to Kill an 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 PID instead us.
# kill -9 10132
4) How to Check If the Idle Session is Killed or not?
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 126.96.36.199 10:36 0.00s 0.00s 0.00s w
Verify using the pstree command.
# pstree -p |-sshd(2023)---sshd(10199)---bash(10208)---pstree(10431)