I am using the following options in my ~/.ssh/config in order to enable connection multiplexing:

ControlMaster auto
ControlPath ~/.ssh/control/master-%r@%h:%p

However, this has the annoying problem that the first shell to connect to a particular server must be the last to disconnect, because it is the master connection that all the other connections are using. So if you log out of the master, it appears to just hang. To solve this, I would like to wrap ssh with a script that checks if the control master file exists, and if not, starts a master ssh process in the background. Then it would start a slave ssh session.

In order to accomplish this, my script would have to determine the path to the control file that ssh would use. This would entail parsing the ssh command line options and config files and implementing the logic for determining the ControlPath. Is there any way to just ask ssh what path it would use, so I can check it?


Just found it. ssh -O check HOST


From my tests the accepted answer:

ssh -O check HOST

will only give the control file if it doesn't exits

Control socket connect(<path to control file>): No such file or directory


Master running (pid=<number>)

However, grep and ssh -G will always give you the path.

-G Causes ssh to print its configuration after evaluating Host and Match blocks and exit.

>>> ssh -G HOST | grep controlpath
controlpath <path to control file>

Note: If you use %l or %C (for example) in the ssh config file this will evaluate for the local machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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