Administrators and developers often open several SSH sessions to the same Linux server during a workday. Without connection sharing, each new session performs another connection setup and authentication exchange. OpenSSH’s ControlMaster feature can reduce that overhead by allowing multiple sessions to reuse one already-authenticated network connection.
What Is SSH ControlMaster?
SSH multiplexing allows a master connection to carry additional sessions to the same destination. The first connection becomes the master; later SSH commands can attach to its local Unix-domain socket instead of repeating the full connection setup. ControlMaster is part of OpenSSH, so a separate third-party client is not required on systems with a sufficiently recent OpenSSH installation.
Configure Multiplexing
Open or create ~/.ssh/config and add a host-specific configuration. A broad example is shown below; many administrators prefer to scope it to selected hosts rather than enable it globally.
Host my-server
HostName server.example.com
User admin
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 10m
The options have distinct roles: ControlMaster auto reuses an existing master or creates one when needed; ControlPath defines where the socket is stored; and ControlPersist controls how long the master remains available after the initial session closes.
Create a Secure Socket Directory
Create the socket directory before using the configuration and restrict its permissions:
mkdir -p ~/.ssh/sockets
chmod 700 ~/.ssh/sockets
The socket is sensitive because anyone with access to it may be able to use the authenticated master connection. Keep the directory private and avoid placing sockets in a shared temporary directory.
Check and Stop a Master Connection
OpenSSH provides control commands for managing a master connection. To check whether one is active:
ssh -O check my-server
To stop the master connection:
ssh -O stop my-server
Stopping the master also interrupts sessions that depend on it. Check for active jobs before doing so.
Benefits and Trade-offs
- Faster repeated sessions: Additional commands can attach quickly without repeating the full handshake.
- Lower connection overhead: Several sessions can share one TCP connection to the host.
- Useful for automation: Scripts that execute many SSH commands may spend less time connecting.
- Socket security matters: A local user who can access the socket may be able to reuse the authenticated connection.
- Audit behaviour changes: The server may see one underlying connection even when several logical sessions use it.
Avoid unlimited persistence on shared or unattended computers. Test the configuration with your automation tools before rolling it out broadly, especially where session auditing or connection lifetime requirements are strict.
Choosing a ControlPersist value
ControlPersist decides how long the master connection stays open after the last session using it closes. A short value, such as a few minutes, keeps the convenience for a burst of activity while limiting how long an unattended authenticated connection lingers. A longer value suits a long working session with frequent commands, but it also extends the window in which someone with access to the socket could reuse the connection. There is no single correct value; it depends on how you work and how sensitive the server is. A reasonable approach is to start short, notice whether you keep waiting for new connections, and lengthen it only if the delay is genuinely getting in your way. Avoid leaving a master connection open indefinitely on a shared machine or a laptop that might be left unattended.
When multiplexing causes trouble
Multiplexing is usually invisible, which is exactly why its failures can be confusing. If the master connection drops because of a network interruption, the sessions that depend on it can hang or fail together. Some tools that open many parallel connections may behave unexpectedly when they share one underlying connection, especially if they assume each session is independent. Port forwarding set up through a multiplexed session can also behave in ways that surprise users who expect each session to carry its own forwards. When something behaves strangely, try the same command without multiplexing to see whether the problem disappears. If it does, the issue is in the shared connection, not in the command itself, and you can narrow it down from there.
Using ControlMaster with automation tools
Scripts and automation tools that run many SSH commands in sequence benefit the most from multiplexing, because the time saved on each connection adds up. Before relying on it in automation, test the exact commands your tools run, including file transfers and commands that request a terminal. Some tools manage their own connections and may conflict with a master connection that they did not create. Make sure the socket directory exists before the automation runs, and that the account running the automation has permission to use it. Add a check that reports whether the master connection is active, so a failed master does not produce a confusing error halfway through a job. For scheduled jobs, consider whether a persistent master connection is appropriate at all, since nobody is present to notice if it stops working.
Scoping multiplexing to selected hosts
Enabling multiplexing for every host is convenient, but it is not always wise. A server that handles sensitive data, a shared jump host, or a machine whose audit trail must show each connection separately may deserve its own settings. Scoping the configuration to selected hosts keeps the convenience where it helps and avoids surprises where it does not. In the SSH configuration file, place host-specific blocks before any broad catch-all block, because the first matching value for each option wins. Test the scoped configuration by connecting to a host that should use multiplexing and one that should not, and confirm that each behaves as intended. This small check prevents a global setting from quietly applying to a host you meant to exclude.
Cleaning up stale sockets
A socket file can be left behind when a master connection ends abnormally, for example after a network failure or a forced shutdown. A stale socket can cause new connections to fail with an error about a connection that no longer exists. The fix is usually to remove the stale socket file and let a fresh master connection be created. Check the socket directory occasionally and remove files that no longer correspond to a running connection. Do not delete socket files while sessions are actively using them. If stale sockets appear frequently, it may be a sign of an unstable network or a ControlPersist value that does not match how the connection is actually used, and adjusting either can reduce the problem.
Conclusion
SSH ControlMaster is a built-in OpenSSH feature that can make repeated administration tasks more responsive. A small configuration block and a private socket directory are usually enough to get started. Use it selectively, understand how the master connection is shared, and follow the OpenSSH ControlMaster documentation for the exact options supported by your client.

Komentar (0)
Belum ada komentar. Jadilah yang pertama berkomentar.