SSH timeouts as a result of inactivity can be quite irritating and frustrating. This usually compels you to re-initiate the connection and start all over again.
Thankfully, with a small configuration change now you can easily increase the SSH timeout limit and keep your SSH session alive even after some inactivity. This happens when either the server or the client sends null packets to the other system to keep the session alive.
Increase SSH Connection Timeout
On the server, head over to the /etc/ssh/sshd_config
configuration file.
$ sudo vi /etc/ssh/sshd_config
Scroll and locate the following parameters:
#ClientAliveInterval #ClientAliveCountMax
The ClientAliveInterval
parameter specifies the time in seconds that the server will wait before sending a null packet to the client system to keep the connection alive.
On the other hand, the ClientAliveCountMax
parameter defines the number of client alive messages which are sent without getting any messages from the client. If this limit is reached while the messages are being sent, the sshd daemon will drop the session, effectively terminating the ssh session.
The timeout value is given by the product of the above parameters i.e.
Timeout value = ClientAliveInterval * ClientAliveCountMax
For example, let’s say you have defined your parameters as shown:
ClientAliveInterval 1200 ClientAliveCountMax 3
The Timeout value will be 1200 seconds * 3 = 3600 seconds. This is an equivalent of 1 hour, which implies that your ssh session will remain alive for idle time of 1 hour without dropping.
Alternatively, you can achieve the same result by specifying the ClientAliveInterval
parameter alone.
ClientAliveInterval 3600
Once done, reload the OpenSSH daemon for the changes to come into effect.
$ sudo systemctl reload sshd
Conclusion and Recommendations
Following best security practices, its always recommended to not set the SSH timeout value too high. This is to prevent someone passing by or hijacking your session when you’re away from your desk or PC for an extended period of time. Thanks for visiting us, please share and follow us on social media for latest news and updates.
Credits: Tecmint, Linux