I recently noticed that ssh connections from my mac laptop to my server would time out and disconnect if I left the connection idle for a long period of time. This could turn out to be really annoying sometimes.

Add the following line to your ssh config file $HOME/.ssh/config:

ServerAliveInterval 180
ServerAliveCountMax 5

That will fix the time out problem and will save your frustration for something else ;)

What this does, essentially is every 180 seconds, the client sends a small keep-alive packet to the server to make it look like the ssh connection is being actively used. The second parameter will terminate the connection from the client side if the server didn’t reply to the specific number of tries. Like in this case the connection will be terminated after 180*5 = 900 sec.

There are two reasons which i am able to find out for the timeout:
1. Firewall/router – Firewalls or routers are configured in the way to kill the idle connections to save the wastage of bandwidth, to reduce its memory footprint, or to improve performance for other clients.
2. Security issue – The could be session hijacking in the idle connection which could give your privileges to someone else. Or if you are away from your computer then someone else could use your computer and get your privileges.

Now i don’t care about none of the above features (the only thing i care about is my sessions), so i enabled these in my ssh config file.

If you enjoyed this post, make sure you subscribe to my RSS feed!!!!

No related content found.