How to Use SSH Multiplexing on Ubuntu Linux

SSH multiplexing allows you to reuse an existing SSH connection for subsequent sessions to the same host, drastically reducing the time it takes to establish new connections. This guide covers how to enable, configure, and manage SSH multiplexing on Ubuntu Linux, allowing you to speed up multiple terminal sessions, SFTP file transfers, and automation tools like Ansible.

What is SSH Multiplexing?

By default, every time you open a new SSH session, your system must perform a cryptographic handshake, authenticate your user, and establish a secure channel. This process can take anywhere from a fraction of a second to several seconds, depending on network latency.

SSH multiplexing bypasses this overhead. The first connection you establish (the “master” connection) creates a Unix socket. Subsequent connections (the “slave” connections) piggyback on this existing socket, bypassing authentication and handshaking entirely for near-instantaneous connections.

Step 1: Create the Sockets Directory

First, create a dedicated directory to store the socket files that SSH will use to manage the multiplexed connections. Open your terminal and run:

mkdir -p ~/.ssh/sockets

Set the correct permissions to ensure this directory remains secure:

chmod 700 ~/.ssh/sockets

Step 2: Configure SSH Multiplexing

To enable multiplexing, you need to modify your local SSH configuration file. Open the file using your preferred text editor:

nano ~/.ssh/config

If the file does not exist, this command will create it. Add the following configuration block to the file:

Host *
    ControlMaster auto
    ControlPath ~/.ssh/sockets/%r@%h:%p
    ControlPersist 10m

Here is what these directives mean: * Host *: Applies these rules to all remote hosts. You can replace the asterisk with specific domain names or IP addresses if you only want multiplexing enabled for specific servers. * ControlMaster auto: Tells SSH to look for an existing master connection socket. If one exists, it uses it; if not, it establishes a new master connection. * ControlPath: Defines the path where the socket files will be stored. The variables represent the remote username (%r), host (%h), and port (%p). * ControlPersist 10m: Keeps the master connection open in the background for 10 minutes after you close your last active terminal session. This ensures that if you reconnect within 10 minutes, you still benefit from the speed increase.

Save and close the file (in Nano, press Ctrl+O, Enter, and then Ctrl+X).

Step 3: Test and Verify the Speed Improvement

To see the performance difference, you can measure the time it takes to run a simple command on your remote server before and after a master connection is established.

Run the following command to establish the initial master connection and measure the time:

time ssh user@remote_host exit

Run the same command a second time:

time ssh user@remote_host exit

The second command should run almost instantaneously because it reuses the socket created by the first connection, eliminating the key exchange and authentication steps.

Step 4: Managing Multiplexed Connections

You can manage your active multiplexed connections using the -O flag in SSH.

To check the status of a master connection:

ssh -O check user@remote_host

To request the master connection to exit gracefully (it will wait for all slave connections to close):

ssh -O exit user@remote_host

To force the master connection to close immediately, terminating all slave connections:

ssh -O stop user@remote_host