Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Fix SSH Disconnects After Idle Timeout: 10 Effective Methods

Fix SSH Disconnects After Idle Timeout: 10 Effective Methods

SSH disconnects due to idle timeouts can disrupt workflows, especially for developers, system administrators, and anyone relying on stable remote connections. This list covers 10 practical ways to prevent SSH sessions from dropping after periods of inactivity, helping you maintain persistent, reliable connections on Linux, macOS, or Windows systems.

1. Adjust ClientKeepAlive Settings

The SSH client configuration file (~/.ssh/config) supports the ServerAliveInterval and ServerAliveCountMax options. Setting ServerAliveInterval to a nonzero value (e.g., 60 seconds) instructs the client to send a keepalive message at that interval, preventing idle timeout disconnections. ServerAliveCountMax controls how many unanswered keepalives trigger a disconnect. This method is effective because it keeps the connection active without user interaction.

2. Modify ServerClientAlive Settings

On the SSH server side, you can configure ClientAliveInterval and ClientAliveCountMax in /etc/ssh/sshd_config. These settings send keepalive messages from the server to the client, preventing the server from dropping idle sessions. Adjusting ClientAliveInterval to a low value like 60 seconds and increasing ClientAliveCountMax to 3 or more can significantly reduce unwanted disconnects.

3. Use TCPKeepAlive Option

The SSH client and server both support the TCPKeepAlive option, which enables TCP keepalive packets at the network level. While this helps detect broken connections, it does not prevent idle timeouts caused by firewall or server policies. Setting TCPKeepAlive yes can complement other keepalive settings but is usually insufficient alone to stop SSH disconnects.

4. Enable Client-Side Autossh

autossh is a utility designed to automatically restart SSH sessions and tunnels when connections drop. It monitors the SSH connection and reconnects if it detects a disconnect. Installing and running your SSH sessions through autossh adds resilience, especially for long-running or critical remote tasks that cannot tolerate dropped connections.

5. Use Mosh Instead of SSH

Mosh (Mobile Shell) is a remote terminal application designed to maintain sessions even through IP address changes and temporary network loss. Unlike SSH, Mosh uses UDP and has built-in mechanisms to handle roaming and intermittent connectivity, making it ideal for unstable networks. However, it requires server-side installation and may not support all SSH features.

6. Configure SSH Session Multiplexing

SSH multiplexing allows multiple SSH sessions to share a single TCP connection, reducing overhead and improving performance. While it doesn’t directly prevent disconnects, multiplexing can help you reconnect faster and reuse existing connections. Enable it by setting ControlMaster auto, ControlPath, and ControlPersist in your SSH client config.

7. Adjust Firewall and Router Timeout Settings

Network devices like firewalls and routers often terminate idle connections after a timeout. If you control these devices, increase their TCP session timeout values to accommodate longer SSH idle periods. This prevents intermediate devices from dropping your SSH connection, which is a common cause of disconnects in corporate or home networks.

8. Use Screen or Tmux on Remote Host

Running terminal multiplexers like screen or tmux on the remote server preserves your session state even if the SSH connection drops. While this does not prevent disconnects, it allows you to resume your work without losing progress. It’s a practical workaround that complements keepalive settings for uninterrupted workflows.

9. Send Periodic Commands Using Scripts

You can automate the sending of benign commands (like echo) at intervals to keep the SSH session active. For example, a simple shell script running on the client can send a newline or a harmless command every few minutes. This method mimics user activity and can bypass idle timeout policies that detect inactivity.

10. Increase SSH Timeout Values on the Server

Some SSH servers impose explicit timeout limits on idle sessions using options like LoginGraceTime and IdleTimeout (though the latter is less common). Review and increase these values in sshd_config to allow longer idle periods before disconnecting clients. Remember to restart the SSH service after changes for them to take effect.

How to Choose

Choosing the right method depends on your level of control over the client and server, network environment, and use case. If you have server access, adjusting ClientAliveInterval and ClientAliveCountMax is straightforward and effective. For client-only control, setting ServerAliveInterval helps maintain connections without server changes. When network devices cause disconnects, modifying firewall timeouts or using Mosh can be more reliable. For mission-critical sessions, combining keepalive settings with tools like autossh or tmux ensures resilience. Always test changes in your environment to find the balance between connection persistence and resource usage.

Frequently Asked Questions

Q1: Why does my SSH disconnect after a few minutes of inactivity?
Many servers and intermediate network devices have idle timeout policies that close inactive connections to free resources or enhance security.

Q2: Will enabling ServerAliveInterval increase network traffic?
Yes, it sends small keepalive packets at defined intervals, but the bandwidth impact is minimal and generally negligible.

Q3: Can Mosh replace SSH entirely?
Mosh improves stability for interactive sessions but does not support all SSH features like port forwarding or file transfers, so it may not fully replace SSH.

Q4: Is using autossh secure?
Yes, autossh simply monitors and restarts SSH connections; it inherits SSH’s security model without exposing additional vulnerabilities.

Q5: What is the difference between TCPKeepAlive and ServerAliveInterval?
TCPKeepAlive operates at the TCP layer to detect dead connections, while ServerAliveInterval sends SSH protocol-level keepalives to prevent idle timeouts.

Q6: Do I need to restart SSH after changing sshd_config?
Yes, you must restart or reload the SSH server (e.g., sudo systemctl restart sshd) for configuration changes to take effect.

Final Thoughts

SSH idle disconnects can be frustrating but are usually preventable with proper configuration. Adjusting keepalive settings on the client and server, using utilities like autossh or tmux, or switching to Mosh can dramatically improve session stability. Understanding your network environment and testing different approaches will help you maintain persistent SSH connections tailored to your needs.

Related

Fix SSH Disconnects After Idle Timeout: 10 Effective Methods
Scroll to top