Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
1  
Not killing it, but you can simply not use the persisting connection via ssh -S none (maybe this helps you already). –  sr_ Nov 5 '11 at 9:15
    
No I'm trying to remove a user on a remote server, but the hanging connections prevent me from doing it. –  Paolo Nov 5 '11 at 10:17

3 Answers 3

up vote 15 down vote accepted

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).

share|improve this answer
    
This is an excellent answer. This is perfect for what I was trying to achieve. check is even more useful than what I was looking for, which is what exit does anyway! :D –  elliottcable Dec 6 '11 at 14:19
1  
In OS X fuser can't send signals, but this works just as well: lsof -Fp /tmp/ssh_mux_* | cut -c 2- | xargs kill -HUP –  Ori Jan 15 '12 at 21:06

You can run fuser /tmp/ssh_mux_blablabla (possible needing sudo) and kill the PID it returns. fuser shows which processes are using a file. (And more, check out man fuser.)

Update: check out Gilles' answer; it is much more detailed.

share|improve this answer

I wrote an open source utility to manage ControlMaster sessions: cmc

Usage:  cmc [ -c HOST | -o HOST | -x HOST ]
        cmc [ -L | -l | -O | -X ]
        cmc -h

ControlMaster Controller - Eases management of SSH ControlMaster connections

Options:
    -h      show this help message and exit
    -c HOST check HOST ControlMaster connection status (maybe specified more
            than once)
    -L      list ControlMasters defined in SSH_CONFIG
    -l      list ControlMaster connection sockets in ~/.ssh/ and check their
            connection status
    -O      open all ControlMasters defined in SSH_CONFIG
    -o HOST open a ControlMaster session (maybe specified more than once)
    -x HOST close ControlMaster session (maybe specified more than once)
    -X      exit all ControlMaster connections with sockets in ~/.ssh/

Notes:
    * Any unopened sockets in ~/.ssh/ are removed with -l and -X
share|improve this answer
    
Where do we find this? –  vonbrand Feb 18 '13 at 16:29
    
@vonbrand The hyperlink in first link points to the code repository at github.com, a code hosting site. –  Jokester Mar 20 '13 at 16:55
    
@jokester, I know. Others might not. The repository might go away, github might go under, ...; better give full name so it can be found in 5 year's time. –  vonbrand Mar 20 '13 at 18:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.