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


9

最近,我经常使用SSH客户端的ControlMaster功能,该功能使我可以将单个SSH-TCP连接用于多个外壳和端口转发到同一远程系统。最令人讨厌的是,打开的第一个外壳程序进程将自动变为ControlMaster。这意味着,如果该过程终止,则使用控制主连接的所有其他外壳程序和端口转发将不可用。

我真的很想,到远程系统的第一个ssh命令会产生一个额外的后台进程来保存该连接,只要仍然有使用ControlMaster连接的连接,那么我就可以简单地关闭实际的shell,而不必担心会崩溃连接。理想情况下,后台ControlMaster进程甚至可以配置为等待一定时间,以便新的外壳或端口转发使用ControlMaster,然后最终关闭。

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

让包装程序脚本做起来也不是那么容易,因为我经常在.ssh / config中使用已配置的速记来表示远程服务器名称,并且ControlMaster套接字是使用USERNAME @ NETWORK_NAME:NETWORK_PORT作为名称创建的。因此,包装程序需要完全理解.config / ssh才能按预期工作。

Answers:


10

您应该使用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 no是默认行为。我使用ControlPersist 4h允许后台会话定期清理自己。


这在RHEL上可用吗?
ewwhite 2014年

1
@ewwhite我没有RHEL,但CentOS应该相同。它在CentOS 7中,但似乎不在CentOS 6.5中。openssh更改日志表明它已添加到openssh 5.6中,而CentOS 6.x仅具有5.3(7.0具有openssh 6.4)
Daniel Lawson 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.