Problem
SSH connections timeout after 3 minutes of idle time. How do I increase this?
Solution
The ServerAliveInterval and ServerAliveCountMax SSH settings can be used to configure keepalives on the client-side. When added to the SSH config file, these settings will cause the client to send server alive messages through the encrypted channel, which may be sent without receiving any messages back from the server. If this threshold is reached while server alive messages are being sent, ssh will disconnect from the server, terminating the session.
Linux/macOS (applies to SSH2 only)
Adding the following to the ~/.ssh/config file will keep an idle connection open for 10 minutes:
host *
ServerAliveInterval 300
ServerAliveCountMax 2
Windows/PuTTY
Use the following steps to set the client to ask the server for a sign of life every 15 seconds, thus keeping the connection open:
* Open the PuTTY general configuration.
* In the left Category list section, select Connection.
* In the field ‘Seconds between keepalives (0 to turn off)', enter 15.
Windows/OpenSSH
* Create an ssh config file: c:\Users\<username>\.ssh\config.txt
* Add the following to the config file (this will keep an idle connection open for 10 minutes):
Host *
ServerAliveInterval 300
ServerAliveCountMax 2
Server-side
To customize the settings on the server side, so that it applies to all client connections, try setting the ClientAliveInterval and ClientAliveCountMax in the sshd_config and restart ssh. The following will keep an idle session alive for 10 minutes. After 10 minutes of idle time the session will be terminated (Note: These settings apply to ssh protocol version 2 only.):
ClientAliveInterval 300
ClientAliveCountMax 2
NOTE: Keep in mind that setting a long idle timeout value associated with a connection could allow an unauthorized user access to another user’s ssh session (for example, user starts an ssh session and walks away from their computer without locking the screen.) Setting a short idle timeout reduces this risk. For long running processes/jobs on a remote machine that will end if an ssh session is terminated, it is recommended to use something like screen or tmux, which allow you to start processes/jobs and reconnect at a later time, rather than leaving an idle session open while these run.