C.W.K.
Stream
Lesson 09 of 12 · published

ControlMaster — Connection Multiplexing

~18 min · controlmaster, controlpath, controlpersist, multiplexing

Level 0Pinger
0 XP0/101 lessons0/12 achievements
0/150 XP to next level150 XP to go0% complete

The optimization that compounds

ControlMaster reuses one TCP+SSH connection for many sessions to the same host. Without it, every ssh, scp, and rsync to a host opens a fresh connection — 200–500 ms of TCP handshake plus SSH negotiation each time. With it, the first connection becomes the master, and every subsequent connection rides that same channel — nearly instant.

This compounds dramatically for scripted operations. A loop that SSHs to the same host 20 times goes from "painfully slow" to "essentially free."

The three directives

  • ControlMaster auto — first connection becomes master; later ones reuse it.
  • ControlPath ~/.ssh/sockets/%r@%h-%p — where to store the socket file. %r = remote user, %h = host, %p = port. Make sure the directory exists.
  • ControlPersist 10m — keep the master alive for 10 minutes after the last session closes. Subsequent ssh within that window is instant.

When something goes weird

Stale sockets are the only real failure mode. If a host gets stuck (network change, bug), you can either remove the socket file by hand or use ssh -O exit hostname to gracefully close the master.

Code

ControlMaster setup·bash
# Create the sockets directory once
mkdir -p ~/.ssh/sockets
chmod 700 ~/.ssh/sockets
Add to ~/.ssh/config·ssh-config
Host *
    ControlMaster auto
    ControlPath ~/.ssh/sockets/%r@%h-%p
    ControlPersist 10m
    # ...other Host * settings
Manage masters·bash
# Open a master and detach
ssh -fN office

# Check master status
ssh -O check office
# Output: Master running (pid=12345)

# Gracefully exit a master
ssh -O exit office

# If a socket is stale and ssh -O exit fails:
rm ~/.ssh/sockets/you_username@192.168.1.100-22

External links

Exercise

Set up ControlMaster as in the snippets above. Then time it: open a fresh terminal and run time ssh office uptime. Note the seconds. Run it again — should be substantially faster (sub-second). Run ssh -O check office while a session is open to see the master report. Then ssh -O exit office and time again — should be slow again because the master is gone.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.