Posted in SSH

Keeping SSH connections alive behind some NAT routers

April 27, 2008 - 2 comments

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 :

http://www.openssh.com/faq.html

Comments

marco

September 4, 2008 - 10:03

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

Sébastien Wains

September 10, 2008 - 20:57

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.

Before submitting, some rules :
- Is your comment related to the article ?
- You're having a problem ? Have you checked Google, other howtos, docs, manpages ?
- You're still having the problem ? Have you raised log verbosity, checked traces, ran tcpdump ?
- Have you checked your configuratoin for typo ?
Unless your comment is providing additional info or respect the rules above, DON'T comment.
If you don't understand what you are doing, I urge you to read the documentation, I'm not your free Level 1 helpdesk guy.