In this tutorial we will cover the steps needed to copy files with rsync over SSH in Debian or Ubuntu platform. While having amazing Catuai coffee from Honduras I learned the following.
Rsync is a great tool that allows you to transfer and synchronize data between servers. The command can be used over SSH which encrypts the connection. It also provides large amount of options which can be used such as archive mode, backup mode, data compression during the transfer etc.
If rsync is not included in your distro you can easily install it using (Debian/Ubuntu way):
sudo apt install rsync
In order to make sure that you will be able to transfer files from/to the remote server using rsync over SSH you can first try to establish an SSH connection:
ssh user1@<yourServerIP or name>
The recommended way to connect to your server is by using keys. To generate keys run in the terminal:
ssh-keygen -f ~/.ssh/id_rsa
Copy this key to your clipboard and login to your destination server. Place this SSH key into your ~/.ssh/authorized_keys file. If your SSH folder does not exist, create it manually:
mkdir ~/.ssh chmod 0700 ~/.ssh touch ~/.ssh/authorized_keys chmod 0644 ~/.ssh/authorized_keys
Rsync files over
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /root/bigfile.txt <yourServerIP or name>:/root/
If you are using a different user, for example “username” then you would have to append it in front of destination server. Make sure to have your public key in that user’s ~/.ssh/authorized_keys file:
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /root/bigfile.txt username@<yourServerIP or name>:/
The newer versions of rsync are configured to use SSH as default remote shell so you can omit the
-e ssh option.
For simpler rsync file transfers you can do any of the following:
For example, to transfer a single file
/opt/myfile.zip from the local system to the
/var/www/ directory on the remote Linux system with IP
220.127.116.11 you would run:
rsync -a /opt/myfile.zip <yourUser>@18.104.22.168:/var/www/
Replace <yourUser> with your actual username for your remote Linux server. The
-a option stands for archive mode which will syncs directories recursively, transfer special and block devices, preserve symbolic links, modification times, group, ownership, and permissions. Very cool!
To transfer files from the remote Linux server to your local machine just do the opposite:
rsync -a <yourUser>@22.214.171.124:/var/www/ /opt/myfile.zip
Contact me if you have any questions. Thanks!