SSH ControlMaster: reuse one connection with ControlPath and ControlPersist
ControlMaster lets later ssh, scp and sftp commands reuse one open SSH connection, with no new login. Set ControlMaster, ControlPath and ControlPersist.
Connection multiplexing lets one SSH connection carry many sessions. The first ssh to a host logs in and becomes the master. Later ssh, scp and sftp commands to the same host find the master through a socket file and open their session inside its connection. They skip the TCP connection, the key exchange and the login, so they start at once and do not ask for a password again.
Turn it on
Make a folder for the sockets that only you can write to:
mkdir -m 700 ~/.ssh/cm
Then add three lines to ~/.ssh/config, for one host or for all of them:
Host host.example.com
ControlMaster auto
ControlPath ~/.ssh/cm/%C
ControlPersist 10m
ControlMaster autouses a master if one is running, and otherwise starts a new connection that becomes the master.yesalways makes a new master;no, the default, only uses one if it exists.askandautoaskwork likeyesandautobut confirm each new session withssh-askpass.ControlPathis the socket file.%Cis a hash of the local host name, remote host, port, user name and jump host, so each combination gets its own master and the path stays short. WithoutControlPath, nothing is shared.ControlPersistkeeps the master running in the background after the session that started it ends.10mcloses it after 10 minutes with no sessions;yeskeeps it until you stop it. With the defaultno, the master is not put in the background: it lives in the firstsshyou started, so keep that session open while you use the others.
scp and sftp run ssh with your config, so they share the connection too, as do other tools that start ssh, such as git. See Do scp and sftp use ~/.ssh/config? Host aliases, -F and -P.
Check and stop the master
ssh -O sends a command to the running master for that host:
ssh -O check host.example.com
Master running (pid=4242)
ssh -O exit host.example.com
Exit request sent.
checkreports whether a master is running.exitcloses the master and every session in it.stopstops the master from accepting new sessions, and lets the ones already open finish.forwardadds a port forward to the running connection, for examplessh -O forward -L 8080:localhost:80 host.example.com, andcancelremoves one.
ssh -S path names the socket on the command line instead of ControlPath, and -S none turns sharing off for one command. -M starts a master like ControlMaster yes.
What to know before you use it
- Logging in again does not happen. A later session uses the identity, user and authentication of the master. Changing a key or password option on a later command has no effect while the master runs; stop it with
-O exitfirst. - Agent and X11 forwarding belong to the master. The ssh_config manual says sessions share the display and agent forwarded by the master; you cannot forward a different agent per session. See The agent and agent forwarding.
- One connection, one point of failure. If the network drops, every session in it ends together. Keep-alive settings are those of the master. See SSH keep-alives and idle timeouts.
- The server limits sessions per connection.
MaxSessionsinsshd_configdefaults to 10. Past that, new sessions fail; see SSH "ControlPath too long" and other connection-sharing errors. - The socket is a key to your login. Anyone who can open the socket file can start sessions on the connection without authenticating. That is why the manual recommends a folder other users cannot write to.
Messages you will see
Shared connection to host.example.com closed.A session that ran through the master, with a terminal, has ended. The master may still be running. It is information, not an error;ssh -qhides it.Master running (pid=...)andExit request sent.come from-O checkand-O exit.- Errors about the socket path, a refused session or an existing socket are in SSH "ControlPath too long" and other connection-sharing errors.
Related
- The ssh config file
- SSH "ControlPath too long" and other connection-sharing errors
- Do scp and sftp use ~/.ssh/config? Host aliases, -F and -P
- SSH keep-alives and idle timeouts
- Port forwarding: local, remote, dynamic
- SSH: how it works
- SSH "X11 forwarding request failed on channel 0" and "cannot open display" with ssh -X