首次访问 ssh 远程系统时自动生成 ControlMaster 后台进程

首次访问 ssh 远程系统时自动生成 ControlMaster 后台进程

最近我经常使用 SSH 客户端的 ControlMaster 功能,该功能允许我使用单个 SSH-TCP 连接将多个 shell 和端口转发到同一个远程系统。最烦人的是,打开的第一个 shell 进程会自动成为 ControlMaster。这意味着,如果此进程终止,则使用控制主连接的所有其他 shell 和端口转发都将不可用。

我非常希望第一个 ssh 命令能够生成一个额外的后台进程,只要仍有使用 ControlMaster 连接的连接,该进程就会保持连接,这样我就可以简单地关闭实际的 shell,而不必冒着破坏其他连接的风险。理想情况下,后台 ControlMaster 进程甚至可以配置为等待一段时间,让新的 shell 或端口转发使用 ControlMaster,然后再最终关闭。

有没有办法让 ssh 客户端做这样的事情?我知道可以在使用 ssh 创建第一个 shell 之前手动创建这样的连接,但我明确希望这能自动发生,因为否则我肯定会时不时忘记这样做。

让包装器脚本执行此操作也不太容易,因为我经常在 .ssh/config 中使用配置的远程服务器名称简写,并且 ControlMaster 套接字是使用 USERNAME@NETWORK_NAME:NETWORK_PORT 作为名称创建的。因此,包装器需要完全理解 .config/ssh 才能按预期工作。

答案1

您应该使用 ControlPersist 配置选项。

 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 否是默认行为,正如您所描述的那样。我使用控制坚持4小时允许后台会话定期自我清理。

相关内容