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:

         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.

         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.
| improve this answer | |
  • 4
    The man page is clear on Interval set to 0 disabling it. But it is not clear if you set Max to 0. Would it send infinite Alive pings, or none? – gcb Dec 3 '15 at 12:05
  • 1
    it is not clear to me if setting ServerAliveInterval 0 will keep the connection open indefinitely or not – Francesco Nov 10 '17 at 5:16
  • 1
    @Francesco By default the connection will remain open forever, unless one end explicitly closes it. – Michael Hampton Nov 10 '17 at 5:18
  • @MichaelHampton It may be worth clarifying that neither of the OPs descriptions are accurate - the true behaviour is a hybrid of the two descriptions (the link liveness checks are only sent when idle, but the link is only dropped if the server doesn't reply to them - they continue indefinitely if the server replies) – ncoghlan Sep 16 at 4:22

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.