SSH – How to Close (Kill) SSH ControlMaster Connections Manually

connection-sharingssh

With the following .ssh/config configuration:

ControlMaster auto
ControlPath /tmp/ssh_mux_%h_%p_%r
ControlPersist 4h

How to close the persisting connection before the 4 hours?

I know you can make new connections, but how to close them (all)?

Maybe there is a way to show all the persisted connections and handle them individually but I can not find it.

Best Answer

From the manual:

-O ctl_cmd
Control an active connection multiplexing master process. When the -O option is specified, the ctl_cmd argument is interpreted and passed to the master process. Valid commands are: check (check that the master process is running), forward (request forwardings without command execution), cancel (cancel forwardings), exit (request the master to exit), and stop (request the master to stop accepting further multiplexing requests).

Older versions only have check and exit, but that's enough for your purpose.

ssh -O check host.example.com

If you want to delete all connections (not just the connection to a particular host) in one fell swoop, then fuser /tmp/ssh_mux_* or lsof /tmp/ssh_mux_* will list the ssh clients that are controlling each socket. Use fuser -HUP -k tmp/ssh_mux_* to kill them all cleanly (using SIGHUP as the signal is best as it lets the clients properly remove their socket).

Related Question