What do options `ServerAliveInterval` and `ClientAliveInterval` in sshd_config do exactly?
ServerAliveInterval: number of seconds that the client will wait before sending a null packet to the server (to keep the connection alive).
ClientAliveInterval: number of seconds that the server will wait before sending a null packet to the client (to keep the connection alive).
Setting a value of 0 (the default) will disable these features so your connection could drop if it is idle for too long.
ServerAliveInterval seems to be the most common strategy to keep a connection alive. To prevent the broken pipe problem, here is the ssh config I use in my .ssh/config file:
Host myhostshortcut
HostName myhost.com
User barthelemy
ServerAliveInterval 60
ServerAliveCountMax 10
The above setting will work in the following way,
- The client will wait idle for 60 seconds (ServerAliveInterval time) and, send a "no-op null packet" to the server and expect a response. If no response comes, then it will keep trying the above process till 10 (ServerAliveCountMax) times (600 seconds). If the server still doesn't respond, then the client disconnects the ssh connection.
ClientAliveCountMax on the server side might also help. This is the limit of how long a client are allowed to stay unresponsive before being disconnected. The default value is 3, as in three ClientAliveInterval.
This is explained in sshd_config
manual (man sshd_config
):
ClientAliveInterval
Sets a timeout interval in seconds after which if no data has been received from the client, sshd will send a message through the encrypted channel to request a response from the client. The default is 0, indicating that these messages will not be sent to the client. This option applies to protocol version 2 only.
ClientAliveCountMax
The default value is 3. If
ClientAliveInterval
(see below) is set to 15, andClientAliveCountMax
is left at the default, unresponsive SSH clients will be disconnected after approximately 45 seconds. This option applies to protocol version 2 only.
For the client options, see the explanation in man ssh_config
:
ServerAliveInterval
Sets a timeout interval in seconds after which if no data has been received from the server,
ssh
will send a message through the encrypted channel to request a response from the server. The default is 0, indicating that these messages will not be sent to the server. This option applies to protocol version 2 only.
ServerAliveCountMax
The default value is 3. If, for example,
ServerAliveInterval
is set to 15 andServerAliveCountMax
is left at the default, if the server becomes unresponsive,ssh
will disconnect after approximately 45 seconds. This option applies to protocol version 2 only.
Based on above, 0 means it's disabled. Therefore you should set these values high enough to avoid Broken pipe error.
The Answer from Barthelemy is cool but doesn't really get to the root of the problem. You suspend your machine and want the SSH session to be still alive when you boot up your computer.
There is no such configuration for ssh that will keep the connection alive like that. SSH uses TCP, for a start you need the three way handshake and then keep alive after some idle time. When you shutdown/hibernate all your TCP connections are closed with FIN. No way to overcome that.
For a dirty workaround you can use VPS or another online box with screen to keep the connection. My advice don't do that for security reasons.