How to Prevent SSH Inactivity Timeouts: A Complete Guide

SSH sessions timing out due to inactivity can be frustrating, especially when you’re running long processes or working intermittently. Fortunately, you can configure both the SSH client and SSH server to ensure your connections remain active without unwanted disconnections.

In this guide, I’ll show you how to adjust the SSH/SSHD configurations on both the client and server sides to prevent inactivity timeouts.

Client-Side Configuration

Per-User Configuration

To prevent timeouts for your user, you should modify your personal SSH configuration file (~/.ssh/config).

  1. Open or create the configuration file:nano ~/.ssh/config
  2. Add the following configuration:
Host *
ServerAliveInterval 60
ServerAliveCountMax 10000

What do these settings do?

  • ServerAliveInterval 60: This sends a keep-alive message to the server every 60 seconds.
  • ServerAliveCountMax 10000: This ensures the client keeps the connection alive (almost) indefinitely, without considering any limits on failed keep-alive messages.

By adding this configuration, your SSH session will not terminate due to inactivity, keeping the connection alive with periodic messages.

Global Client-Side Configuration

If you prefer to apply this configuration globally (for all users on the client machine), you need to edit the global SSH configuration file:

  1. Open the global SSH config file:sudo nano /etc/ssh/ssh_config
  2. Add the same configuration under the Host * section:
Host *
ServerAliveInterval 60
ServerAliveCountMax 10000

This will ensure all users on the client machine benefit from the inactivity timeout prevention.

Server-Side Configuration

To prevent the SSH server from disconnecting clients after inactivity, you’ll need to adjust the SSH daemon settings.

  1. Open the SSH daemon configuration file:sudo nano /etc/ssh/sshd_config
  2. Add or modify these lines:

What do these settings do?

  • ClientAliveInterval 0: Disables the server from sending any keep-alive messages to the client.
  • ClientAliveCountMax 0: Prevents the server from disconnecting inactive clients based on any timeout limits.

This ensures that the server will not automatically disconnect clients, even if they’ve been idle for a long time.

Restart SSHD

After making changes to the server’s SSH configuration, you need to restart the SSH daemon for the changes to take effect:

sudo systemctl restart sshd

Conclusion

With these configurations in place, your SSH sessions should remain active, regardless of any inactivity periods. The server won’t force disconnects, and the client will send periodic keep-alive messages to ensure the connection remains open.

By understanding both client and server configurations, you can take control of your SSH sessions and prevent annoying disconnections due to timeouts.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.