Linux下SSH Session复制功能的实现方法

本篇内容主要讲解“Linux下SSH Session复制功能的实现方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux下SSH Session复制功能的实现方法”吧!

代码如下:


Linux/mac下,在$HOME/.ssh/config中加入
Host *
ControlMaster auto
ControlPath <a>/tmp/ssh-%r@%h</a>

至此只要第一次SSH登录输入密码,之后同个Hosts则免登。

配置文件分析

man ssh_config 5

代码如下:


ControlPath
            Specify the path to the control socket used for connection sharing as described in the ControlMaster section
            above or the string “none” to disable connection sharing.  In the path, &lsquo;%l&rsquo; will be substituted by the
            local host name, &lsquo;%h&rsquo; will be substituted by the target host name, &lsquo;%p&rsquo; the port, and &lsquo;%r&rsquo; by the remote
            login username.  It is recommended that any ControlPath used for opportunistic connection sharing include at
            least %h, %p, and %r.  This ensures that shared connections are uniquely identified.

%r 为远程机器的登录名
%h 为远程机器名

原理分析

严格地讲,它并不是真正意义上的Session Copy,而只能说是共享Socket。
第一次登录的时候,将Socket以文件的形式保存到:/tmp/ssh-%r@%h这个路径
之后登录的时候,一旦发现是同个主机,则复用这个Socket
故,一旦主进程强制退出(Ctrl+C),则其他SSH则*退出。

可以通过ssh -v参数,看debug信息验证以上过程

到此,相信大家对“Linux下SSH Session复制功能的实现方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!