BY[MAGESH MARUTHAMUTHU][1]·PUBLISHED : JUNE 21, 2017 || LAST UPDATED: JUNE 21, 2017
Let’s imagine that you are working in server through ssh, for some reason your session get disconnected like, internet issue or power failure or your Local PC get rebooted, etc,.
You may or mayn’t login to the server again to work but you have left the previous ssh session with out closing.
How to Kill an inactive ssh sessions ? Identify the Inactive or Idle ssh sessions with help of`w`command, then get the idle session PID by issuing`pstree`command, finally kill the sessions using`kill`command.
Suggested Read :[SSH Related Articles][2]
Suggested Read :[Mosh (Mobile Shell) – Best Alternative for SSH to Connect Remote System][3]
#### How to Identify Inactive or Idle SSH Sessions
Login to the system and check how many user’s are currently logged in by issuing`w`command. If you identified your own session then note the Inactive or Idle ssh sessions to kill.
In my scenario, I can see two users are currently logged in, one is my new ssh session which am currently running`w`command and another one for my idle session.
root pts/2 219.91.219.14 10:36 0.00s 0.00s 0.00s w
```
#### How to Get a SSH Session PID
To kill idle ssh session, we need a parent`PID`of the idle session. To get a parent pid, run the`pstree`command to see a tree diagram of all the processes.
You also get the similar kind of output like below. The pstree command output is much bigger so, i removed many things from the file for better understanding.
From the above output, you can see the tree of sshd processes and branch’s. The main sshd process is`sshd(2023)`and two branch’s,`sshd(10132)`and`sshd(10199)`.
As i told in the beginning of the article, one is my new session`sshd(10199)`because it shows the`pstree`command which is currently running by me, so the idle session is another one`sshd(10132)`.
Suggested Read :[How to Access Secure Shell (SSH) Servers Through Standard Web Browsers][4]
Suggested Read :[PSSH – Execute Commands on Multiple Linux Servers in Parallel][5]
#### How to kill Idle SSH Sessions
We got all the information about idle session. Now, i’m going to kill the idle session, using the`kill`command. Make sure you have replace your PID instead of us.
```
# kill -9 10132
```
#### Recheck whether the idle session get killed or not
Again use`w`command to recheck whether the idle session get killed or not. Yes, it’s not there because i can see only one session which is owned by me.