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.
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
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 10m
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.
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
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
ssh_config man page for more information.