I’m trying to run an rsync between two servers. I’m basing things off of this post: How to rsync files between two remotes?

What I find missing is how to facilitate the rsync (via ssh) when a key (the same key) is required for logging into each server.

Here’s the closest I’ve got:

ssh -i ~/path/to/pem/file.pem -R localhost:50000:SERVER2:22 ubuntu@SERVER1 ‘rsync -e “ssh -p 50000” -vur /home/ubuntu/test localhost:/home/ubuntu/test’
It seems like the initial connection works properly, however I can’t seem to figure out how to specific the key and username for SERVER2.

Answer

Create a ~/.ssh/config on the SERVER1 with all you need to connect to the SERVER2, such as:

Host SERVER2
Port 50000
User user2
Hostname localhost
IdentityFile ~/path/to/pem/file.pem
Try to connect to SERVER2 hosts without any arguments (from SERVER1 after initiating port forwarding):

ssh SERVER2 # works?
If not, add -vvv and investigate what went wrong.

Run the rsync command:

ssh -i ~/path/to/pem/file.pem -R localhost:50000:SERVER2:22 ubuntu@SERVER1 \
‘rsync -vur /home/ubuntu/test SERVER2:/home/ubuntu/test’
Note, you need to have the authentication key available on the SERVER1, if you want to do it this way. It is better to create a new key there than copying your private key from your machine.

Leave a Reply

Your email address will not be published. Required fields are marked *