ControlMaster is the OpenSSH option that keeps one master connection open and routes every later session to the same server through it. With ControlMaster auto, a ControlPath for the socket and ControlPersist to keep it alive after you log out, the next ssh, scp or sftp skips the handshake and authentication entirely.
How SSH ControlMaster works: one connection, many sessions
OpenSSH listens on TCP port 22 by default, and without multiplexing every ssh, scp or sftp opens a fresh connection: TCP setup, key exchange and full authentication. With ControlMaster, the first client becomes the master and leaves a Unix socket on disk. Later clients to the same destination find that socket and ask for a new channel inside the already authenticated connection.
You feel the difference in three places: several tabs open to the same server, scripts that fire off many ssh host command calls in a row, and scp transfers, which since OpenSSH 9.0 use the SFTP protocol by default and ride the shared channel just the same. If your key has a passphrase or the server asks for a second factor, you are only prompted when the master opens.
Setting ControlMaster auto, ControlPath and ControlPersist in ~/.ssh/config
Three ssh_config directives turn on multiplexing in any desktop or server OpenSSH:
mkdir -p ~/.ssh/sockets
chmod 700 ~/.ssh ~/.ssh/sockets
# ~/.ssh/config
Host *
ControlMaster auto
ControlPath ~/.ssh/sockets/%C
ControlPersist 10m
ServerAliveInterval 30
- ControlMaster auto: use a valid socket if one exists; otherwise this connection becomes the master. Other values:
yes(always listen as master),no(the default: never creates a master but still connects to an existing one),askandautoask(confirm through ssh-askpass). - ControlPath ~/.ssh/sockets/%C: the socket path.
%Cis a hash of the local host, remote host, port and user, so each combination gets its own master and the path stays short. The classic%r@%h:%pworks too, but produces longer names. - ControlPersist 10m: the master moves to the background when the first session ends and stays up for ten idle minutes.
yeskeeps it untilssh -O exit;noties it to the session that created it. - ServerAliveInterval 30: not part of multiplexing, but it spots a master whose TCP connection has died much sooner.
To enable it only for specific machines, replace Host * with their aliases. Every option is documented in the ssh_config manual page.
ssh -O commands to manage the ControlMaster master connection
The -O flag sends a command to the master over the socket without opening a new session:
ssh -O check web1 # is a master running? shows its PID
ssh -O exit web1 # shut down the master and its sessions
ssh -O stop web1 # refuse new sessions; open ones continue
ssh -O forward -L 8080:localhost:80 web1 # add a forward to the master
ssh -O cancel -L 8080:localhost:80 web1 # remove it
ssh -S none web1 # standalone connection, no socket
ssh -O exit is the one you will reach for most: it closes the master after you rotate keys, or when the network changed and new sessions hang. ssh -S none is how you prove a new configuration authenticates on its own instead of inheriting the master's session.
4 common mistakes when configuring ControlMaster in ~/.ssh/config
- The socket directory does not exist. OpenSSH will not create
~/.ssh/sockets; the client reportsunix_listener: cannot bind to pathand carries on without multiplexing. Create it withmkdir -pand mode 700. - The path exceeds the Unix socket length limit. Long hostnames with
%r@%h:%ptriggertoo long for Unix domain socket.%Cyields a fixed-length hash and avoids it. - The socket sits in a shared directory. Anyone who can open the socket rides your authenticated session without a key. Never use
/tmpon multi-user machines: keep the socket under~/.ssh, mode 700. - The master is alive but its connection is dead. After the laptop sleeps or the network changes, the TCP link is gone but the process remains, so new sessions hang.
ssh -O exit hostclears it; if it does not answer, delete the socket file.
Desktop ControlMaster versus Mosh and tmux on iOS and Android
Mosh runs over UDP, on ports 60000-61000 by default, and starts mosh-server through an initial SSH connection. It solves a problem ControlMaster ignores: keeping the session alive across network changes, which is what a phone goes through all day.
| What is measured | ControlMaster (desktop OpenSSH) | Mosh + tmux on mobile |
|---|---|---|
| What gets reused | The authenticated SSH connection for new sessions | The remote session; tmux keeps processes and windows |
| Transport | TCP, port 22 by default | UDP, ports 60000-61000 after an SSH bootstrap |
| Wi-Fi to mobile data switch | The master's TCP connection breaks | The Mosh session carries on |
| Port forwarding | Yes, even added live with ssh -O forward | No: Mosh does no port or X11 forwarding |
termi1, a mobile SSH and Mosh terminal, follows the second model: it keeps the session through Mosh when you move from Wi-Fi to data and uses tmux so whatever you left running is still there when you come back. If you manage a bastion from your phone and that bastion uses ControlMaster towards other hosts, the config block above applies on that server unchanged. termi1's keyboard has Esc, Ctrl, Tab and arrow keys for tmux, and in Review mode the AI types ssh -O exit web1 into the prompt without pressing Enter. The terminal is free from termi1 on the App Store.
Frequently asked questions
What does ControlMaster auto do in SSH?
ControlMaster auto tells ssh to reuse an existing master connection when it finds the socket named in ControlPath, and to turn the current connection into the master when none exists. It is the practical value for Host *, since you never have to start a master manually with ssh -M.
How do I close a hung ControlMaster connection?
A hung ControlMaster connection is closed with ssh -O exit followed by the host alias, which tells the master to terminate through its socket. If the master no longer responds, delete the socket file named in ControlPath and the next connection will create a fresh one.
What value should I use for ControlPersist?
ControlPersist accepts yes, no or a duration such as 10m. A few minutes covers a burst of one-off commands without leaving masters open for days; yes keeps the master until ssh -O exit, handy in scripts and risky on machines shared by several people.
Is SSH ControlMaster safe to use?
SSH ControlMaster is safe when the socket lives in a directory only you can access, such as ~/.ssh with mode 700. Anyone able to open that socket reaches the server without authenticating, so it must never sit in /tmp on shared machines or in synced folders.