che*_*ner 32
如果您打开第一个连接-M:
ssh -M $REMOTEHOST
Run Code Online (Sandbox Code Playgroud)
后续连接$REMOTEHOST将"搭载"主设备建立的连接ssh.最值得注意的是,不需要进一步的身份验证.有关man ssh_config详细信息,请参阅"ControlMaster".使用-S指定的路径,共享插座; 我不确定默认值是什么,因为我使用配置文件来配置连接共享.
在我的.ssh/config文件中,我有以下几行:
host *
ControlMaster auto
ControlPath ~/.ssh/ssh_mux_%h_%p_%r
Run Code Online (Sandbox Code Playgroud)
这样,我就不必记得使用-M或-S; ssh确定主机/端口/用户名组合是否已存在可共享连接,并在可能的情况下使用该连接.
自2004年以来,此选项在OpenSSH中可用.
Jas*_*son 14
我更喜欢Puppet Labs中描述的方法https://puppetlabs.com/blog/speed-up-ssh-by-reusing-connections
将这些行添加到~/.ssh/config并运行mkdir ~/.ssh/sockets
Host *
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 600
Run Code Online (Sandbox Code Playgroud)
阅读完整的博客文章,了解更多有用的信息,了解这些内容以及ssh在使用时的特性.我强烈建议您阅读博客,否则您可能会发现某些内容无法正常运行.
hax*_*ode 12
或者,你可以这样做:
$ssh_conn="ssh -t -o ControlPath=~/.ssh/master-$$ -o ControlMaster=auto -o ControlPersist=60"
$ssh_conn user@server
Run Code Online (Sandbox Code Playgroud)
ControlPath=~/.ssh/master-$$为ssh连接设置控制路径,限制连接重用到当前shell(通过
$$PID)ControlMaster=auto 允许使用.共享连接会话 ControlPathControlPesist=60 设置由于不活动而导致连接保持打开的时间