登录后约 30 秒 SSH 挂起

登录后约 30 秒 SSH 挂起

这可能是我的网络问题,但我认为这里有人可能有一些步骤可以帮助解决问题。我目前可以通过 SSH 顺利登录服务器,但大约 30 秒到 40 秒后它就完全挂起了。

从数据包捕获中我可以看到数据包在客户端和服务器 ssh 进程之间来回发送,直到最后我的服务器只是反复尝试发送数据包而没有收到任何返回。

以下是 tcpdump 的一个主要部分

18:49:28.466635 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [.], ack 2490, win 274, options [nop,nop,TS val 1248808280 ecr 19660300], length 0
18:49:28.466990 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 1904:2356, ack 2490, win 274, options [nop,nop,TS val 1248808281 ecr 19660300], length 452
18:49:28.468229 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [.], ack 2356, win 294, options [nop,nop,TS val 19660301 ecr 1248808281], length 0
18:49:28.470848 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 2490:2598, ack 2356, win 294, options [nop,nop,TS val 19660301 ecr 1248808281], length 108
18:49:28.471451 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 2598:2850, ack 2356, win 294, options [nop,nop,TS val 19660301 ecr 1248808281], length 252
18:49:28.471489 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 2850:3006, ack 2356, win 294, options [nop,nop,TS val 19660301 ecr 1248808281], length 156
18:49:28.471591 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [.], ack 3006, win 305, options [nop,nop,TS val 1248808285 ecr 19660301], length 0
18:49:28.615964 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 3006:3106, ack 2356, win 294, options [nop,nop,TS val 19660337 ecr 1248808285], length 100
18:49:28.656916 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [.], ack 3106, win 305, options [nop,nop,TS val 1248808471 ecr 19660337], length 0
18:49:33.621448 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2356:2408, ack 3106, win 305, options [nop,nop,TS val 1248813435 ecr 19660337], length 52
18:49:33.623447 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 3106:3134, ack 2408, win 294, options [nop,nop,TS val 19661589 ecr 1248813435], length 28
18:49:33.623509 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [.], ack 3134, win 305, options [nop,nop,TS val 1248813437 ecr 19661589], length 0
18:49:38.628948 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2408:2460, ack 3134, win 305, options [nop,nop,TS val 1248818443 ecr 19661589], length 52
18:49:38.630927 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 3134:3162, ack 2460, win 294, options [nop,nop,TS val 19662841 ecr 1248818443], length 28
18:49:38.630981 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [.], ack 3162, win 305, options [nop,nop,TS val 1248818445 ecr 19662841], length 0
18:49:43.636271 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2460:2512, ack 3162, win 305, options [nop,nop,TS val 1248823450 ecr 19662841], length 52
18:49:43.637913 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 3162:3190, ack 2512, win 294, options [nop,nop,TS val 19664093 ecr 1248823450], length 28
18:49:43.637970 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [.], ack 3190, win 305, options [nop,nop,TS val 1248823452 ecr 19664093], length 0
18:49:48.641414 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2512:2564, ack 3190, win 305, options [nop,nop,TS val 1248828455 ecr 19664093], length 52
18:49:48.643492 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 3190:3218, ack 2564, win 294, options [nop,nop,TS val 19665344 ecr 1248828455], length 28
18:49:48.643575 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [.], ack 3218, win 305, options [nop,nop,TS val 1248828457 ecr 19665344], length 0
18:49:53.649083 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2564:2616, ack 3218, win 305, options [nop,nop,TS val 1248833463 ecr 19665344], length 52
18:49:53.650953 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 3218:3246, ack 2616, win 294, options [nop,nop,TS val 19666596 ecr 1248833463], length 28
18:49:53.651018 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [.], ack 3246, win 305, options [nop,nop,TS val 1248833465 ecr 19666596], length 0
18:49:58.656273 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2616:2668, ack 3246, win 305, options [nop,nop,TS val 1248838470 ecr 19666596], length 52
18:49:58.658237 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 3246:3274, ack 2668, win 294, options [nop,nop,TS val 19667848 ecr 1248838470], length 28
18:49:58.658292 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [.], ack 3274, win 305, options [nop,nop,TS val 1248838472 ecr 19667848], length 0
18:50:03.663677 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2668:2720, ack 3274, win 305, options [nop,nop,TS val 1248843477 ecr 19667848], length 52
18:50:03.665593 IP 10.0.3.5.22 > 10.0.2.11.58776: Flags [P.], seq 3274:3302, ack 2720, win 294, options [nop,nop,TS val 19669100 ecr 1248843477], length 28
18:50:03.665656 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [.], ack 3302, win 305, options [nop,nop,TS val 1248843479 ecr 19669100], length 0
18:50:08.671149 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2720:2772, ack 3302, win 305, options [nop,nop,TS val 1248848485 ecr 19669100], length 52
18:50:08.879977 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2720:2772, ack 3302, win 305, options [nop,nop,TS val 1248848694 ecr 19669100], length 52
18:50:09.087980 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2720:2772, ack 3302, win 305, options [nop,nop,TS val 1248848902 ecr 19669100], length 52
18:50:09.503964 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2720:2772, ack 3302, win 305, options [nop,nop,TS val 1248849318 ecr 19669100], length 52
18:50:10.360063 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2720:2772, ack 3302, win 305, options [nop,nop,TS val 1248850174 ecr 19669100], length 52
18:50:12.023959 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2720:2772, ack 3302, win 305, options [nop,nop,TS val 1248851838 ecr 19669100], length 52
18:50:15.352072 IP 10.0.2.11.58776 > 10.0.3.5.22: Flags [P.], seq 2720:2772, ack 3302, win 305, options [nop,nop,TS val 1248855166 ecr 19669100], length 52

在 SSH 的服务器端,它看起来几乎相同,只是由于 NAT 规则,服务器看到的是 10.0.0.10 而不是 10.0.2.11,但端口是相同的。

如果我直接登录到服务器,我尝试通过 SSH 连接,我可以连续 ping 它而不会出现问题。

非常感谢您的帮助。数据包正在流动,然后突然停止,这似乎很奇怪。

这似乎不是一个 MTU 问题,因为我的网络中的所有内容都设置为 1500,并且我可以使用 相互 ping 两个系统ping 10.0.3.5 -s 1472 -D

答案1

这听起来像是路径 mtu 问题。这种情况很常见,蜗牛书中的常见问题解答

答案2

TL;DR:检查是否有其他方式让包从客户端流向服务器,反之亦然,也称为非对称路由

我遇到了同样的问题,但解决方法不同。所有设备上的 MTU 都正确设置,但 ssh 连接在大约 30 秒后仍会冻结。经过一番谷歌搜索,我发现这个帖子

远程主机通过无线适配器连接到 LAN,因此来自远程主机的所有流量都在本地传输,

这提示我找到了正确的解决方案。我的服务器位于由防火墙隔开的另一个 LAN 中,但是,为了进行测试,我在与客户端相同的网络上设置了第二个 NIC。这导致了问题,客户端会通过防火墙将数据包发送到服务器,但服务器会通过直接连接到客户端网络的 NIC 进行响应,而不是使用防火墙。

如图:

client --ssh--> firewall --ssh--> NIC_1 server
  ^                                  |
  |                                  V
  +-----------------ssh---------- NIC_2 server

由于答案没有通过防火墙,它假定连接失败并在 30 秒后关闭它。

答案3

这仅适用于我的设置,但是如果其他人遇到此问题并且不是 MTU 设置不正确(对于大多数看到此问题的人来说,这很可能是该问题),请检查您的防火墙设置。

我禁用了 LAN 中所有端口的防火墙,这个问题就解决了。等我有时间了,我会再多玩一会儿,看看到底是什么被阻止了。

相关内容