为什么 SSH 会断开连接并显示“ssh_dispatch_run_fatal:连接到 xxxx 端口 2020:连接已损坏”?

为什么 SSH 会断开连接并显示“ssh_dispatch_run_fatal:连接到 xxxx 端口 2020:连接已损坏”?

我正在尝试弄清楚为什么我总是断开与 SSH 会话的连接。主机是 CentOS 服务器,客户端是 MacOS。

这个错误似乎是偶尔发生的 - 有时每小时一次,有时一分钟几次。但有趣的是,这种情况不仅仅发生在我不使用连接时 - 我甚至可以在打字时连接就会断开。错误是:

ssh_dispatch_run_fatal: Connection to x.x.x.x port 2020: Connection corrupted

我也尝试打开多个选项卡并在每个选项卡中打开一个 ssh 连接,但它们似乎同时断开了连接。

打开调试级别 3 后,我得到了/var/log/secure有关断开连接时间的输出:

Feb 16 08:06:08 46 sshd[2864]: debug2: channel 0: request [email protected] confirm 1
Feb 16 08:06:08 46 sshd[2864]: debug3: send packet: type 98
Feb 16 08:06:08 46 sshd[2864]: debug3: receive packet: type 100
Feb 16 08:06:08 46 sshd[2864]: debug1: Got 100/121 for keepalive
Feb 16 08:06:38 46 sshd[2864]: debug2: channel 0: request [email protected] confirm 1
Feb 16 08:06:38 46 sshd[2864]: debug3: send packet: type 98
Feb 16 08:06:38 46 sshd[2864]: debug3: receive packet: type 100
Feb 16 08:06:38 46 sshd[2864]: debug1: Got 100/122 for keepalive
Feb 16 08:06:51 46 sshd[4517]: Connection closed by x.x.x.x port 62073
Feb 16 08:06:51 46 sshd[4517]: debug1: channel 0: free: server-session, nchannels 1
Feb 16 08:06:51 46 sshd[4517]: debug3: channel 0: status: The following connections are open:\r\n  #0 server-session (t4 r0 i0/0 o0/0 fd 12/8 cc -1)\r\n
Feb 16 08:06:51 46 sshd[4517]: Close session: user my_username from x.x.x.x port 62073 id 0
Feb 16 08:06:51 46 sshd[4517]: debug3: mm_request_send entering: type 30
Feb 16 08:06:51 46 sshd[4517]: debug3: session_unused: session id 0 unused
Feb 16 08:06:51 46 sshd[4517]: debug1: do_cleanup
Feb 16 08:06:51 46 sshd[4517]: debug3: PAM: sshpam_thread_cleanup entering
Feb 16 08:06:51 46 sshd[4517]: debug3: mm_request_send entering: type 122
Feb 16 08:06:51 46 sshd[4517]: debug3: mm_request_receive_expect entering: type 123
Feb 16 08:06:51 46 sshd[4517]: debug3: mm_request_receive entering
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_request_receive entering
Feb 16 08:06:51 46 sshd[4514]: debug3: monitor_read: checking request 30
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_answer_pty_cleanup entering
Feb 16 08:06:51 46 sshd[4514]: debug1: session_by_tty: session 0 tty /dev/pts/1
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_session_close: session 0 pid 4517
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_session_close: tty /dev/pts/1 ptyfd 4
Feb 16 08:06:51 46 sshd[4514]: debug1: session_pty_cleanup: session 0 release /dev/pts/1
Feb 16 08:06:51 46 sshd[4514]: debug3: session_unused: session id 0 unused
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_request_receive entering
Feb 16 08:06:51 46 sshd[4514]: debug3: monitor_read: checking request 122
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_request_send entering: type 123
Feb 16 08:06:51 46 sshd[4517]: debug3: mm_request_send entering: type 124
Feb 16 08:06:51 46 sshd[4517]: Transferred: sent 1503792, received 18928 bytes
Feb 16 08:06:51 46 sshd[4517]: Closing connection to x.x.x.x port 62073
Feb 16 08:06:51 46 sshd[4517]: debug3: mm_audit_event entering
Feb 16 08:06:51 46 sshd[4517]: debug3: mm_request_send entering: type 112
Feb 16 08:06:51 46 sshd[4517]: debug3: mm_request_send entering: type 50
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_request_receive entering
Feb 16 08:06:51 46 sshd[4514]: debug3: monitor_read: checking request 124
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_request_receive entering
Feb 16 08:06:51 46 sshd[4514]: debug3: monitor_read: checking request 112
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_answer_audit_event entering
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_request_receive entering
Feb 16 08:06:51 46 sshd[4514]: debug3: monitor_read: checking request 50
Feb 16 08:06:51 46 sshd[4514]: debug3: mm_answer_term: tearing down sessions
Feb 16 08:06:51 46 sshd[4514]: debug1: PAM: cleanup
Feb 16 08:06:51 46 sshd[4514]: debug1: PAM: closing session
Feb 16 08:06:51 46 sshd[4514]: pam_unix(sshd:session): session closed for user my_username
Feb 16 08:06:51 46 sshd[4514]: debug1: PAM: deleting credentials

部分内容被混淆x我的用户名


更新

我在上面添加了一组新日志 - 比原来的少,但我希望是适用的。新行:

Feb 16 08:06:51 46 sshd[4517]: Connection closed by x.x.x.x port 62073

我对此很感兴趣。这是否意味着我的设备正在断开连接并且与 SSH 无关?我看到(我认为)在断开连接之前成功发送了保持活动数据包。


我尝试重新启动守护程序,并在 Mac 上使用 OpenSSH(通过 brew)版本的 SSH,但没有任何区别。

服务器信息:

● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2022-02-11 10:21:40 GMT; 1h 52min ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 6271 (sshd)
   CGroup: /system.slice/sshd.service
           └─6271 /usr/sbin/sshd -D

我们与标准 CentOS 的唯一区别sshd_config是端口、日志级别和:

ClientAliveInterval 30
ClientAliveCountMax 5

为什么我会断开连接?什么原因导致连接中断?

答案1

这很可能是断开连接的问题,我不确定您使用的是哪个版本,如果你检查 ssh 代码库,它说:

ssh 将返回 SSH_ERR_CONN_CORRUPT,主要原因是 sshpkt_disconnect。

现在的问题是如何保持连接处于活动状态,我们有多种方式,一种方式是“milenao”已经共享,但在更改设置之前,请先阅读效果。另一种方式是运行某种隧道来发送任何类型的流量,以便服务认为连接处于活动状态,例如您可以根据需要以某种频率发送 ping、日期、监视、睡眠。

更新

通过以上日志我可以说您的连接未处于活动状态并且已启动连接关闭,这与我们所预测的相同。

   /* The connection has been terminated. */
   ssh_packet_get_bytes(ssh, &ibytes, &obytes);
   verbose("Transferred: sent %llu, received %llu bytes",
   (unsigned long long)obytes, (unsigned long long)ibytes);

   verbose("Closing connection to %.500s port %d", remote_ip, remote_port);

答案2

您可以尝试增加 ClientAliveInterval 和 ClientAliveCountMax 值:

ClientAliveInterval 60
ClientAliveCountMax 10

最有可能的是,服务器停止接收来自客户端的数据包,并且由于超时而断开连接

答案3

从这里:https://linux-tips.com/t/how-to-solve-ssh-disconnect-packet-corrupt-problems/258

尝试运行此命令来禁用硬件校验和:

sudo ethtool -K eth0 tx off rx off

听起来可能是硬件问题。我还在其他地方读到过,打开/关闭整个服务器或者执行某个操作ip link set dev ${INTERFACE} down; ip link set dev ${INTERFACE} up也可能有效。

答案4

我遇到了同样的问题,因此我将以下内容添加到我的.ssh/配置文件,我还没有看到该错误。

Host i-* mi-*
ServerAliveInterval 300
ServerAliveCountMax 2

不能保证这一定会有帮助,但到目前为止,它允许我的连接保持比现在更长的时间。

相关内容