Strovent SystemsSign in
Reference

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 auto uses a master if one is running, and otherwise starts a new connection that becomes the master. yes always makes a new master; no, the default, only uses one if it exists. ask and autoask work like yes and auto but confirm each new session with ssh-askpass.
  • ControlPath is the socket file. %C is 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. Without ControlPath, nothing is shared.
  • ControlPersist keeps the master running in the background after the session that started it ends. 10m closes it after 10 minutes with no sessions; yes keeps it until you stop it. With the default no, the master is not put in the background: it lives in the first ssh you 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.
  • check reports whether a master is running.
  • exit closes the master and every session in it.
  • stop stops the master from accepting new sessions, and lets the ones already open finish.
  • forward adds a port forward to the running connection, for example ssh -O forward -L 8080:localhost:80 host.example.com, and cancel removes 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 exit first.
  • 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. MaxSessions in sshd_config defaults 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 -q hides it.
  • Master running (pid=...) and Exit request sent. come from -O check and -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.

Sources

Spotted a mistake? Email [email protected].