Linux SSH Sessions

When you have been disconnected from the server several times, causing the ssh session to be inactive. You know this because when you use the " w" command, you will see the following ……

1
2
3
4
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

In this tutorial, we will explain how to find an idle ssh process and kill it.

Identifying idle ssh sessions using pstree

There are several ways to terminate an idle ssh session. Including editing your sshd_config, but here is a simple after-the-fact method.

Run the pstree command.

1
pstree -p

The output will look like this.

1
2
3
├─sshd(3102)─┬─sshd( 3649 )───bash(3656)
└─sshd(16680)───bash(16687)───pstree(17073)

To terminate an idle ssh session, type.

1
kill 3649

Replace 3649 with your idle ssh session process ID and check it with the “w” command.

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

1
2
ClientAliveInterval 600
ClientAliveCountMax 3

Identifying idle ssh sessions with htop

Another way to identify idle (isolated ssh sessions) is to use htop. we can see that the “top” command is still running below the inactive ssh sessions.

top