How to Stop Ubuntu from Automatically Disconnecting Inactive SSH Sessions (ClientAliveInterval)

When you connect to an Ubuntu server via SSH (Secure Shell), the connection is maintained by the SSH daemon (sshd). If you leave your terminal window open but do not type any commands for an extended period, the server’s firewall, load balancers, or the SSH daemon itself may automatically terminate the connection to conserve resources, resulting in a frustrating “Broken pipe” or “Connection reset by peer” error.

If you are monitoring a long-running log file or stepping away for lunch, returning to a dead terminal session is incredibly annoying, as you lose your command history and context.

You can configure the SSH server to automatically send hidden “keep-alive” packets to the client, tricking the network into keeping the connection open indefinitely.

How to Enable ClientAliveInterval in SSHD

You must edit the main SSH daemon configuration file on the Ubuntu server.

  1. Connect to your Ubuntu server (or open a local terminal).
  2. Open the SSH daemon configuration file in a text editor with root privileges:

sudo nano /etc/ssh/sshd_config

  1. Scroll down the file until you find the following two lines (they may be commented out with a #):

#ClientAliveInterval 0
#ClientAliveCountMax 3

  1. Remove the # to uncomment both lines.
  2. Change the ClientAliveInterval to a reasonable number of seconds, such as 60. This tells the server to send a hidden ping to your client every 60 seconds to keep the connection alive.
  3. Change the ClientAliveCountMax to a higher number, such as 120. (This means the server will try 120 times before giving up if you truly disconnect).

ClientAliveInterval 60
ClientAliveCountMax 120

  1. Save the file in nano (Ctrl+O, Enter) and exit (Ctrl+X).
  2. Restart the SSH service to apply the changes:

sudo systemctl restart sshd

From now on, your Ubuntu server will actively maintain idle SSH connections, ensuring your terminal is exactly how you left it when you return to your desk.

Get the best tech tips delivered straight to your inbox.

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