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.

Steps to share and reuse SSH connection via multiplexing:

  1. Launch terminal.
  2. Open SSH client configuration file for your user using your preferred text editor.
    $ vi ~/.ssh/config
  3. 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.*
  4. Enable multiplexing for the selected host(s).
    host *
        controlmaster auto
  5. Set how long do you want the multiplexing session to persist.
    host *
        controlmaster auto
        controlpersist 10m
  6. 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
  7. Establish the master connection by connecting to your server.
    $ ssh 192.168.111.159 -- hostname
    user@192.168.111.159's password: 
    remotehost
  8. 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.

  9. 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.

Discuss the article:

Comment anonymously. Login not required.