设置SSH连接超时

mul*_*lle 39 ssh timeout

我正在努力减少ssh尝试打开与主机的连接的时间.如果我举个例子,ssh www.google.com它需要很长时间,直到提示回来.

我读过关于使用的内容ssh -o ConnectTimeout=10 www.google.com,但即使这需要很长时间.我可以修改一些尝试来减少阻塞时间吗?

sin*_*law 57

问题可能是ssh正在尝试连接到解析为的所有不同IP www.google.com.例如在我的机器上:

# ssh -v -o ConnectTimeout=1 -o ConnectionAttempts=1 www.google.com
OpenSSH_5.9p1, OpenSSL 0.9.8t 18 Jan 2012
debug1: Connecting to www.google.com [173.194.43.20] port 22.
debug1: connect to address 173.194.43.20 port 22: Connection timed out
debug1: Connecting to www.google.com [173.194.43.19] port 22.
debug1: connect to address 173.194.43.19 port 22: Connection timed out
debug1: Connecting to www.google.com [173.194.43.18] port 22.
debug1: connect to address 173.194.43.18 port 22: Connection timed out
debug1: Connecting to www.google.com [173.194.43.17] port 22.
debug1: connect to address 173.194.43.17 port 22: Connection timed out
debug1: Connecting to www.google.com [173.194.43.16] port 22.
debug1: connect to address 173.194.43.16 port 22: Connection timed out
ssh: connect to host www.google.com port 22: Connection timed out
Run Code Online (Sandbox Code Playgroud)

如果我使用特定的IP运行它,它返回的速度要快得多.

编辑:我已经定时(带time),结果如下:

  • www.google.com - 5.086秒
  • 173.94.43.16 - 1.054秒

  • 想象一下,如果上面的连接真的通过了:) (4认同)

Joh*_*ote 5

ConnectTimeout 选项允许您告诉您的 ssh 客户端在返回错误之前您愿意等待连接多长时间。通过将 ConnectTimeout 设置为 1,您实际上是在说“最多尝试 1 秒,如果您还没有连接,则失败”。

问题是当您按名称连接时,DNS 查找可能需要几秒钟。通过 IP 地址连接要快得多,实际上可能在一秒钟或更短的时间内工作。sinelaw 正在经历的是,每次通过 DNS 名称进行连接的尝试都无法在一秒内发生。ConnectTimeout 的默认设置遵循 linux 内核连接超时,这通常很长。

  • DNS 查找需要几秒钟的时间是相当不寻常的。 (7认同)