As someone who has been working with servers for over a decade and a half, I feel embarrassed to admit that I only recently stumbled upon this setting. Throughout my years of connecting to remote servers over the internet, I’ve encountered instances where connections freeze or break after a period of inactivity. Sometimes it happens more frequently, while other times it never occurs at all. When I was in the US connecting to Amazon servers, there were occasions when the connection remained stable for days on end. This led me to believe that the issue might be related to the internet or the complexity of the network routing required to reach the server.

However, I have finally discovered a solution—a setting that can be applied to your local machine: ServerAliveInterval. This setting sends a ping to the server every x seconds to maintain the connection. I set mine to 30 seconds, and since then, I have been able to keep my connections alive even during periods of 2 days of inactivity 🎉.

To implement this configuration, you can edit your local/user SSH configuration file located at ~/.ssh/config and add the following line:

ServerAliveInterval 30

Alternatively, you can apply this configuration globally to your system by editing /etc/ssh/ssh_config and adding the same line at the end of the file.

Discovering this solution has been one of the most significant productivity boosts I’ve experienced recently, and I wanted to share it through this blog post.