I'm using the following line in my scripts:

ssh -f -N -M -S <control socket> <host>

This means the initial connection just stays in the background and I can use it for subsequent calls to ssh:

ssh -S <control socket> <host> <command>

However, if I have multiple scripts with commands which are supposed to use the same control socket and put the "background" call to ssh into all of them, I will get the following message at some point:

ControlSocket <control socket> already exists, disabling multiplexing

This has no influence over the rest of the script because obviously the socket exists and can be used by the subsequent ssh commands. However, even though the "background" session couldn't open the socket, it doesn't quit and stays active - just without multiplexing.

Using [ -S or ssh -O check to check the existence of the socket would still leave the possibility of race conditions.

How can I do "open control socket if it doesn't exist yet, and exit if it does"?


1 Answer 1


I think you are looking for ControlMaster auto, which can be either specified in configuration file or directly on command-line with -o ControlMaster=auto.

This allows you to unify the commands opening the connection and using it (also very helpful with ControlPersist).

  • 1
    Unfortunately, the -M sets it to master, which is not what you want for transparent usage of the multiplexing.
    – Jakuje
    Commented Apr 18, 2020 at 20:30
  • 1
    I still get the error even with ControlMaster auto
    – capr
    Commented Nov 17, 2021 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .