How to prevent ssh timeout in linux?

2 min read

By sending “null packets” between client and server at certain intervals smaller than the timeout value, we can avoid SSH timeout. It doesn’t matter if packets are sent from the client or server, as long as there is communication happening between them.

Prevent SSH timeout on server side:

Please add the following to your SSH daemon configuration at /etc/ssh/sshd_config on your server to prevent clients from timing out – so they don’t need to change their local SSH configuration:

ClientAliveInterval 120
ClientAliveCountMax 720

This will make the server send “null packets” to the client every 120 seconds and not disconnect it until the client is inactive for 720 intervals (120 seconds * 720 = 86400 seconds = 24 hours).

Check the values via command line …

[root@bahcesi]# sshd -T | grep -i client
clientaliveinterval 120
clientalivecountmax 720

Prevent SSH timeout on client side:

on many commodity firewalls, connections are terminated only after 300 seconds. To avoid your SSH session becoming unresponsive after e.g. 5 minutes, do the following:

Linux client:

If we are using Linux, you can edit your local SSH configuration file at ~/.ssh/ssh_config (or) /etc/ssh/ssh_config and add the following line:

ServerAliveInterval 120

This will send “null packets” every 120 seconds on your SSH connection to keep it alive.

Windows client:

Putty - On Windows (PuTTY)

In your session properties, go to Connection and under Send null packets to keep session active, set Seconds between keepalives (0 to turn off) to e.g. 300 (5 minutes).

Mobaxterm - You can check the “Enable SSH keepalive” box under “Settings” –> “Configuration” –> “SSH” tab.

Share this post

Found this helpful? Share it with others.

comments powered by Disqus