Ssh LocalCommand oddity – why do I have to background an rsync

rsyncssh

I am using rsync to copy files to a target server when I connect to it using ssh. To do this I have the following in my ssh config file:-

ControlMaster auto
ControlPath ~/.ssh/%r@%h:%p
...
...
host dns
   LocalCommand rsync --update -a -e "ssh -o PermitLocalCommand=no" ~/.cfg/ dns:.cfg/ &
...
...

The ControlMaster bit means that after one ssh to the server subsequent ssh connections don't ask for the password. Thus the rsync runs without asking for the password.

The oddity is that it won't work without the & at the end of the LocalCommand line in the config file. Why does it need this? Without the & the login just hangs after entering the password and if you CTRL/C it you get an rsync error as it's been interrupted by the CTRL/C and the connection fails. The rsync copy only takes a fraction of a second, the hang is not due to rsync taking a long time.

I took quite a while to find how to make this work, initially without the & it just hung, as I said. I then put the rsync command in a script and called that, it still hung. I found that starting a terminal from the script and running the rsync in that worked OK. Then I went backwards and put the & after the rsync in the script and that worked, finally I got to what I have above.

It's not a big issue, I'm happy with the way it works, but I'd love to kow why the & is needed.

This is all on Linux, both ends running xubuntu/ubuntu.

Best Answer

Possibly answer in this blogpost:

Finally, there is something of an annoyance with ControlMaster, and it’ll probably confuse you mightily when you first come across it. Because all of your SSH sessions are multiplexed down a single TCP connection initiated by the first SSH session, that first session must stay alive until all of the other sessions are complete. This problem will manifest itself as an apparent “hang” when you log out of the remote session that is acting as the master — instead of getting your local prompt back, SSH will just sit there. If you Ctrl-C or otherwise kill this session, all of the other sessions you’ve got setup to that server will drop, so don’t do that. Instead, when you logout of all the other sessions, the master will then return to the local prompt.

I've solved similar problem by adding -S none to list ssh options:

LocalCommand rsync --update -a -e "ssh -o PermitLocalCommand=no -S none" ~/.cfg/ dns:.cfg/
Related Question