When working over slow or unstable internet connections, SSH sessions can frequently disconnect, causing frustration and lost work. This list covers essential SSH timeout settings and techniques designed to maintain active SSH connections even when network reliability is poor. It’s especially useful for remote developers, system administrators, and anyone relying on SSH over intermittent or high-latency links.
ClientAliveInterval
This is a server-side SSH configuration setting that defines how often (in seconds) the server sends a null packet to the client to check if the connection is still alive. Setting a lower ClientAliveInterval (for example, 30 seconds) helps detect broken connections sooner and keeps the session active by preventing idle timeouts on slow networks. It is configured in /etc/ssh/sshd_config and requires a server restart to take effect.
ClientAliveCountMax
This setting works in tandem with ClientAliveInterval and specifies how many unanswered client alive messages the server will send before disconnecting the session. Increasing ClientAliveCountMax (default is 3) allows the server to tolerate longer delays or packet loss common in slow connections. For example, setting it to 10 means the server waits for up to 5 minutes (if ClientAliveInterval is 30 seconds) before dropping the connection.
ServerAliveInterval
Unlike the previous two which are server-side, ServerAliveInterval is a client-side SSH setting that controls how often the client sends keepalive messages to the server. Setting this to a value like 60 seconds helps maintain the connection by preventing network devices from closing idle sessions. This is configured in the client’s SSH configuration file, typically ~/.ssh/config.
ServerAliveCountMax
Paired with ServerAliveInterval, this client-side setting determines how many unanswered keepalive messages the client will send before terminating the connection. Increasing this value helps accommodate intermittent network delays by allowing more missed responses before disconnecting. For slow links, a value of 10 or higher can reduce premature session drops.
TCPKeepAlive
This option enables TCP-level keepalive packets, which operate below the SSH protocol layer. When set to yes (default), it lets the operating system send periodic TCP keepalive probes to maintain the connection. On slow or flaky connections, enabling TCPKeepAlive can help detect broken links faster, but in some cases, disabling it and relying on SSH-level keepalives might be more effective to avoid false disconnects.
UseDNS
In the server configuration, setting UseDNS no disables reverse DNS lookups on incoming SSH connections. This can reduce connection delays and timeouts caused by slow or failing DNS servers, which is particularly helpful when internet connections are slow or unstable. It speeds up the initial handshake phase, minimizing the chance of premature timeouts.
Compression
Enabling SSH compression with the -C flag or Compression yes in the client config can improve performance over slow connections by reducing the amount of data sent over the network. While not directly a timeout setting, compression helps keep data flowing smoothly, reducing the likelihood of timeouts caused by network congestion or delays.
Adjusting TCP Keepalive Settings at OS Level
Beyond SSH config, tuning TCP keepalive parameters at the operating system level (like tcp_keepalive_time, tcp_keepalive_intvl, and tcp_keepalive_probes on Linux) can influence SSH connection stability. Lowering these values makes TCP detect broken connections faster, helpful for slow or unreliable networks. However, changes require root access and careful testing to avoid unintended side effects.
Using Mosh as an Alternative
While not a direct SSH configuration, Mosh (Mobile Shell) is a remote terminal application designed for intermittent and slow connections. It uses UDP and intelligent state synchronization to maintain sessions despite network drops, roaming, or high latency. For users frequently facing slow internet, Mosh can be a more robust alternative to traditional SSH, reducing the need for complex timeout tweaks.
Increasing SSH Client Timeout Settings
On the client side, increasing the SSH command’s timeout options such as ConnectTimeout (which sets the timeout for establishing a connection) can help when initial handshakes take longer on slow connections. Setting ConnectTimeout to a higher value (e.g., 60 seconds) ensures SSH doesn’t abort prematurely during connection attempts on high-latency networks.
How to Choose
When selecting SSH timeout settings for slow internet, consider whether you control the server, the client, or both. Server-side settings like ClientAliveInterval and ClientAliveCountMax help maintain sessions from the server perspective, while client-side options like ServerAliveInterval and ServerAliveCountMax keep the client connection active. Adjust these values based on your network’s latency and packet loss patterns—lower intervals and higher count max values generally improve tolerance for instability.
Also, decide if TCP keepalives or SSH-level keepalives suit your environment better; some networks respond better to one or the other. Enabling compression can improve throughput on slow links but may add CPU overhead. Finally, if SSH sessions still drop frequently despite tuning, consider alternative tools like Mosh designed for unreliable networks.
Frequently Asked Questions
Q: What is the difference between ClientAliveInterval and ServerAliveInterval?
ClientAliveInterval is a server-side setting where the server sends keepalive messages to the client; ServerAliveInterval is client-side, where the client sends keepalives to the server.
Q: Should I disable TCPKeepAlive if I use SSH keepalives?
It depends on your network. Sometimes disabling TCPKeepAlive reduces false disconnects when SSH keepalives are active, but in other cases, TCPKeepAlive helps detect dead peers faster. Test both configurations.
Q: Can changing SSH timeout settings fix all disconnect issues on slow connections?
Not always. While timeouts can help, underlying network instability or high latency may require additional solutions like VPNs, better hardware, or tools like Mosh.
Q: How do I apply these settings?
Server settings are changed in /etc/ssh/sshd_config and require restarting the SSH daemon. Client settings go in ~/.ssh/config or can be passed as command-line options.
Q: What values are recommended for slow connections?
Common recommendations are setting ClientAliveInterval to 30 seconds, ClientAliveCountMax to 10, ServerAliveInterval to 60 seconds, and ServerAliveCountMax to 10, but adjust based on your specific latency and packet loss.
Q: Does enabling compression increase SSH session stability?
Compression reduces the amount of data sent, which can improve performance on slow links, but it does not directly affect timeout settings or session stability.
Final Thoughts
Optimizing SSH timeout settings is crucial for maintaining stable remote sessions over slow or unreliable internet connections. By carefully adjusting server- and client-side keepalive intervals and counts, enabling compression, and tuning TCP keepalives, you can significantly reduce unwanted disconnects. When traditional SSH settings are insufficient, exploring alternatives like Mosh or improving the underlying network may be necessary. With these techniques, you can create a more resilient SSH environment tailored to challenging connectivity scenarios.
Related
- Costco Bagels vs Local Bakery: Which Should You Choose?
- How to Convert Latitude and Longitude to Decimal Degrees Easily
- How to Freeze Chimichurri Sauce for Long-Term Storage