Rsync Linux User Creation and Test

Rsync Linux User Creation and Test

Before starting the backup process between two servers via Rsync utility, we need to create a unique Linux user for the Rsync utility for security purposes.


1. [Destination server] We need to create a unique Linux user for Rsync utility on the destination server, run the below command:

root@vpsprof-backup:~# useradd -m rsyncuser
Add Rsync User in Ubuntu

2. [Destination server] Add a password for rsyncuser we’ve just created by running the below command then submit the new password two times as below:

root@vpsprof-backup:~# passwd rsyncuser
Add Password to Rsync Linux User

3. [Destination server] Enter the file: /etc/sudoers, on the destination server by running the following command:

root@vpsprof-backup:~# nano /etc/sudoers

Then add at the end of the file this line to enable the permission for rysncuser by adding it to sudoers:

root@vpsprof-backup:~# rsyncuser ALL= NOPASSWD:/usr/bin/rsync

Note that the file should look like below at the end. If so, close and save the file.

Add Rysnc User to Sudoers

4. [Source server] Go to the source server then run the below command to generate a public SSH key:

root@mail:~# ssh-keygen

After that, the prompt will ask you this question:

Enter file in which to save the key (/root/.ssh/id_rsa):

You need to hit Enter key to keep the default directory.

Then, you will be asked for the passphrase, hit Enter key twice to confirm it without typing anything in the passphrase field.

Generate SSH Key on Source Server

5. [Source server] Send the public key generated to the user rsyncuser on the destination server to establish the connection using the below command. Don’t forget to replace the IP address in bold with your own IP address if you haven’t added it in the golden box on top.

root@mail:~# ssh-copy-id rsyncuser@137.184.33.153 -p 22

You will get a prompt to enter the password of the rsyncuser, type the password then hit Enter key.

Send SSH Key to the Destination Server

And it’s all done! Now we can use the rsyncuser on the destination server to sync the data between the two servers. Furthermore, we can disable the root access on the destination server for security purposes.

Sync Backup Directory Between Source and Destination Servers

6. [Source server] Let’s test the Rsync now. For this purpose, we will run the below command to sync the master_backup directory which we created in this article from the source server to the destination server. We don’t need to create any directory on the destination server as the Rsync utility will create it automatically. Don’t forget to replace your IP address with the IP address below.

root@mail:~# flock -n lock_file -c "rsync -vzru -e 'ssh -p 22' --rsync-path='sudo rsync' --delete /var/master_backup/ rsyncuser@137.184.33.153:/var/master_backup/"
Test Rsync Utility

And as you see, it works! We didn’t enter any password to sync between the two servers! the --delete option is to sync with delete on destination, you can remove it if you don’t wish to delete any directory or file on the destination server

7. [Destination server] Now let’s make sure that the master_backup directory has been created on the destination server, run the below command:

root@vpsprof-backup:~# ls /var
Make Sure Rsync Utility Worked

As you see, it’s there! You can also check the directories and files inside, they should be exactly same as the source server.

Leave a Comment

Your email address will not be published.