![旧硬件和干净操作系统上的 SSH 连接不稳定](https://linux22.com/image/949589/%E6%97%A7%E7%A1%AC%E4%BB%B6%E5%92%8C%E5%B9%B2%E5%87%80%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E4%B8%8A%E7%9A%84%20SSH%20%E8%BF%9E%E6%8E%A5%E4%B8%8D%E7%A8%B3%E5%AE%9A.png)
我在 Intel Core Quad Q6600 和旧的 Gigabyte 965P-S3 主板上安装了 Ubuntu Server 18.04.6 LTS。目前 SSH 不稳定。客户端在 10 分钟不活动后断开连接,但没有任何错误。客户端只是卡住了。服务器继续保持连接,即使一个小时后也不会断开连接。问题可能是硬件吗?
答案1
OpenSSH 通常不会关闭空闲(或“丢失”)连接,因为定义空闲的方式有很多种,有些可能与您的定义不同。但是,您可以编辑 SSH 配置文件以包含以下两行:
ClientAliveInterval 300
ClientAliveCountMax 2
这将断开 600 秒(10 分钟)内未发出命令的连接。
ClientAliveCountMax
Sets the number of client alive messages which may be sent without sshd(8) receiving
any messages back from the client. If this threshold is reached while client alive
messages are being sent, sshd will disconnect the client, terminating the session.
It is important to note that the use of client alive messages is very different from
TCPKeepAlive. The client alive messages are sent through the encrypted channel and
therefore will not be spoofable. The TCP keepalive option enabled by TCPKeepAlive
is spoofable. The client alive mechanism is valuable when the client or server
depend on knowing when a connection has become unresponsive.
The default value is 3. If ClientAliveInterval is set to 15, and
ClientAliveCountMax is left at the default, unresponsive SSH clients will be
disconnected after approximately 45 seconds. Setting a zero ClientAliveCountMax
disables connection termination.
ClientAliveInterval
Sets a timeout interval in seconds after which if no data has been received from the
client, sshd(8) will send a message through the encrypted channel to request a
response from the client. The default is 0, indicating that these messages will not
be sent to the client.
简单来说,服务器会每隔300
(或多)秒向客户端发送一条消息。如果客户端在尝试(或多)次尝试后仍未响应2
,则连接将关闭。
修改配置文件后,请务必重新启动 OpenSSH,因为它仅在守护进程启动时读取。