Table of Contents

SSH Multiplexing

Connection multiplexing lets multiple SSH commands and shells reuse a single connection, avoiding the overhead of establishing new connections repeatedly. Configure in ~/.ssh/config:

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

How it works:

Practical example:

$ ssh host1          # creates master socket
$ # (in another terminal or background)
$ ssh host1 "ls"     # reuses socket, no new connection
$ scp file host1:/   # reuses socket
$ ssh -O check host1 # check if master exists

Per-host configuration:

Host work
    HostName work.example.com
    ControlMaster auto
    ControlPath ~/.ssh/socket-work-%r@%h:%p
    ControlPersist 30m

Control commands:

$ ssh -O check host       # check if master is running
$ ssh -O exit host        # close master socket
$ ssh -O forward -L 8000:localhost:3000 host  # add forwarding to existing master
$ ssh -O cancel -L 8000:localhost:3000 host   # remove forwarding

Benefits:

Disable multiplexing for a single command:

$ ssh -o ControlMaster=no user@host

On slow networks, multiplexing is especially noticeable.