Posted in SSH
Keeping SSH connections alive behind some NAT routers
SSH connections made from behind my Linksys WAG54G NAT gateway like to die after idling for something like 5 minutes.
The fix :
Add in /home/USER/.ssh/config
Host *
ServerAliveInterval 60
ServerAliveCountMax 60
The SSH client will send a packet every 60 seconds in order to keep the connection alive.
The second option means that after 60 keepalive packets sent, it will stop trying to keep the connection alive, and the connection will eventually die.
See what OpenSSH FAQ has to say about it :
2.12 – My ssh connection freezes or drops out after N minutes of inactivity.
This is usually the result of a packet filter or NAT device timing out your TCP connection due to inactivity. You can enable ClientAliveInterval in the server’s sshd_config, or enable ServerAliveInterval in the client’s ssh_config (the latter is available in OpenSSH 3.8 and newer).
Enabling either option and setting the interval for less than the time it takes to time out your session will ensure that the connection is kept “fresh” in the device’s connection table.
Link :
Comments
Sébastien Wains
Hi,
It should, if not, try using the ClientAliveInterval option on the server side ?
Regards
Seb
Leave Comment
Please consider visiting the partners below if you enjoyed this article :If this post saved you time and money, please consider checking my Amazon wishlist.







marco
Dear Sebastien,
I have notice the following:
from the same computer I connect to 2 different remote hosts via ssh.
One closes me out after a while due to inactivity, the other does not.
So it seems that the difference is in the routers of the hosts I connect to. Does this tip you give solve this problem?
thanks a best regards
Marco