Multiplexing allows a single SSH connection to be shared by multiple sessions. The connection could be set to persist for a specified time even if you have already logged out from the SSH server and then reused for fast reconnection to the server.

Reconnecting to an SSH server with an already established session does not require reauthentication. It speeds up operations that require multiple SSH connections to a single server, usually when automating tasks via SSH.

You can use multiplexing and share or reuse your SSH connection by configuring your SSH client. It does not require any configuration on the server.

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. Check for existing control file on local host.
    $ ls ~/.ssh/muxmasters
  8. Log in to a remote host.
    $ ssh 192.168.111.159 -- hostname
    user@192.168.111.159's password: 
    remotehost
  9. Check for created control file to confirm.
    $ ls ~/.ssh/muxmasters
    598a7155ff90f076057fb265730c6ffe5997d4bb
  10. Log in again to the same host to test.
    $ ssh 192.168.111.159 -- hostname
    host

    Should be without password and very fast as long as you log in again within the controlpersist time value.

Discuss the article:

Comment anonymously. Login not required.

Share!