
¿Qué hace realmente ServerAliveCountMax en SSH?
Estoy tratando de asegurarme de que cuando me conecto a mi servidor a través de SSH, la conexión permanezca abierta durante un largo período de tiempo en lugar de que muera después de un breve período de inactividad. Este es el ejemplo
Host *
ServerAliveInterval 60
ServerAliveCountMax 2
He oído deuna fuenteque la configuración anterior siempre enviará una respuesta al servidor cada 60 segundos siempre que el servidor reciba esa respuesta. Sin embargo, si por alguna razón la respuesta no llega al servidor, intentará enviar otro mensaje. Si ese mensaje también falla, se cerrará la conexión. (Siento que esto está mal)
ElsegundoyterceroSin embargo, la fuente dice algo diferente. Afirman que se enviará un mensaje al servidor cada 60 segundos si hay un período de inactividad, pero solo enviará 2 solicitudes y luego cerrará la conexión.
Entonces, ¿qué hace exactamente ServerAliveCountMax?
Respuesta1
Su sensación de que "esto está mal" es correcta.Ver la página de manual:
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.
Respuesta2
Los mensajes de servidor activo son útiles cuando un servidor SSH se ha configurado para cerrar conexiones después de un período de tiempo sin tráfico (los proveedores de alojamiento web compartido que ofrecen acceso SSH casi siempre hacen esto, por ejemplo). Al configurar estas dos opciones, se envía un paquete cada ServerAliveInterval
segundo, durante un máximo de ServerAliveCountMax
veces, manteniendo así viva la sesión.
Para responder a los comentarios sobre la incertidumbre de configurar cualquiera de las opciones en 0
, he leído el código fuente de la openssh
implementación y esto es lo que veo...
Configurar
ServerAliveInterval
NO0
enviará los paquetes, pero mantendrá la sesión activa indefinidamente suponiendo que la conexión no se interrumpa debido al tiempo de espera de TCP y que el servidor no esté configurado para descartar clientes inactivos.Establecer
ServerAliveCountMax
en0
tiene el mismo efecto que establecerServerAliveInterval
en0
.Establecer cualquiera de los valores en negativo o en cualquier valor mayor que
INT_MAX
(es decir, 2.147.483.647) dará como resultado un"valor entero..."error.Establecer
ServerAliveCountMax
entreINT_MAX/1000+1
(es decir, 2.147.484) yINT_MAX
(es decir, 2.147.483.647) también sería equivalente a establecer cualquiera de los valores en0
.
Entonces, en esencia, la mayor cantidad de tiempos de espera que puede obtener (mientras sigue enviando los paquetes) es INT_MAX/1000
(es decir, 2,147,483). Con un tiempo de espera 1
y ningún tráfico en las sesiones, eso le daría casi 25 días.
Obviamente, otras implementaciones de SSH pueden tener resultados diferentes.