Rsync is a popular Linux utility that allows you to easily transfer files and data between two locations on the same or different systems. In most cases, by default, Rsync will ask for password for authentication. But sometimes you may need to use rsync between two systems without a password. This is required especially if you are calling rsync command from within a shell script that is used to automate tasks. In this article, we will learn how to setup Rsync between two servers without password.
How to Setup Rsync Between Two Servers Without Password
Here are the steps to setup rsync between two servers without password. Let us say you want to transfer data from local directory /home/ubuntu to remote directory /home/backup at 54.43.32.21 IP address.
1. Check Password Requirement
First, we need to check if rsync on our system asks for a password. For that purpose, run the following command.
$ rsync -avz -e ssh /home/ubuntu/ user@54.43.32.21:/home/backup/
If you are asked for a password, then follow the steps below.
2. Generate Keys Using SSH-Keygen
Use ssh-keygen tool on your local machine to generate private & public keys.
$ ssh-keygen Enter passphrase (empty for no passphrase): Enter same passphrase again:
In both the above prompts, leave the password blank by pressing enter/return key.
3. Copy Public Key to Remote Host
Next, we will use ssh-copy-id to copy public key to remote host.
$ ssh-copy-id -i ~/.ssh/id_rsa.pub 54.43.32.21
When you run the above command, you will be asked to enter password for remote host. On entering it, you will login successfully and the public key file will be copied from your local machine to remote system.
If the above command does not work for you, open the public key file in a text editor on your local machine.
$ vi ~/.ssh/id_rsa.pub
Copy the file content and quit without saving.
Log into your remote system and open the file that contains all approved keys.
$ vi /root/.ssh/authorized_keys
Paste the public key that you copied earlier here. Save and close the file.
4. Run RSync
Now you should be able to rsync with remote host without using password.
$ rsync -avz -e ssh /home/ubuntu/ user@54.43.32.21:/home/backup/
In this article, we have learnt how to rsync without using password. Please note, even though we do not use password here, it does not mean there is no authentication. In this case, the authentication is done using private/public keys instead of using passwords. This is really useful if you want to automate rsync file transfer without having to manually enter password every time. For example, system administrators can use it to automatically backup their data to remove servers using rsync and cron jobs.
Also read:
How to Enable HSTS in NGINX
How to Change Apache Log Location
How to Redirect URL to Another Domain Without Changing URL
How to Exclude URL from Auth in Apache
How to Exclude Directory from Auth in Apache
Related posts:
Sreeram Sreenivasan loves coding & writing tech articles. He has helped many Fortune 500 companies in the areas of BI & software development. He has more than 10 years of experience in web development, Python, Linux, SQL and database programming.