How to setup Rsync to Sync directories between two Ubuntu Linux Server Without Password

What is Rsync: -
Rsync stands for Remote Sync is a synchronizer tool to sync data local to remote server and Vice-versa. 
It helps us to sync data faster by syncing only modify and newly add file or directories.


Prerequisites: -
1- Two Linux Server 
2- Sudo privileges user
3- Rsync package installed on both the server

In my case, I am using Two Ubuntu 16.04 LTS.

Server-A -192.168.102.10                        UserName- user1
Server-B - 192.168.102.11                       UserName- user2

Step: 1- Install Rsync tool
Server A -192.168.102.10-   SSH to Server A and run command below
user1@US16:~$ sudo apt install rsync
Server B -192.168.102.11-   SSH to Server B and run command below
user2@Server16:~$ sudo apt install rsync

Step: 2- Configure without password login Server-A to Server-B.

Server A -192.168.102.10Server A Generate Public and Private Key using the command below, and hit the enter button for each prompt.


user1@US16:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user1/.ssh/id_rsa):
Created directory '/home/user1/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user1/.ssh/id_rsa.
Your public key has been saved in /home/user1/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:v7JEcHi/VJmEdiyiIQTA/Y6rQsOpR2MSONmiuVHQrdQ user1@US16
The key's randomart image is:
+---[RSA 2048]----+
|ooo=. o. |
|..o.E ... +.oo |
|.= ...oooo o+ |
|* + ..+ . . |
|o*. o S o |
|==+. . . o . |
|o*... . o |
|+ .. .. . |
|.o. .o. |
+----[SHA256]-----+
user1@US16:~$


Let's copy public key from Server A to Server B using the command below.
Server-A
user1@US16:~$ ssh-copy-id user2@192.168.102.11
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/user1/.ssh/id_rsa.pub"
The authenticity of host '192.168.102.11 (192.168.102.11)' can't be established.
ECDSA key fingerprint is SHA256:xaB8bGd7f6A/49x402pzRe0ibgri0OfANsmI5G7AYOg.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
user2@192.168.102.11's password: ******

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'user2@192.168.102.11'"
and check to make sure that only the key(s) you wanted were added.

user1@US16:~$


Let's Check SSH From Server A to Server B
Server-A
user1@US16:~$ ssh user2@192.168.102.11
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-62-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

111 packages can be updated.
0 updates are security updates.


*** System restart required ***
Last login: Thu Jun 22 16:32:43 2017 from 192.168.102.10

user2@Server16:~$ who
user2 pts/1 2017-06-22 17:09 (192.168.102.10)
user2@Server16:~$


SSH working fine without password!!


Step: 3- Use Rsync to copy data Local to Remote Server.

I have a directory name rsynctest and sub-directory a, b, and c on my local Server. 
Server-A
user1@US16:~$ ls -l rsynctest/
total 12
drwxrwxr-x 2 user1 user1 4096 Jun 22 07:37 a
drwxrwxr-x 2 user1 user1 4096 Jun 22 07:37 b
drwxrwxr-x 2 user1 user1 4096 Jun 22 07:37 c
user1@US16:~$


Let's Sync directory rsynctest to Remote Server using command below
Server-A
user1@US16:~$ rsync -avzh rsynctest/ user2@192.168.102.11:/home/user2/rsynctest
sending incremental file list
created directory /home/user2/rsynctest
./
a/
b/
c/

sent 123 bytes received 75 bytes 132.00 bytes/sec
total size is 0 speedup is 0.00

Where switches as follow
-a, --archive               archive mode;
-v, --verbose increase verbosity
-z, --compress compress file data during the transfer
-H, --hard-links preserve hard links


Check synced directory on Server B
Server-B
user2@Server16:~$ ls -l rsynctest/
total 12
drwxrwxr-x 2 user2 user2 4096 Jun 22 18:07 a
drwxrwxr-x 2 user2 user2 4096 Jun 22 18:07 b
drwxrwxr-x 2 user2 user2 4096 Jun 22 18:07 c

user2@Server16:~$


All three directory synced successfully from Server A to Server B

Step: 4- Use Rsync to copy data Remote to Local Server.
 Let's create a new directory or file over Server B
Server-B
user2@Server16:~$ cd rsynctest
user2@Server16:~/rsynctest$ mkdir s2d
user2@Server16:~/rsynctest$ ll
total 24
drwxrwxr-x 6 user2 user2 4096 Jun 22 22:00 ./
drwxr-xr-x 5 user2 user2 4096 Jun 22 18:11 ../
drwxrwxr-x 2 user2 user2 4096 Jun 22 18:07 a/
drwxrwxr-x 2 user2 user2 4096 Jun 22 18:07 b/
drwxrwxr-x 2 user2 user2 4096 Jun 22 18:07 c/
drwxrwxr-x 2 user2 user2 4096 Jun 22 22:00 s2d/

Server-A
 Run command below to sync newly added directory 
user1@US16:~$ rsync -avzh user2@192.168.102.11:/home/user2/rsynctest/ /home/user1/rsynctest/
receiving incremental file list
./
s2d/

sent 34 bytes received 137 bytes 114.00 bytes/sec
total size is 0 speedup is 0.00


Newly added directory successfully synced to Server A, check using command ls -l
user1@US16:~$ ls -l rsynctest/
total 16
drwxrwxr-x 2 user1 user1 4096 Jun 22 07:37 a
drwxrwxr-x 2 user1 user1 4096 Jun 22 07:37 b
drwxrwxr-x 2 user1 user1 4096 Jun 22 07:37 c
drwxrwxr-x 2 user1 user1 4096 Jun 22 11:30 s2d

!!!Rsync has been successfully setup between two servers without password!!!