How to Keep a Linux SSH Session Alive to Prevent Disconnections

When managing a Linux server remotely, dropped connections are a common and frustrating reality. If you are halfway through a large file transfer, a complex database query, or a lengthy compilation, a dropped SSH connection will terminate your session and kill the processes you were running.

This usually happens because intermediate routers, firewalls, or load balancers automatically terminate idle TCP connections to save resources. To prevent this, you can configure your SSH client or server to send “keep-alive” packets—invisible signals that tell the network the connection is still active.

Method 1: Configure Your Local SSH Client (Recommended)

The safest and most common way to keep a session alive is to configure your local machine (the client) to send a periodic ping to the server. This requires no administrative privileges on the remote server.

You need to edit your local user’s SSH configuration file.

  1. Open your local terminal.
  2. Open or create the SSH config file using a text editor like nano:
    nano ~/.ssh/config
  3. Add the following two lines to the file:
    Host *
        ServerAliveInterval 60
        ServerAliveCountMax 3
  4. Save and exit the file (in nano, press CTRL + O, Enter, then CTRL + X).

What do these settings mean?

  • Host *: Applies these settings to all SSH connections you make. (You can replace the * with a specific IP address if you only want to apply this to one server).
  • ServerAliveInterval 60: Tells your SSH client to send a null packet to the server every 60 seconds of inactivity. This is usually enough to trick firewalls into keeping the connection open.
  • ServerAliveCountMax 3: If the server does not respond to 3 consecutive keep-alive packets, the client will assume the connection is genuinely dead and disconnect, rather than hanging indefinitely.

Method 2: One-Time Keep-Alive (Command Line Flag)

If you don’t want to edit your configuration file and only need to keep a single, specific session alive, you can pass the keep-alive interval directly into your SSH command using the -o flag.

Run your SSH command like this:

ssh -o ServerAliveInterval=60 username@server_ip

This achieves the exact same result as the config file, but only for the duration of that specific command.

Method 3: Configure the Remote Server (For Sysadmins)

If you are the server administrator and want to ensure that all clients connecting to your server stay alive (perhaps because a strict corporate firewall sits in front of the server), you can configure the SSH daemon itself to send pings down to the clients.

  1. Log into your Linux server and open the SSH daemon configuration file with sudo privileges:
    sudo nano /etc/ssh/sshd_config
  2. Scroll down to find the `ClientAliveInterval` and `ClientAliveCountMax` settings. (If they are commented out with a `#`, delete the hash symbol). Configure them like this:
    ClientAliveInterval 60
    ClientAliveCountMax 3
  3. Save and exit the file.
  4. Restart the SSH service for the changes to take effect:
    sudo systemctl restart ssh

    (Note: On CentOS/RHEL systems, the service name is `sshd`, so use `sudo systemctl restart sshd`).

Now, your server will send a pulse to idle clients every 60 seconds, ensuring that no active administrator gets disconnected due to an overzealous firewall.

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.