Configuring SSH session timeout

To configure the timeout for SSH sessions, let’s see where the SSH server configuration file is located:

sudo find / -name sshd_config

Open it in any text editor, for example nano:

sudo nano /etc/ssh/sshd_config

And we specify the timeout interval in seconds, for example 300 (this is 5 minutes), through which sshd requests a response from the client, the default value is 0, that is, requests are not sent to the client.

ClientAliveInterval 300

To terminate inactive connections, specify ClientAliveCountMax, which specifies the number of ClientAliveInterval without an answer, through which the connection will be terminated (the default value is 3):

ClientAliveCountMax 36

That is, 300 seconds * 36 = 10800 seconds = 180 minutes = 3 hours.

Restart the ssh server to apply the changes:

sudo /etc/init.d/ssh restart

The method specified above terminates inactive connections and will not terminate active idle connections, so you can also specify the session timeout in the /etc/bash.bashrc file.

Let’s open it:

nano /etc/bash.bashrc

And add to the very end:

TMOUT=10800
readonly TMOUT
export TMOUT

In this case, after the specified time, idle SSH and local sessions will be terminated.

You can see the number of active sessions in their idle time (IDLE):

w

See also my articles:
How to disconnect SSH user
Installing and Configuring SSH

Leave a comment

Leave a Reply