得之我幸 失之我命

when someone abandons you,it is him that gets loss because he lost someone who truly loves him but you just lost one who doesn’t love you.

ssh 配置 ControlMaster

multiplexing 和独立会话的区别是,在服务器和客户端,都只看到一个进程(即使多次连接会话)

在服务器和客户机上,可以看到只打开了一个 TCP 连接,OpenSSH 使用现有的 TCP 连接实现多个 SSH 会话,降低新建 TCP 连接的负载

那么如何做到让 SSH 复用连接呢

通过 ControlMaster,ControlPath 和 ControlPersist 配置来实现,被复用的连接也可以叫主连接(master connection)

  1. ControlMaster:允许通过单个网络连接共享多个会话

    • no(default)
    • yes:ssh 会监听该路径下的 control socket,多个 session 会去连接该 socket,则会尽可能的复用连接
    • ask
    • auto:尝试使用主连接,但如果不存在主连接,则后退到创建新连接
    • autoask
  2. ControlPath:指定 control socket 的路径

    配合以下参数食用:

    • %L:本地主机名的第一个组件
    • %l:本地主机名(包括域名)
    • %h:远程主机名(命令行输入)
    • %n:远程原始主机名
    • %p:远程主机端口
    • %r:远程登录用户名
    • %u:本地 ssh 正在使用的用户名
    • %i:本地 ssh 正在使用 uid
    • %C:值为 %l%h%p%r 的 hash
  3. ControlPersist:指定连接打开后,后台保持的时间

    • no(default):主连接不会放入后台,最初的客户端关闭就关闭
    • yes/0:主连接无限期地保留在后台
    • 整数+单位:主连接保留在后台的时间

注意:在 ssh_config 关于 ControlMaster 的描述中,有一段令人迷惑的说辞

Additional sessions can connect to this socket using the same ControlPath with ControlMaster set to no (the default). These sessions will try to reuse the master instance's network connection rather than initiating new ones, but will fall back to connecting normally if the control socket does not exist, or is not listening.

❓“即使 ControlMaster 的值是 no,其他会话可以使用相同的 ControlPath 连接到此套接字”,这是为什么?

可能是因为 ControlMaster controls whether an ssh instance will be a control master, not whether it uses the control feature at all. If you want to disable the control feature for an ssh instance, set ControlPath to “none”.

be slow to promise and quick to perform.