I can connect via ssh to two hosts, say host1 and host2 and I need to copy files from host1 to host2 via rsync.

I know I can realize this first copying files from host1 to localhost and then from localhost to host2 or perform simple copy via scp -3 <user1@host1:/file_path1> <user2@host2:/file_path2>, but my goal is test ssh tunnel via rsync.

Someone can help me?


I've read about ssh tunnel, but can't reach my goal... probably there's something I've don't understood. For example if I run

$ ssh -L 9999:host2:22 user_host1@host1

this means: map my local port 9999 to port 22 of host2 and connect to host1 as user user_host1 but... with this mapping what's the purpose of connect to host1 ?

  • The tunnel should be configured e.g. on host1 to reach host2 through (what you called localhost) a third computer that can connect to both host1 and host2. Otherwise, you need remote port forwarding. I suggest you read more about it. – simlev Aug 1 '17 at 11:51
  • Assume You unable to connect ssh directly from host1 to host2. (Otherwise simplest is direct connect host1 to host2)
  • Assume it impossible to connect from host1 or host2 to Your PC.

Next Command make rsync

ssh -R user_host1@host1   rsync -e "ssh -p 9999" /file_path1 user2@

Lets parse it:

  • ssh make connection to host1
  • -R listen on remote (host1) loopback at port 9999 and forward connection over client host to host2:22
  • on host1 rsync run
  • -e "ssh -p 9999" force using non default ssh port for connection
  • rsync local path /file_path1 to remote path user2@ we make connection to user2@host2 but as address we use mapped earlier loopback port

I hope not mistyped in command.


For troubleshooting try at first connect interactive ssh with tunnel

$ ssh -R user_host1@host1

and manual ssh connect over tunel

[user_host1@host1 ]$ ssh -p 9999 user_host1@

Tunneling can be disabled in sshd config at host1.

  • Unfortunately this configurations doesn't works: I receive the message ssh: connect to host port 9999: Connection refused, but I'm sure I can connect to both host: I've added ssh keys to ssh-agent. – Sim Sca Aug 1 '17 at 13:38
  • I checked. It works on my hosts. – Mikhail Moskalev Aug 1 '17 at 13:49

First of all, thanks to mmv-ru: your answer was a great start-point to find what was wrong in my case.

I've write an answer to explain step by step what to do:

1 Remote Loopback

in one console run ssh -R user_host1@host1 -N

2 Work on remote

in other console goto server host1 ssh user_host1@host1 and then run

rsync -e "ssh -p 9999" /file_path1 user2@

in my case this command fails, because I need:

1 - add []:9999 to my known hosts by clicking yes

2 - since I ssh to host2 via a single key, I need to add that key into host1 and set it' permissions to 400.

So my command is

rsync -e "ssh -p 9999 -i my_ssh_key_for_host2" /file_path1 user2@

and works!!!


after step 2 (creation key for host2 on remote host1) I can perform all with one command:

ssh -R user_host1@host1 'rsync -e "ssh -p 9999 -i my_ssh_key_for_host2" /file_path1 user2@'

Note single quote in second part!



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.