Keeping SSH Connections Alive: The Ultimate Guide to Uninterrupted Connectivity

SivaPraveen R
4 min readJul 7, 2023
Photo by Aaron Burden on Unsplash

Introduction

In today’s interconnected world, SSH (Secure Shell) has become an indispensable tool for managing remote servers and network devices. However, one common challenge faced by many of us is maintaining stable and uninterrupted SSH connections.

When I recently started using AWS EC2 Instances, My SSH connections are always timed out, which was pretty frustrating by the way. So this blog came out of fixing it.

I will share invaluable insights and practical techniques to help you keep your SSH connections alive effortlessly. Say goodbye to frustrating disconnects and hello to seamless connectivity!

Understanding SSH Connection Timeouts

SSH connections are susceptible to timeouts due to various factors such as network instability, idle periods, or restrictive firewall configurations. When a connection times out, it disrupts your workflow, causes inconvenience, and can even lead to potential data loss. To ensure uninterrupted connectivity, it is crucial to implement effective strategies that keep your SSH sessions active and responsive.

Configuring SSH Keep-Alive

One of the most effective ways to prevent SSH connection timeouts is by enabling the built-in SSH keep-alive mechanism. By configuring the SSH client to send periodic keep-alive messages to the server, you can maintain an active connection and avoid being disconnected due to inactivity.

To enable SSH keep-alive, follow these simple steps:

  1. Open your SSH client configuration file, typically located at ~/.ssh/config (for Linux/macOS) or %userprofile%\.ssh\config (for Windows).
  2. Add the following lines to the configuration file:
Host *
ServerAliveInterval 60

Save the configuration file.

In the above example, the ServerAliveInterval option specifies the time interval (in seconds) between keep-alive messages. Adjust this value according to your requirements. It is recommended to keep the interval reasonably low to maintain responsiveness without overwhelming the server with excessive keep-alive traffic.

Tweaking Server-Side Settings

While configuring the client-side keep-alive is essential, optimizing the server-side settings can also contribute to a more stable SSH connection. By adjusting the server’s idle timeout and TCP keep-alive settings, you can further enhance the reliability and responsiveness of your SSH sessions.

Modifying Idle Timeout

SSH servers often have an idle timeout period, after which they automatically disconnect idle sessions. To prevent premature disconnections, consider modifying the server’s idle timeout setting.

To modify the idle timeout:

  1. Locate the SSH server configuration file, typically located at /etc/ssh/sshd_config.
  2. Open the configuration file using a text editor.
  3. Find the line that specifies the ClientAliveInterval and ClientAliveCountMax options.
  4. Adjust these values to meet your requirements. For example:
ClientAliveInterval 300
ClientAliveCountMax 3

In the above example, the ClientAliveInterval option sets the time interval (in seconds) between server-to-client alive messages, while ClientAliveCountMax defines the number of unanswered messages allowed before the server terminates the connection.

Fine-tuning TCP Keep-Alive

TCP keep-alive is a network-level mechanism that detects inactive or broken connections. By adjusting the TCP keep-alive parameters, you can ensure the SSH server promptly detects and terminates unresponsive connections.

To fine-tune TCP keep-alive:

  1. Access the SSH server configuration file (/etc/ssh/sshd_config).
  2. Locate the TCPKeepAlive option and set it to yes if it is not already enabled.
TCPKeepAlive yes

Save the configuration file and restart the SSH server for the changes to take effect.

Dealing with Network Instability

Even with the best configurations in place, network instability can still cause SSH connection disruptions. However, you can minimize the impact of network issues by utilizing SSH multiplexing.

Introducing SSH Multiplexing

SSH multiplexing allows you to reuse an existing SSH connection for subsequent sessions, eliminating the need to establish new connections for every interaction. By leveraging SSH multiplexing, you can mitigate the effects of network instability and reduce connection setup overhead.

To enable SSH multiplexing, follow these steps:

  1. Open your SSH client configuration file (~/.ssh/config or %userprofile%\.ssh\config).
  2. Add the following lines to the configuration file:
Host *
ControlMaster auto
ControlPath ~/.ssh/master-%r@%h:%p
ControlPersist 5m

Save the configuration file.

In the above example, the ControlMaster option enables SSH connection sharing, while ControlPath specifies the location of the control socket used for multiplexing. Adjust the ControlPersist value to set the maximum time the control connection remains open after the last active session exits.

Conclusion

Maintaining stable and uninterrupted SSH connections is crucial for efficient remote server management. By implementing the strategies outlined in this guide, including configuring SSH keep-alive, tweaking server-side settings, and utilizing SSH multiplexing, you can ensure seamless connectivity and enhance your productivity. Say goodbye to frustrating disconnections and embrace a smoother SSH experience!

--

--