DEV Community

Nate
Nate

Posted on

When Your Network Changes, Reconnect to SSH Without Reopening Every Terminal

If several SSH terminals stop responding after you switch Wi-Fi networks or connect to a VPN, they may be sharing one SSH connection. OpenSSH connection multiplexing can reuse an authenticated master connection for multiple sessions, which saves repeated handshakes—but it also means those sessions depend on the same underlying network path.

The important distinction: multiplexing does not reconnect existing shells when the network changes. Recovery means retiring the old shared connection and making a new one. First, identify what is shared and whether ending it could interrupt other sessions.

Configure a dedicated host alias

Use a host-specific alias so multiplexing is enabled only where you want it. For example, add this to ~/.ssh/config:

Host work
    HostName server.example.net
    User alice
    IdentityFile ~/.ssh/id_ed25519
    ControlMaster auto
    ControlPath ~/.ssh/control-%C
    ControlPersist 10m
Enter fullscreen mode Exit fullscreen mode

Connect with:

ssh work
Enter fullscreen mode Exit fullscreen mode

ControlMaster auto lets the first connection become the master and later connections reuse it. ControlPersist 10m keeps the master available for ten minutes after its last session closes; it does not keep an interactive shell alive. %C gives the control socket a connection-specific hashed name, helping avoid collisions between different connection settings. For background on host aliases and other SSH options, see practical SSH config examples.

The socket path must be accessible only to your account. The home directory normally provides that protection; if you put sockets in a separate directory, create it with restrictive permissions, such as mkdir -m 700 ~/.ssh/control. Keep the path short enough for the operating system’s Unix-domain socket length limit. If OpenSSH reports that the control path is too long, use a shorter directory, for example under /tmp with a private per-user subdirectory.

This setup is intended for OpenSSH clients on Unix-like systems. Do not assume that every SSH client or platform supports ControlMaster in the same way.

Check the master—not just the terminal

After the network changes, a shell may appear frozen while a new ssh work succeeds. Ask the configured alias whether it has a master connection:

ssh -O check work
Enter fullscreen mode Exit fullscreen mode

If a master is available, OpenSSH reports that it is running and identifies its process ID. If there is no usable control socket, the command reports an error. Exact wording can vary by OpenSSH version.

A successful check is not a test of end-to-end network reachability. It confirms that the local control socket can communicate with a master process; the master’s TCP connection to the server could still be unusable after a Wi-Fi or VPN change. Conversely, a failed check does not by itself prove the server is down. It may mean the socket is missing, stale, or inaccessible.

You can inspect the effective settings for the alias with:

ssh -G work | grep -E '^(controlmaster|controlpath|controlpersist) '
Enter fullscreen mode Exit fullscreen mode

This helps catch a common source of confusion: a terminal may have been opened with a different hostname, user, port, or configuration, and therefore may not use the alias or socket you are checking.

Retire the old connection safely

Before exiting the master, consider whether other terminals are still using it. Multiple shells can share one underlying connection, so terminating the master may affect those sessions. If they are valuable and still responsive, finish or save their work first. A master’s lifetime is separate from each shell’s process lifetime, but the shared network connection is a common dependency.

When it is safe to close, request that the master exit:

ssh -O exit work
Enter fullscreen mode Exit fullscreen mode

Then open a fresh connection:

ssh work
Enter fullscreen mode Exit fullscreen mode

The new command should establish a new master if no reusable one exists. You can check again with ssh -O check work. If -O exit cannot reach the old master because the connection or socket is already gone, do not immediately delete files by pattern: first verify which socket path this alias uses and whether a master process is still running. Removing a socket file while a live master is using it can leave the process running but prevent new clients from finding it. Avoid killing a process by PID unless you have confirmed it belongs to this SSH connection.

If you want to drain a master without accepting new sessions, OpenSSH also supports ssh -O stop work; it stops accepting new multiplexed connections and exits after existing sessions close. Use that when you want current sessions to finish rather than force a quick reset. Check your local ssh(1) manual for the exact behavior supported by your OpenSSH version.

Why keepalives are not the recovery mechanism

ServerAliveInterval and related keepalive settings can help detect an unresponsive connection or prevent some idle-session timeouts. They cannot migrate an established TCP connection onto a new Wi-Fi or VPN route. Keepalives and multiplexing solve different problems: one concerns detecting or avoiding idle disconnects, while the other shares a connection among sessions. See SSH keepalive behavior and configuration for the idle-timeout case.

After a network switch, a stale master may remain long enough for new SSH commands to try using it. If ssh work fails through the master, inspect the master with -O check, close or stop it when safe, and retry. If the fresh connection also fails, troubleshoot the new route, DNS, VPN access, host, and port separately; multiplexing cannot fix a server that is no longer reachable. For connections through a bastion, remember that the route also depends on the jump host; ProxyJump connection setup covers that separate path.

The practical rule is simple: a master is a reusable connection, not a durable session manager. After a network change, check the local master, retire it deliberately, and let a new SSH connection authenticate over the current network.

I'm building ShadeTerm — an SSH client that organizes terminals, SFTP, code editing, and server tools into workspaces.

Top comments (0)