← index

SSH multiplexing, and why my scripts got faster

A deploy script was spending most of its time doing nothing. Each of the forty commands opened its own connection, and every handshake cost about half a second.

Three lines in ~/.ssh/config fixed it:

Host *
    ControlMaster auto
    ControlPath ~/.ssh/cm-%r@%h:%p
    ControlPersist 10m

The first connection opens a socket; the rest reuse it. The script went from fifty seconds to under nine.

Two things to watch. The socket path has a length limit on some systems, so keep the directory short. And ControlPersist means a connection lingers after your shell exits, which surprises you the first time a firewall rule changes and nothing seems to take effect.

To close them by hand: ssh -O exit host.