
O que o ServerAliveCountMax no SSH realmente faz?
Estou tentando garantir que, quando me conectar ao meu servidor via SSH, a conexão permaneça aberta por um longo período de tempo, em vez de a conexão morrer após um curto período de inatividade. Este é o exemplo
Host *
ServerAliveInterval 60
ServerAliveCountMax 2
Eu ouvi deUma fonteque a configuração acima sempre enviará uma resposta ao servidor a cada 60 segundos, desde que o servidor receba essa resposta. Porém, se por algum motivo a resposta não chegar ao servidor, ele tentará enviar outra mensagem. Se essa mensagem também falhar, a conexão será encerrada. (Eu sinto que isso está errado)
Osegundoeterceirofonte, entretanto, diz algo diferente. Eles afirmam que uma mensagem será enviada ao servidor a cada 60 segundos se houver um período de inatividade, mas só será enviada através de 2 solicitações e depois fechará a conexão.
Então, o que exatamente ServerAliveCountMax faz?
Responder1
Seu sentimento de que “isso está errado” está correto.Veja a 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.
Responder2
Mensagens de servidor ativo são úteis quando um servidor SSH foi configurado para fechar conexões após um período de tempo sem tráfego (provedores de hospedagem compartilhada que oferecem acesso SSH quase sempre fazem isso, por exemplo). Definir essas duas opções envia um pacote a cada ServerAliveInterval
segundo, no máximo, ServerAliveCountMax
mantendo a sessão ativa.
Para responder aos comentários sobre a incerteza de definir qualquer opção como 0
, li o código-fonte da openssh
implementação e aqui está o que vejo...
Definir
ServerAliveInterval
como0
NÃO enviará os pacotes, mas manterá a sessão ativa indefinidamente, assumindo que a conexão não seja interrompida devido ao tempo limite do TCP e que o servidor não esteja configurado para descartar clientes inativos.Definir
ServerAliveCountMax
como0
tem o mesmo efeito que definirServerAliveInterval
como0
.Definir qualquer valor como negativo ou qualquer valor maior que
INT_MAX
(ou seja, 2.147.483.647) resultará em um"valor inteiro..."erro.Definir
ServerAliveCountMax
entreINT_MAX/1000+1
(ou seja, 2.147.484) eINT_MAX
(ou seja, 2.147.483.647) também seria equivalente a definir qualquer um dos valores como0
.
Então, em essência, o maior tempo limite que você pode obter (enquanto ainda envia os pacotes) é INT_MAX/1000
(ou seja, 2.147.483). Com um tempo limite 1
e nenhum tráfego nas sessões, você teria quase 25 dias.
Obviamente, outras implementações de SSH podem ter resultados diferentes.