SSH (Secure Shell) is a powerful tool for secure remote access and file transfer. Beyond its basic functionality, SSH offers advanced features like multiplexing, which allows multiple SSH sessions to share a single network connection. This can speed up operations, reduce connection overhead, and simplify session management.
Multiplexing in SSH can be particularly useful when performing multiple operations on a remote server. Instead of establishing a new connection for each operation, you can reuse an existing connection, making tasks faster and more efficient. This is especially beneficial in scripts or automated tasks where multiple SSH commands might be run in quick succession.
SSH multiplexing is achieved using the ControlMaster, ControlPath, and ControlPersist configuration options in the SSH client. These options allow you to set up and manage master and client sessions.
Related: How to accelerate SSH authentication
Steps to share and reuse SSH connection via multiplexing:
- Launch terminal.
- Open SSH client configuration file for your user using your preferred text editor.
$ vi ~/.ssh/config
- Select the host(s) that you want to implement multiplexing.
host *
* applies configuration for all hosts. Other possible host options:
example.com *.example.com 192.168.100.10 192.168.100.*
- Enable multiplexing for the selected host(s).
host * controlmaster auto
- Set how long do you want the multiplexing session to persist.
host * controlmaster auto controlpersist 10m
- Set path and filename for controlmaster file.
host * controlmaster auto controlpersist 10m controlpath ~/.ssh/muxmasters/%C
Create folder if doesn't exist.
$ mkdir -p ~/.ssh/muxmasters
- Establish the master connection by connecting to your server.
$ ssh 192.168.111.159 -- hostname user@192.168.111.159's password: remotehost
- Check for the existence of the socket file specified in ControlPath to verify.
$ ls ~/.ssh/muxmasters 598a7155ff90f076057fb265730c6ffe5997d4bb
If multiplexing is active, you'll see the socket file listed.
- Open multiplexed sessions by opening new SSH sessions to the same host while the master connection is active.
$ ssh 192.168.111.159 -- hostname host
These sessions will use the master connection without the need for re-authenticating. It should also be very fast as long as you log in within the controlpersist period.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.