This article will help us to understand the multiplex SSH sessions by setting up a master session, then having subsequent sessions by using multiplexer to speed up the SSH connection on Linux.
Multiplexing is nothing but send more than or more over a single connection of SSH and can reuse the existing TCP/IP connection for multiple concurrent SSH connection. This will reduce the load of creating new TCP connections on the server.
Advantages of Multiplexer on SSH Connection.
If the config does not exist in the .ssh folder in your home directory, create it with permissions 600: readable and writable only for you.
Edit the .ssh/config of the user –
# vi ~/.ssh/config ControlMaster auto ControlPath ~/.ssh/master-%r@%h:%p.socket ControlPersist 30m
Sample Config file –
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.
From a client machine connect to the server with below commands.
#ssh firstname.lastname@example.org 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. email@example.com's password: Last login: Fri Apr 22 13:26:56 2016 from 192.168.1.84
Go to the SSH server and run the below command.
# lsof -U | grep master ssh 69518 root 4u unix0xffff8801378f7580 0t0 607468 /firstname.lastname@example.org ssh 69518 root 5u unix0xffff880139986b80 0t0 607482 /root/.ssh/master email@example.com
We can check with another command
# ssh -O stop 192.168.2.225 Master running (pid=69518) #
If we run a lot of terminal or scripts using OpenSSH connections to the same server, you can speed them by using the multiplexing, which makes first connection as the master and allowing the others connected to share its TCP connection to the server.