08.25.07

How to fix SSH Timeouts

Posted in Ubuntu, Linux at 2:00 am by Michael

If you’re like me you use ssh quite a bit throughout your day. I have had problems with ssh timing out on me after just 5 or 10 minutes for a long time now. I recently got fed up with it and decided to find a solution. It turns out that the problem wasn’t really ssh or even the shell. The problem was router NAT tables. You see most routers will close an idle connection that is being translated to save space in the tables. Normally I would say this is great, but for ssh, where sometimes I will leave it logged in yet idle, this was frustrating. I did some searching and found an option that made this problem go away.

All you have to do is simply edit the file /etc/ssh/sshd_config and add 1 line:

ClientAliveInterval 300

That’s it. Add that line to the bottom of your sshd_config file, save it, and then restart ssh. This option will cause sshd to send a keepalive across an idle connection every 5 minutes. This prevents the router from timing out the NAT connection, and you can happily idle as long as you need to. This option should be used with care, it’s never a good idea to leave a login session open on a computer that is not secure. As long as you aren’t lazy and leave your computer unattended where someone could use your session, you should be safe. Never, ever, ever leave yourself logged in as root on a server for longer than absolutely necessary. This is just plain common sense, but I can’t stress it enough. I have seen too many “pranks” played on people who did this out of pure laziness. Whenever you are using root, do what you have to do and exit the shell. If you want to stay idle, do it as an unprivileged user.

Having ssh timeout when you are not quite done working but still idle for whatever reason can be really annoying. This small fix should make things much more bearable for people that work like I do. I’m glad I finally took the time to research this issue, and I share it here so that others can learn from my discoveries. Enjoy.