How to use rsync with a remote remote host

I am a happy user of rsync -e ssh and use it to synchronize data between my machine and a remote host using

rsync -avz -e ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98f5fdd8f0f7ebecd9">[email protected]</a>:~/folder ~/folder

And this did what I needed until now.

I am now working with a configuration where the access is a bit more complicated. Suppose now ~/folder is on hostB, but hostB is not directly accessible from my machine, but only indirectly via hostA.

So if I only want to see the folder on hostB. I first need to ssh to hostA, then to hostB after which I can see my files in ~/folder on the remote host.

My question now is, what is the (best) method to use rsync directly between my machine and hostB?

Unfortunately this is the configuration I need to work with. Assume there is no possibility to temporarily store the data on hostA itself.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Have a look at How do I connect to a pc through another pc using ssh

You create a new ~/.ssh/config entry with the name tunnelb:

Host tunnelb
HostName hostB
User user
ProxyCommand ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1267617760527a7d616653">[email protected]</a> nc %h %p

If you have a recent version of ssh you can use Proxycommand ssh [email protected] -W %h:%p instead. This is preferred as it does not rely on nc

Now you can just use rsync -e ssh tunnelb:~/folder ~/folder as it will use the defined entry in ~/.ssh/config.

Method 2

Variant 1:

First, start ssh on hostA permanently with local port forwarding in another terminal. E.g.:

ssh -N -v -L 22022:hostB:22 hostA

Then, “feed” rsync with ssh to localhost:22022. One variant is to write to ~/.ssh/config:

Host hostB
HostName localhost
Port 22022

Variant 2:

Replace -e ssh with -e to some script which contains:

ssh hostA 'ssh hostB' "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e9cda9">[email protected]</a>"

I didn’t try it so some details would be elaborated additionally.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x