Keeping Your SSH Connections Alive: Configuring ServerAliveInterval and ServerAliveCountMax



Maintaining a stable SSH connection is essential when working on remote servers. Disconnections due to inactivity can interrupt workflows and be quite frustrating. Fortunately, you can configure SSH to send periodic keep-alive messages, preventing your connection from timing out.

Step-by-Step Guide

1. Open the SSH Client Configuration File

Use your preferred text editor to modify the SSH configuration file. For example, with vim:

sudo vim /etc/ssh/ssh_config

If OpenSSH is installed via Homebrew, the configuration file might be located at:

/usr/local/etc/ssh/ssh_config

2. Modify the Configuration

Add the following lines after the Host * directive to apply these settings to all SSH connections:

    ServerAliveInterval 240
    ServerAliveCountMax 10
  • ServerAliveInterval: Specifies the interval (in seconds) at which the client sends keep-alive messages to the server. In this case, every 240 seconds.
  • ServerAliveCountMax: Limits the number of keep-alive messages sent without response before the connection is terminated. Here, the client will attempt to send 10 keep-alive messages.

3. Save and Exit

If you’re using vim:

  • Press ESC to enter command mode.
  • Type :wq and press Enter to save and exit.

4. Test the Configuration

To test if the new settings are working:

  • Connect to a remote server using SSH:
    ssh user@your-server
    
  • Ensure the connection remains active during periods of inactivity.

Conclusion

With this configuration, your SSH client will actively send keep-alive signals, reducing the risk of unexpected disconnections. Adjust the values of ServerAliveInterval and ServerAliveCountMax as needed to suit your environment.

Comments

Popular posts from this blog

Installing the Certbot Let’s Encrypt Client for NGINX on Amazon Linux 2

psql: error: connection to server at "localhost" (127.0.0.1), port 5433 failed: ERROR: failed to authenticate with backend using SCRAM DETAIL: valid password not found

Deploy Nuxt.js app using Apache 2