SSH 中的 ServerAliveCountMax

Joh*_*ord 41 ssh

SSH 中的 ServerAliveCountMax 实际上是做什么的?

我试图确保当我通过 SSH 连接到我的服务器时,连接保持打开状态很长一段时间,而不是连接在短时间内不活动后死亡。这是例子

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 2
Run Code Online (Sandbox Code Playgroud)

我从一个来源听说,只要服务器收到该响应,上述设置将始终每 60 秒向服务器发送一次响应。但是,如果由于某种原因响应没有通过服务器,它将尝试发送另一条消息。如果该消息也失败,则它将关闭连接。(我觉得这是错误的)

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

那么 ServerAliveCountMax 究竟是做什么的呢?

Mic*_*ton 42

你认为“这是错误的”是正确的。请参阅手册页

 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.
Run Code Online (Sandbox Code Playgroud)

  • 手册页清楚地将“间隔”设置为“0”以禁用它。但是不清楚你是否将 `Max` 设置为 `0`。它会发送无限的 Alive ping,还是不发送? (7认同)

Dre*_*pin 12

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

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

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

  • 设置ServerAliveCountMax0与设置ServerAliveInterval为相同的效果0

  • 将任一值设置为负数或任何大于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 的其他实现可能会有不同的结果。