49

I want to rsync to a cluster node to which I usually connect passing through another system:

Say I connect first to

  ssh user@bridge 

and from there to

  ssh user@clusternode

Now I want to rsync from my workstation to clusternode. I do the following:

  • I open a ssh tunnel

    ssh -L8000:clusternode:8000 user@bridge
    
  • I rsync from my workstation to clusternode

    rsync -e "ssh -p8000" source user@localhost:destination
    

and it does not work, I get

 ssh_exchange_identification: Connection closed by remote host

Why does it not work? What do I have to do?


I have found a lot of information here:

http://toddharris.net/blog/2005/10/23/rsyncing-through-an-ssh-tunnel/

I think to understand that my problem is the second authentication between the bridge and the destination, so I changed to method 2 that is also not very elegant, but it works. I would like to try method 3, but I don't know how to configure a rsync daemon

simona
  • 2,009
  • 6
  • 29
  • 41
  • Try using the -v (or -vvv) option to ssh command: `rsync -e "ssh -vvv -p8000" source user@localhost:destination` to enable the verbose ssh logging, the error is almost always in there. – Josh May 21 '13 at 21:23
  • You can set up a proxy to be used for your cluster node (see e.g. https://rsync.samba.org/firewall.html, esp. Method 2), and then use rsync without explicitly specifying a proxy. – jciloa May 30 '16 at 16:26
  • A better solution than given in the answers below: https://puppet.com/blog/speed-up-ssh-by-reusing-connections –  Jun 19 '16 at 01:07
  • 3
    Why is this question off-topic? – rashid Mar 23 '20 at 04:42
  • @rashid "We don’t allow questions about professional server or networking-related infrastructure administration on Stack Overflow. You can edit the question so it’s on-topic for Stack Overflow or post a new one on Server Fault." – simona Mar 26 '20 at 20:10

3 Answers3

91

Try this one-liner:

rsync -av -e "ssh -A root@proxy ssh" ./src root@target:/dst
skroll
  • 931
  • 1
  • 6
  • 4
  • 3
    That one liner is perfect, since I don't want to open a persistent tunnel. Thanks! – Marcus Aug 15 '14 at 19:47
  • 11
    one line solution would be nice, but it doesn't work for me. I get "Host key verification failed. rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: unexplained error (code 255) at /SourceCache/rsync/rsync-42/rsync/io.c(452) [sender=2.6.9]" I think it is because it only asks me the password for the bridge and it never asks me the password for the target (they both have a password) – simona Dec 19 '14 at 09:34
  • @simona, if you configured an SSH key instead of a password, this would be moot (the `-A` forwards the agent connection used to access in-memory SSH keys). – Charles Duffy Sep 02 '15 at 19:57
  • 3
    But what if we can't set up SSH keys? Is it possible to do a one liner and provide a password for both servers? – Felix Eve Feb 09 '16 at 23:40
  • Try it without the `-A` and I suspect you would just get asked for the passwords. It's worth a try. I just was successful without the `-A`, but I have keys set up. – abalter Feb 20 '16 at 08:19
  • 2
    I found that using `rsync -av -e "ssh -A user@proxy ssh -o StrictHostKeyChecking=no"` didn't fail with `Host key verification failed`. This will only work if the host key has never been seen before, in case it has changed add `-o UserKnownHostsFile=/dev/null` (not recommended unless you know why it changed). – ashkulz Jan 06 '17 at 05:57
  • 1
    @abalter trying without -A didn't help for me – Ferdinando Randisi Feb 03 '19 at 19:08
42

Here's what worked for me.

I run a command in the background to tunnel to the remote host:

 ssh -N -L 2222:remote.example.com:22 bridge.example.com&

then I rsync to localhost like this:

rsync -auve "ssh -p 2222" . me@localhost:/some/path
5

You should connect to the port 22 of clusternode, so the tunnel should look like

ssh -L localhost:8000:clusternode:22 user@bridge
Niall C.
  • 10,878
  • 7
  • 69
  • 61
radim
  • 51
  • 1
  • 2