use rsync with ssh keys

How to Use Rsync With SSH Key

Rsync is a powerful tools used to copy files & folders from one location to another in Linux. It is also useful in syncing couple of directories so that they contain same content. You can also use rsync to copy files and directories to & from remote servers. In such cases, you need to provide user authentication details such as remote username & password. If your remote server employs key-based authentication instead of password-based authentication, then you need to provide the public SSH key in your rsync commands. In this article, we will learn how to use rsync with SSH key.


How to Use Rsync With SSH Key

Here are the steps to use rsync with SSH keys. Basically, you need to generate public key on your local client system, place a copy of it in remote server to where you need to copy files, and then you can easily transfer files between the two system.


1. Setup Public SSH Keys

First, open terminal on your local client system and run the following command to create passwordless public SSH key on it.

$ ssh-keygen -f ~/.ssh/id_rsa -q -P "" cat ~/.ssh/id_rsa.pub

We are using passwordless keys so that we are not prompted for password, every time we use the key in rsync command.

Open the key file.

$ cat ~/.ssh/id_rsa.pub

You will see the key file

ssh-rsa
...

Copy the contents of this file to your clipboard and log into your remote server. Place this SSH key into your ~/.ssh/authorized_keys file. If you find that this folder does not exist on your destination server, create it manually using the following commands.

$ mkdir ~/.ssh chmod 0700 ~/.ssh touch ~/.ssh/authorized_keys chmod 0644 ~/.ssh/authorized_keys

Now when you run rsync command from your client system, the remote server will authenticate it using the public key that you have just now stored in its ~/.ssh/authorized_keys folder. The file transfer will begin only on successful transfer.


2. Sync Files Using Rsync

Now you can easily use rsync command to copy files from one place to another between the two systems. For our example, we will copy file /home/ubuntu/data.txt from our source system with IP 54.43.32.21 to our destination with IP 44.43.32.22 and save it to the same folder there. For this purpose, login to 44.43.32.21 system and rsync the files to remote server with the following command. Replace username in it with your remote username.

$ rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /home/ubuntu/data.txt username@44.43.32.21:/home/ubuntu

You can verify that the file has been transferred by logging into 44.43.32.21 and viewing the contents using ls command.

$ ls -la /home/ubuntu/data.txt

In this short article, we have learnt how to use rsync with SSH keys. Many cloud-based systems like AWS use key-based authentication so this method can be used in such cases. Also since we are using passwordless public keys, you can add the above commands in a shell script or cronjob to automate tasks.

In fact, once you have copied the public key of your local system to the remote system, you can use it to copy files to as well as from the remote system. It works both ways. But you cannot call rsync from remote system to the local system. For this purpose, you will need to place the public key of remote server on local system for authentication.

Also read:

How to Run Python Scripts in Sequence
How to Download Gmail Attachment Using Shell Script
How to Do Google Search From Terminal
How to Backup WordPress to Dropbox
How to Download Gmail Attachment using Python

Leave a Reply

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