我已经开始了我的 autossh witt 轮询时间为 30 秒:
AUTOSSH_POLL=30 AUTOSSH_LOGLEVEL=7 autossh -M 0 -f -S none -f -N -L localhost:34567:localhost:6543 user1@server1
它工作正常:
Sep 5 12:26:44 serverA autossh[20935]: check on child 23084
Sep 5 12:26:44 serverA autossh[20935]: set alarm for 30 secs
但是如果我物理移除了网络电缆,这意味着隧道不能再工作了,autossh 不会杀死 ssh 守护进程。为什么?我知道如果链接断开,autossh 将无能为力,但我认为它应该尝试执行以下操作:
- 验证子 ssh 进程 (
check on child ...
) - 验证远端!!!(通过隧道的类似 ping 的操作)
- 意识到隧道已关闭
- 停止 ssh 进程
- 尝试再次创建隧道
- 意识到它不起作用,并设置一个(指数增加?)计时器以尽快再次检查
这就是我运行 autossh 的原因:如果隧道出现问题(无论是软件还是硬件问题),它应该尝试重新启动它。相反,它只是在等待 ssh 进程终止。即使没有重新建立连接的希望,它不应该尝试重新启动它吗?
什么样的检查在做 autossh?只需验证 ssh 是否已启动并正在运行?它不做任何形式的远端检查吗?
编辑
根据要求,我添加了 ssh 配置的相关部分:
# (see http://aaroncrane.co.uk/2008/04/ssh_faster)
# The ServerAliveInterval tells SSH to send a keepalive message every 60 seconds while the connection is open;
# that both helps poor-quality NAT routers understand that the NAT table entry for your connection should
# be kept alive, and helps SSH detect when there’s a network problem between the server and client.
ServerAliveInterval 60
# The ServerAliveCountMax says that after 60 consecutive unanswered keepalive messages, the connection should
# be dropped. At that point, AutoSSH should try to invoke a fresh SSH client. You can tweak those
# specific values if you want, but they seem to work well for me.
ServerAliveCountMax 60
TCPKeepAlive yes
autossh 在您的客户端计算机上运行,因此它不能直接杀死服务器上的 ssh 守护进程。
ClientAliveInterval
但是,您可以在服务器上为in指定一个非零值/etc/ssh/sshd_config
(请参阅man sshd_config
参考资料)并重新启动服务器上的 sshd 服务以应用配置更改。然后在网络断开的情况下,ssh 守护进程将在ClientAliveInterval * ClientAliveCountMax
几秒钟后被杀死(但不是通过 autossh)。现在,如果您想问“为什么 autossh 不杀死 ssh 客户端进程?” ,您已指定
-M 0
。从 autossh 手册页:Setting the monitor port to 0 turns the monitoring function off, and autossh will only restart ssh upon ssh's exit
.您不是使用 autossh 来监视连接,而是等待 ssh 在超时
ServerAliveCountInterval * ServerAliveCountMax
几秒钟后退出。在 ssh 退出之前,您已经请求了 60 次 server-alive 检查,连续检查间隔为 60 秒,因此您将在 ssh 客户端退出之前等待一个小时。您还应该强烈考虑
ExitOnForwardFailure
在客户端使用该选项(请参阅man ssh_config
参考资料),以便 ssh 在无法建立隧道时退出,然后 autossh 可以尝试再次启动 ssh。