Site Tools


wiki:ssh-multiplexing

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:

  • First connection to a host creates a “master” socket at ~/.ssh/socket-user@host:port
  • Subsequent commands detect the socket and reuse the connection
  • ControlPersist 10m keeps the master alive for 10 minutes after you disconnect

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:

  • Faster SSH commands (no handshake overhead)
  • Multiple terminals in one session
  • Shared authentication and agent forwarding

Disable multiplexing for a single command:

$ ssh -o ControlMaster=no user@host

On slow networks, multiplexing is especially noticeable.

wiki/ssh-multiplexing.md · Last modified: by 127.0.0.1