What does the ServerAliveCountMax in SSH actually do?
I am trying to ensure that when I connect to my server via SSH that the connection remains open for a long period of time instead of the connection dying after a short period of inactivity. This is the example
Host *
ServerAliveInterval 60
ServerAliveCountMax 2
I've heard from one source that the above setting will always send a response to the server every 60 seconds so long as the server receives that response. However if for whatever reason the response doesn't go through to the server, it will try and send another message. If that message fails too, then it will close the connection. (I feel this is wrong)
The second and third source however say something different. They claim that a message will be sent to the server every 60 seconds if there is a period of inactivity, but it will only send through 2 requests and then it will close the connection.
So what exactly does ServerAliveCountMax do?
Your feeling that "this is wrong" is correct. See the man page:
Server alive messages are useful when an SSH server has been configured to close connections after a period of time with no traffic (shared web-hosting providers that offer SSH access almost always do this for example). Setting these two options sends a packet every
ServerAliveInterval
seconds, for a maximum ofServerAliveCountMax
times thus keeping the session alive.To answer the comments about the uncertainty of setting either option to
0
, I have read through the source code of theopenssh
implementation, and here is what I see...Setting
ServerAliveInterval
to0
will NOT send the packets, but it will keep the session alive indefinitely assuming that the connection is not dropped due to TCP timeout and that the server is not configured to drop inactive clients.Setting
ServerAliveCountMax
to0
has the same effect as settingServerAliveInterval
to0
.Setting either value to a negative or anything greater than
INT_MAX
(i.e. 2,147,483,647) will result in an "integer value..." error.Setting
ServerAliveCountMax
betweenINT_MAX/1000+1
(i.e. 2,147,484) toINT_MAX
(i.e. 2,147,483,647) would also be equivalent to setting either value to0
.So, in essence, the most timeouts you can get (while still sending the packets) is
INT_MAX/1000
(i.e. 2,147,483). With a timeout of1
and no traffic on the sessions at all, that would get you almost 25 days.Obviously, other implementations of SSH may have different results.