我在 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,因为它仅在守护进程启动时读取。