18

I have a script which runs several commands remotely through ssh. I'm running each command separately because I want to do other things in between executions.

However, I don't want to recreate an ssh session every time I issue a new command. I've read about -oControlMaster but I can't seem to get it to work.

When I run:

ssh -oControlMaster=yes -oControlPath=/tmp/test.sock root@host

after I enter my password, I just get an ssh session. If I exit out, the /tmp/test.sock file is no where to be found.

What am I missing?

Lesmana
  • 27,439
nopcorn
  • 9,559

2 Answers2

23

You can use the ControlPersist option to leave the socket after you disconnect from the server.

e.g in my ssh config file i have this snippet, which leave the connection open 3 sec.

Host *
   ControlMaster auto
   ControlPath ~/.ssh/master-socket/%r@%h:%p
   #ControlPath /run/user/%i/sshmasterconn-%C
   #ControlPath ~/.ssh/%r@%h:%p
   ControlPersist 3s
Rabin
  • 3,883
  • 1
  • 22
  • 23
5

The master connection needs to be open for another connection to be able to use the master connection.

The socket file is only available while the master connection is open. If you close the master connection then the socket file is removed. Any open "slave" connection will be closed if the master connection is closed.

Lesmana
  • 27,439