SSH Multiplexing with ControlMaster: One Connection, Many Sessions
Three lines in ~/.ssh/config let every SSH session after the first reuse an authenticated connection — the setup, the verification, and the failure modes.
07 Jul 2025, 03:46 UTC

Opening a fifth terminal tab to the same build server means waiting through the same sequence again: TCP handshake, key exchange, authentication. A script that calls scp or rsync a dozen times pays that cost on every iteration. The takeaway up front: three lines in ~/.ssh/config make every session after the first attach to an already-authenticated connection, so only the first one pays the setup cost.
What the master socket actually does
SSH multiplexing runs several logical sessions over a single TCP connection. The first connection — the master — does the handshake, key exchange, and authentication as usual, then leaves a Unix domain socket on disk. A Unix domain socket is a file that local processes use to talk to each other; here, it acts as a door into the master connection. Later ssh, scp, and rsync invocations find that socket and open a new channel over the existing TCP connection instead of building their own. No new handshake, no new authentication prompt.
The config that does the work
Add this to ~/.ssh/config on your local machine, adjusting the Host pattern and the user/host placeholders to your environment:
Host dev-*
ControlMaster auto
ControlPath ~/.ssh/cm/%r@%h-%p
ControlPersist 10m
ServerAliveInterval 30
ServerAliveCountMax 3
What each line does:
- ControlMaster auto — the first connection becomes the master automatically; later ones reuse it whenever the socket is live.
- ControlPath — where the socket file lives.
%r,%h, and%pexpand to the remote user, host, and port, so each target gets its own socket. - ControlPersist 10m — after your last session closes, the master stays in the background for ten minutes, then exits. Without it, the master dies with the last session and you lose the reuse window between commands.
- ServerAliveInterval / ServerAliveCountMax — send a keepalive every 30 seconds and give up after three missed replies. This keeps NAT devices and firewalls from silently dropping the idle TCP connection (more on that below).
Create the socket directory first, in your local shell — no root required:
mkdir -p ~/.ssh/cm
chmod 700 ~/.ssh/cm
The 700 matters: the socket is an authenticated door, and anyone who can open it can reuse your session without re-authenticating. Keeping it in a directory only you can read is the whole defense.
One path-length caveat: Unix domain socket paths are capped at roughly 104–108 characters depending on the OS. If %r@%h-%p pushes you over, use %C instead — a short hash of the connection details that OpenSSH provides for exactly this purpose.
All of this is long-standing OpenSSH client behavior; any release from the past decade supports multiplexing and ControlPersist. Run ssh -V to confirm what you are on, and check your local ssh_config(5) man page if you are on something unusual.
Confirming the second session actually reuses it
Connect once as usual, then check the master from your local shell:
ssh -O check admin@dev-01
If a master is running for that host, this confirms it is alive and reports the master's OpenSSH version; if not, it fails with an error about the control socket. Then measure the difference:
time ssh admin@dev-01 true
Run it twice. The first run pays the handshake and authentication; the second should return noticeably faster because it only opens a channel. The gap grows with link latency — marginal on a LAN, potentially seconds on a high-latency VPN or intercontinental link. Everything here runs on your local machine; nothing on the server needs to change.
Where multiplexing bites back
- Stale sockets. If the master's TCP connection is dropped out from under it — NAT timeout, network switch, laptop sleep — the socket file can outlive the dead connection, and later sessions hang or fail.
ssh -O checktells you the master is gone; at that point removing the socket file (rm ~/.ssh/cm/<socket>) is safe — it is a local endpoint, not data — and the next connection builds a fresh master. - Unbounded persist.
ControlPersist yeskeeps the master alive indefinitely, which leaves a backgroundsshdprocess on the server until you clean it up. A bounded value like10mgives you the reuse window without the lingering process. - Server-side session cap. OpenSSH's
sshdlimits how many channels one connection can carry (MaxSessions, 10 by default). Heavy parallel fan-out over a single master can hit that ceiling; excess sessions fail rather than degrade. - Auth bypass cuts both ways. Because later sessions skip authentication entirely, an existing master keeps working even after you rotate keys or revoke access — another argument for a bounded
ControlPersist.
Multiplexing also only pays off when you repeatedly connect to the same host. One-off connections to many different targets gain nothing, since each needs its own master anyway.
A low-risk way to adopt it
Scope the config block to a single host alias you use often, run the time comparison above, and keep ControlPersist bounded. When you are done for the day, tear the master down explicitly:
ssh -O exit admin@dev-01
That closes the shared TCP connection — which means any live sessions multiplexed over it drop too, so run it when nothing active is attached. If the timing check shows no meaningful gain on your links, delete the config block; nothing outside the socket directory and the background master process needs undoing.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.