最近我经常使用SSH客户端的ControlMasterfunction,这使我可以使用一个单一的SSH-TCP连接用于多个shell,并将端口转发到同一个远程系统。 最令人讨厌的是,第一个打开的shell进程自动成为ControlMaster。 这意味着,如果此进程终止,则使用控制主连接的所有其他shell和端口转发将不可用。
我真的很喜欢什么时候,第一个远程系统的ssh命令会产生一个额外的后台进程来保持连接,只要仍然有连接使用ControlMaster连接,所以我可以简单地closures实际的shell而不必冒险来使其他的crash连接。 理想情况下,后台ControlMaster进程甚至可以configuration为在最终closures之前等待新shell或端口转发的时间来使用ControlMaster。
有没有办法让SSH客户端做这样的事情? 我知道我可以在使用ssh创build第一个shell之前手动创build这样的连接,但是我明确地希望这会自动发生,否则我一定会忘记每隔一段时间执行一次。
使用包装器脚本也不会那么容易,因为我经常在.ssh / config中为远程服务器名称使用已configuration的shorthands,而使用USERNAME @ NETWORK_NAME:NETWORK_PORT作为名称创buildControlMaster套接字。 因此,一个包装将需要理解.config / ssh完全按预期工作。
您应该使用ControlPersistconfiguration选项。
ControlPersist When used in conjunction with ControlMaster, specifies that the master connection should remain open in the background (waiting for future client connections) after the initial client connec‐ tion has been closed. If set to “no”, then the master connection will not be placed into the background, and will close as soon as the initial client connection is closed. If set to “yes”, then the master connection will remain in the background indefinitely (until killed or closed via a mechanism such as the ssh(1) “-O exit” option). If set to a time in seconds, or a time in any of the formats documented in sshd_config(5), then the backgrounded master connection will automatically terminate after it has remained idle (with no client connections) for the specified time.
ControlPersist no是默认行为,正如您所描述的那样。 我使用ControlPersist 4h来允许后台会话定期清理自己。