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. 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:
Append the following configuration:
Host * ControlMaster auto ControlPath ~/.ssh/master-%r@%h:%p.socket ControlPersist 30m
Here is another example:
Host server1 HostName server1.cyberciti.biz Port 2222 ControlPath ~/.ssh/ssh-mux-%r@%h:%p ControlMaster auto ControlPersist yes
Save and close the file. Where,
- Host * or Host server1 : Start ssh configuration.
- HostName server1.cyberciti.biz : 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)
How do I use it?
Simply start running ssh commands:
$ ssh user@host
$ ssh email@example.com
$ ssh firstname.lastname@example.org
How do I verify that Multiplexer is working?
Use any one of the following command to verify that Multiplexer is working properly:
$ lsof -U | grep master
$ ssh -O check email@example.com
Can I tell master connection not to accept further multiplexing requests?
Yes, use the following syntax:
$ ssh -O stop host
$ ssh -O stop firstname.lastname@example.org
Pass the exit option instead of stop to cancel all existing connections, including the master connection:
$ ssh -O exit host
$ ssh -O exit email@example.com
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 $HOMEfirstname.lastname@example.org: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.