How to Kill Idle SSH Sessions

Therefore, you have been disconnected from your server twice, resulting in inactive ssh sessions. You know it because when youwoo“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 inactive SSH sessions

There are a few ways to end inactive ssh sessions. Involves editing your sshd_config. But here’s an easy after-the-fact method:

Run this command:

pstree -p

The output will look something like this:

├─sshd(3102)─┬─sshd(3649)───bash(3656)
│            └─sshd(16680)───bash(16687)───pstree(17073)

Now find the parent PID of the session from the line Without it “Pastry” command. The line including “pstree” will be your session! ,

To end an inactive ssh session, type:

kill 3649

Replace 3649 with your inactive ssh session PID. Afterwards check with “w” command.

Now to prevent this from happening in the future, let’s edit /etc/ssh/sshd_config and add these two configuration lines:

ClientAliveInterval 600
ClientAliveCountMax 3

Using htop to identify inactive SSH sessions

Another way to identify idle (orphaned ssh sessions) is to use htop. Notice below idle ssh session is running with ‘top’ command. Also, check out htop: Quick Guide and Customization, 90 Frequently Used Linux Commands, and top, above, others.

Published: 9 December 2013
Last Updated: October 18, 2021

Leave a Comment