How to Keep Alive SSH Sessions



Many NAT firewalls time out idle sessions after a certain period of time to keep their trunks clean.

There are two options:

The settings below will make the SSH client or server send a null packet to the other side every 300 seconds (5 minutes), and give up if it doesn’t receive any response after 2 tries, at which point the connection is likely to have been discarded anyway.



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

keepalive settings in PuTTY


To enable the keep alive system-wide, edit /etc/ssh/ssh_config

To set the settings for a user only, edit ~/.ssh/config

Insert the following in the appropriate file:

Host *
    ServerAliveInterval 300
    ServerAliveCountMax 2



Add to the file /etc/ssh/sshd_config:

ClientAliveInterval 300
ClientAliveCountMax 2