ServerAliveCountMax in SSH

Solution 1:

Your feeling that "this is wrong" is correct. See the man page:

 ServerAliveCountMax
         Sets the number of server alive messages (see below) which may be
         sent without ssh(1) 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.  It is important to note that the use of server alive
         messages is very different from TCPKeepAlive (below).  The server
         alive messages are sent through the encrypted channel and there‐
         fore will not be spoofable.  The TCP keepalive option enabled by
         TCPKeepAlive is spoofable.  The server alive mechanism is valu‐
         able when the client or server depend on knowing when a connec‐
         tion has become inactive.

         The default value is 3.  If, for example, ServerAliveInterval
         (see below) is set to 15 and ServerAliveCountMax 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.

 ServerAliveInterval
         Sets a timeout interval in seconds after which if no data has
         been received from the server, ssh(1) 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.

Solution 2:

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 of ServerAliveCountMax 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 the openssh implementation, and here is what I see...

  • Setting ServerAliveInterval to 0 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 to 0 has the same effect as setting ServerAliveInterval to 0.

  • 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 between INT_MAX/1000+1 (i.e. 2,147,484) to INT_MAX (i.e. 2,147,483,647) would also be equivalent to setting either value to 0.

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 of 1 and no traffic on the sessions at all, that would get you almost 25 days.

Obviously, other implementations of SSH may have different results.

Tags:

Ssh