SSH 中的 ServerAliveCountMax

SSH 中的 ServerAliveCountMax

SSH 中的 ServerAliveCountMax 实际上起什么作用?

我试图确保当我通过 SSH 连接到我的服务器时,连接会保持很长时间,而不是在短暂不活动后就断开。这是示例

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 2

我听说一个来源只要服务器收到响应,上述设置就会每 60 秒向服务器发送一次响应。但是,如果出于某种原因响应未到达服务器,它会尝试发送另一条消息。如果该消息也失败,那么它将关闭连接。(我觉得这是错误的)

第二第三然而,消息来源的说法却有所不同。他们声称,如果一段时间内没有活动,则会每 60 秒向服务器发送一条消息,但它只会发送 2 个请求,然后就会关闭连接。

那么 ServerAliveCountMax 到底起什么作用?

答案1

您觉得“这是错误的”是正确的。查看手册页

 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.

答案2

当 SSH 服务器配置为在一段时间内没有流量后关闭连接时,服务器活动消息非常有用(例如,提供 SSH 访问的共享 Web 托管提供商几乎总是这样做)。设置这两个选项每秒发送一个数据包ServerAliveInterval,最多发送几次,ServerAliveCountMax从而保持会话活动。

为了回答关于将任一选项设置为的不确定性的评论0,我已经阅读了实现的源代码openssh,以下是我所看到的......

  • 设置ServerAliveInterval0将不会发送数据包,但它将无限期地保持会话处于活动状态,假设连接不会由于 TCP 超时而断开,并且服务器未配置为丢弃不活动的客户端。

  • 设置ServerAliveCountMax为与设置为0具有同样的效果。ServerAliveInterval0

  • 将任一值设置为负数或任何大于INT_MAX(即 2,147,483,647)的值都将导致“整数值...”错误。

  • 设置ServerAliveCountMaxINT_MAX/1000+1(即 2,147,484) 到INT_MAX(即 2,147,483,647) 之间也相当于将任一值设置为0

因此,实际上,您可以获得的最大超时时间(同时仍发送数据包)是INT_MAX/1000(即 2,147,483)。如果超时时间为1,并且会话中没有任何流量,则可以使用近 25 天。

显然,SSH 的其他实现可能会有不同的结果。

相关内容