Linux / Unix: OpenSSH Multiplexer To Speed Up OpenSSH Connections
How can I multiplex SSH sessions by setting up a master session and then having subsequent sessions go through the master to speed up my ssh connection on a Linux or Unix-like operating systems?
Multiplexing is nothing but send more than one ssh connection over a single connection. OpenSSH can reuse an existing TCP connection for multiple concurrent SSH sessions. This results into reduction of the overhead of creating new TCP connections. First, you need to set a ControlMaster to open a Unix domain socket locally. [donotprint][/donotprint]Rest of all your ssh commands connects to the ControlMaster via a Unix domain socket. The ControlMaster provides us the following benefits:
Use existing unix socket
No new TCP/IP connection
No need to key exchange
No need for authentication and more
How to setup up multiplexing
Edit $HOME/.ssh/config, enter: vi ~/.ssh/config Append the following configuration:
Here is another example:
Save and close the file. Where,
Host * or Host server1 : Start ssh configuration.
HostName server1.sxi.io : The real hostname
ControlPath ~/.ssh/ssh-mux-%r@%h:%p : Specify the path to the control unix socket used for connection sharing as described above. The variables ‘%r’, ‘%h’, ‘%p’ refer to remote ssh username, remote ssh host, and remote ssh port respectively. You need to set all of these three variables.
ControlMaster auto : Enables the sharing of multiple sessions over a single network connection. When set to yes, ssh will listen for connections on a control socket specified using the ControlPath argument. When set to auto, ssh will try to use a master connection but fall back to creating a new one if one does not already exist.
ControlPersist 10m : Specifies that the master connection should remain open in the background for 10 minutes. With no client connections, the backgrounded master connection will automatically terminate after it has remained idle for 10 minutes. If set to yes, then the master connection will remain in the background indefinitely (until killed or closed)
Use any one of the following command to verify that Multiplexer is working properly: $ lsof -U | grep master OR $ ssh -O check firstname.lastname@example.org Sample outputs:
Can I tell master connection not to accept further multiplexing requests?
Yes, use the following syntax: $ ssh -O stop host $ ssh -O stop email@example.com Pass the exit option instead of stop to cancel all existing connections, including the master connection: $ ssh -O exit host $ ssh -O exit firstname.lastname@example.org
How do I the port forwarding?
The syntax is as follows to forward port 3128 on the local host to port 3128 on the remote host using -L: ssh -O forward -L 3128:localhost:3128 v.server1 You can also specifies the location of a control socket for connection sharing: ssh -O forward -L 3128:localhost:3128 -S $HOMEemail@example.com:22 v.server1
The main advantage with SSH multiplexing is that the overhead of creating new TCP connections is removed. SSH client activities that repeatedly open new connections can be significantly speed up using multiplexing. See ssh_config man page for more information.
The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.