Rsync is a software utility that is used for synchronizing files and directories over different locations with minimized data transfer. It allows to copy files recursively with compression and over encrypted channels.
In this tutorial you will see how files are copied using rsync over SSH.
Generate Public SSH Keys
In the original server, you have to generate public SSH keys without passwords. That can be done using the commands below:
ssh-keygen -f ~/.ssh/id_rsa -q -P "" cat ~/.ssh/id_rsa.pub
You will get a public SSH key which can be placed on hosts to give you access. It should look like this:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLVDBIpdpfePg/a6h8au1HTKPPrg8wuTrjdh0QFVPpTI4KHctf6/FGg1NOgM++hrDlbrDVStKn/b3Mu65//tuvY5SG9sR4vrINCSQF++a+YRTGU6Sn4ltKpyj3usHERvBndtFXoDxsYKRCtPfgm1BGTBpoSl2A7lrwnmVSg+u11FOa1xSZ393aaBFDSeX8GlJf1SojWYIAbE25Xe3z5L232vZ5acC2PJkvKctzvUttJCP91gbNe5FSwDolE44diYbNYqEtvq2Jt8x45YzgFSVKf6ffnPwnUDwhtvc2f317TKx9l2Eq4aWqXTOMiPFA5ZRM/CF0IJCqeXG6s+qVfRjB [email protected]
Make sure you copy this key to some file as you will need it for destination server.
Login to your destination server and place this key into your ~/.ssh/authorized_keys file. In case if ssh folder is not present, you can create it manually using:
mkdir ~/.ssh chmod 0700 ~/.ssh touch ~/.ssh/authorized_keys chmod 0644 ~/.ssh/authorized_keys
Using Rsync to Copy Files
Next, we will show you how to copy files using rsync. For example, let’s assume that we are copying a sample file from our original server (22.214.171.124) to destination server (126.96.36.199) and the file to be copied is /root/samplefile.txt
Now, login to original server and rsync the file to destination server:
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /root/samplefile.txt 188.8.131.52:/root/
In case if you are using a normal user and not root user, then you will have to append it in front of destination server. Also, make sure that you have already place the public key in that user’s ~/.ssh/authorized_keys file.
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /root/samplefile.txt [email protected]:/
Now you can verify the copied file from your destination server:
ls -la /root/samplefile.txt