Networking – Rsyncing directories through ssh tunnel


I have 3 computers/servers, let's call them as computer A, computer B and computer C.

  • Computer A has access via ssh to computer B.
  • Only computer B has access via ssh to computer C.
  • Computer A doesn't have access to computer C directly.

So, I want to rsync directories from computer A to computer C, using computer B(since only computer B has access via ssh to computer C). I want to use rsync in a simple way, like this:

rsync -options /path_to_local IpComputerC:/path_to_remote

I'm thinking on a ssh tunnel, but I don't know if I have to make a local port forwarding or a remote port forwarding, or if I have to make two tunnels: one from A to B and other from B to C?

Any suggestions?? I'd be really grateful.

**UPDATE: I dit it!! I created an ssh tunnel with local port forwarding, which means that I used a local port from the machine A to connect with a remote port on the machine C through the machine B. The syntax for the command is:

ssh -f -N -L local_port_machineA:ip_machineC:remoteport_machineC user_machineB@ip_machineB

In my case, remoteport_machineC shoul be port 22, because I want to rsync directories through ssh.

After that, we can execute the rsync command from the machine A, in this way:

rsync -avz -e "ssh -p local_port" /source_directory username_machineC@localhost:/target_directory

That's all. Additionally, you can add more options in the part inside the quotes, such as keys or files, etc.**

Best Answer

Much simpler than other suggestions: use rsync's -e | --rsh option:

rsync -azv -e 'ssh -o "ProxyCommand ssh -A proxy -W %h:%p"' foo/ dest:./foo/

Note that I'm using -A (agent forwarding) but it should also work with password authentication if you don't use keys, and, of course, you can replace proxy with B and dest with C in your example.

If by chance you don't have a new enough ssh version (>= 5.3, IIRC), you can use netcat instead of -W option to ssh:

rsync -azv -e 'ssh -o "ProxyCommand ssh -A proxy nc %h %p"' foo/ dest:./foo/