So, you’ve been disconnected from your server a couple of times, resulting in inactive ssh sessions. You know this because when you use the “w” command, you see something like the following…
22:47:28 up 315 days, 21:09, 2 users, load average: 9.04, 8.17, 7.30 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 ipxxx-xxx-xxx-xxx 23:54 26:40 9.26s 0.86s -bash root pts/1 ipxxx-xxx-xxx-xxx 22:44 0.00s 0.00s 0.00s w
Using pstree to identify idle SSH sessions
There are a few ways to kill idle ssh sessions. Including editing your sshd_config. But here’s an easy after-the-fact method:
Run this command:
the output will look something like this:
├─sshd(3102)─┬─sshd(3649)───bash(3656) │ └─sshd(16680)───bash(16687)───pstree(17073)
Now look for the session’s parent PID from the line without the “pstree” command. The line including “pstree” would be your session! :)
To kill the idle ssh session, type:
Replace 3649 with your idle ssh session PID. Check afterward with the “w” command.
Now to prevent this from happening in the future, lets edit /etc/ssh/sshd_config and add these two config lines:
ClientAliveInterval 600 ClientAliveCountMax 3
Using htop to identify idle SSH sessions
Another method for identifying idle (orphaned ssh sessions) is by using htop. Notice below the inactive ssh session with the ‘top’ command left running. Also, see htop: Quick Guide & Customization, 90 frequently used Linux Commands, and top, atop, others.
Published: December 9th, 2013
Last updated: October 18th, 2021