如何在Linux上使用OpenSSH Multiplexer加速OpenSSH连接
本文将通过设置主会话,然后通过使用多路复用器在Linux上加快SSH连接的速度来进行后续会话,从而帮助我们理解多路SSH会话。
多路复用
复用不过是通过SSH的单个连接发送更多或更多消息,并且可以将现有TCP/IP连接重用于多个并发SSH连接。这将减轻在服务器上创建新的TCP连接的负担。
SSH连接上Multiplexer的优势。
它使用现有的*inx套接字进行连接。
IT使用现有的TCP/IP连接,不再使用新的TCP/IP。
没有更多的密钥交换。
无需身份验证。
配置多路复用
如果您的主目录中的.ssh文件夹中不存在该配置,请使用权限600创建该配置:仅对您可读和可写。
编辑用户的.ssh/config–
# vi ~/.ssh/config ControlMaster auto ControlPath ~/.ssh/master-%r@%h:%p.socket ControlPersist 30m
样本配置文件–
Host * ControlPath ~/.ssh/master-%r@%h:%p ControlMaster auto ControlPersist 30m
说明
Host * or Host client : This start SSH configuration. ControlPath ~/.ssh/ssh-mux-%r@%h:%p : This specifies the path to the control *inx socket to be used for connection and sharing is as described in the above. The variables ‘%r’ - remote ssh username ‘%h’ - remote ssh host ‘%p’ - remote ssh port You need to set all of these three variables for this option. ControlMaster auto: This enables the sharing of multiple sessions over a single network connection. When this set to yes, the SSH will listen for connections on a control socket specified using the ControlPath argument. When this set to auto, ssh will try to use a master connection, but the connection falls back to creating a new one connection, if one does not exist.ControlPersist 30m: This option specifies that the master connection should remain open in the background for 30 minutes. If the connection is with no clients, then the backgrounded master connection will automatically terminate after it had remained idle for 30 minutes.
如何连线
使用以下命令从客户端计算机连接到服务器。
#ssh root@192.168.2.225 The authenticity of host '192.168.2.225 (192.168.2.225)' can't be established. RSA key fingerprint is f7:c8:62:c9:6f:02:50:8e:14:cd:3a:95:ad:b1:67:af. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.2.225' (RSA) to the list of known hosts. root@192.168.2.225's password: Last login: Fri Apr 22 13:26:56 2016 from 192.168.1.84
如何验证多路复用器是否正常工作?
转到SSH服务器并运行以下命令。
# lsof -U | grep master ssh 69518 root 4u unix0xffff8801378f7580 0t0 607468 /root/.ssh/master-root@192.168.2.225 ssh 69518 root 5u unix0xffff880139986b80 0t0 607482 /root/.ssh/master root@192.168.2.225
要么
我们可以用另一个命令检查
# ssh -O stop 192.168.2.225 Master running (pid=69518) #
如果我们使用与同一台服务器的OpenSSH连接运行许多终端或脚本,则可以使用多路复用来加快它们的运行速度,多路复用使第一个连接成为主服务器,并允许其他已连接的服务器将其TCP连接共享给服务器。