How to disable SSH timeout

SSH clients will automatically be disconnected from the server and prompt the below message after being idle or inactive for a while.

Read from remote host Connection reset by peer
Connection to closed.

This is due to the SSH servers' configuration (often by default) to avoid hanging sessions and free up resources. These are the related options in the SSH server configuration;

  • TCPKeepAlive
    • Whether or not to send TCP “alive” message to the connecting clients to test for connection issues. Defaults to yes.
  • ClientAliveInterval
    • A timeout interval to send encrypted alive message to clients if no data has been received from connection. Defaults to 0 (not sending any message).
  • ClientAliveCountMax
    • Number of times to send the encrypted alive message before disconnecting clients if no response are received. Defaults to 3.

If you have administrative access to the SSH servers, you can change the options so that you will not easily be disconnected. Edit the SSH server configuration file (normally in /etc/ssh/sshd_config for Unix based operating systems) and set the related options as the followings (uncomment or add if necessary);

TCPKeepAlive no 
ClientAliveInterval 30
ClientAliveCountMax 100

What it basically means is that the server will not send the TCP alive packet to check if the client's connection is working, yet will still send the encrypted alive message every 30 seconds but will only disconnect after 24 hours of inactivity. Be sure to restart the SSH service after the reconfiguration. The following command would work for most Unix based servers;

sudo service sshd restart

If you don't have administrative access to the server, you can configure the SSH client to send the alive message to the server instead. The key here is the ServerAliveInterval option for the SSH client.

You can do this by updating /etc/ssh/ssh_config (applying the setting to every user in the system) or in ~/.ssh/config (single user). Set the following option to have the client send the alive packet every 30 seconds to the server;

ServerAliveInterval 30

The other alternative is to manually set the ServerAliveInterval option every time you're connecting to a server by using the -o ServerAliveInterval=<time-in-second> prefix as the following example;

ssh -o ServerAliveInterval=30
comments powered by Disqus