Learn how to prevent SSH connections from hanging in the MacOS terminal.

How to keep SSH connections alive in the MacOS terminal

As a regular Windows user, I tend to have problems with pretty basic stuff in MacOS as I'm not used to work with it daily. In a travel I made recently, I had to work with my Macbook Pro as this was the laptop I choosed for this travel. During this travel I had to connect to a server through SSH and run some scripts. I use Putty in Windows to connect to my servers, however in this case, I decided to use the OpenSSH builtin cli utility in the MacOS terminal. I noticed after a few connections that if I didn't type anything for about 5 minutes, the connection would simply close and I needed to connect again.

As I always do this of opening a SSH connection, I stay afk for a while and then use it again, I had to research how do configure it to work properly and I will share with you how to keep your SSH connections open in MacOS Monterey.

1. Modify your OpenSSH SSH client configuration file

The first things you need to do is to locate the SSH system-wide configuration file of the system. In MacOS is located at /etc/ssh/ssh_config. You can modify it using the following command (for more information about this configuration file, read the man page here):

sudo nano /etc/ssh/ssh_config

Alternatively if you don't want to make the modifications at system-wide level but at current user level, modify instead the ~/.ssh/config file:

sudo nano ~/.ssh/config

In any of this files, you will have to make the modification of the next step to keep connections alive.

2. Keep SSH connections alive

Now all you need to do is to add the ServerAliveInterval parameter to your settings. This adjustment can be done for every SSH connections or for a specific host. Scroll down to the end of the file and add the parameter with a numeric value that corresponds to the interval in seconds in which the client will send a keep alive header and will prevent the connection from dropping. If you want to do this globally (to any host you may connect, the keep alive header will be sent), configure it like this:

Host *
    ServerAliveInterval 30

With this configuration the header will be sent every 30 seconds and it should keep the connection alive. In case that this value is not enough and your connections keep dropping, lower this value to your needs (e.g to 10 seconds).

If you need to keep the connection alive only on certain hosts, you may specify the configuration as well like this:

Host mywebsite.com
    ServerAliveInterval 30

Your configuration file should look like this after making the modification:

MacOS ssh configuration file

Save the changes to the configuration file and reconnect to your servers, the connection should now always be active as long as the terminal process is open (feel free to watch a youtube video and return to your workplace without losing the connection to your server 😳).

Happy coding ❤️!


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors