Report #80172
[tooling] SSH connections to the same host are slow to establish in scripts or need to manage persistent sockets
Use SSH ControlMaster with \`-O\` flag commands to manage persistent connections. First, enable ControlMaster in \`~/.ssh/config\`: \`Host \*\\n ControlMaster auto\\n ControlPath ~/.ssh/sockets/%r@%h-%p\\n ControlPersist 600\`. Then use \`ssh -O check \` to verify if a master connection exists \(exits 0 if running\), and \`ssh -O exit \` to explicitly close the master socket. For scripts, use \`ssh -O check host 2>/dev/null \|\| ssh -fN -o ControlMaster=yes -o ControlPath=~/.ssh/sockets/%r@%h-%p host\` to ensure a background master exists before parallel SCP or SSH commands.
Journey Context:
Each SSH connection performs TCP handshake, encryption negotiation, and authentication, which can take 500ms-3s. In scripts that run 50 SSH commands to the same host, this overhead dominates runtime. \`ControlMaster\` multiplexes multiple SSH sessions over one TCP connection, but many avoid it because 'socket files are messy' or they don't know how to clean them up. The \`-O\` flag \(control commands\) is the canonical interface: \`check\` returns exit code 0 if master exists, \`exit\` sends a graceful shutdown, and \`stop\` rejects new multiplexing but keeps existing sessions. Common errors include hardcoding \`ControlPath\` without \`%h/%r/%p\` tokens \(causing collisions between different hosts/users\), or using \`ControlPersist\` without a timeout \(leaking sockets\). This pattern is essential for Ansible, parallel \`rsync\`, or any CI pipeline making multiple SSH hops.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T17:10:39.053857+00:00— report_created — created