How to Transfer Files with Rsync over SSH
4 min read
When it comes to transferring files between systems on the network, Linux and Unix users have a lot of tools at their disposal.
The most popular protocols for data transfer are SSH and FTP . While FTP is very popular, always prefer using SSH as it is the most secure way to transfer your files.
There are specialized tools for file transfer over SSH such as
sftp but none of them has all the features that
rsync can be used for mirroring data, incremental backups, copying files between systems and so on.
In this tutorial, we will explain how to copy files with
rsync over SSH.
rsyncutility must be installed on both the destination and the source systems. If it is not installed you can install it using your distribution’s package manager:
Ubuntu and Debian:
sudo apt install rsync
CentOS and Fedora:
sudo yum install rsync
SSH access to the remote computer.
The user running the
rsynccommand and the remote SSH user must have appropriate permissions to read and write files.
rsync to Transfer Files over SSH
rsync, you can transfer files and directories over SSH from and to remote servers.
The general syntax for transferring files with
rsync is as follows:
Local to Remote: rsync [OPTION]... -e ssh [SRC]... [USER@]HOST:DEST Remote to Local: rsync [OPTION]... -e ssh [USER@]HOST:SRC... [DEST]
SRC is the source directory,
DEST is the destination directory
USER is the remote SSH username and
HOST is the remote SSH host or IP Address.
The newer versions of rsync are configured to use SSH as default remote shell so you can omit the
-e ssh option.
For example, to transfer a single file
/opt/file.zip from the local system to the
/var/www/ directory on the remote system with IP
220.127.116.11 you would run:
rsync -a /opt/file.zip firstname.lastname@example.org:/var/www/
-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.
If you haven’t set a passwordless SSH login to the remote machine, you will be prompted to enter the user password.
If the file exists on the remote server it will be overwritten. If you want to save the file under a different name, specify the new name:
rsync -a /opt/file.zip email@example.com:/var/www/file2.zip
To transfer data from a remote to a local machine, use the remote location as the source and the local location as destination:
rsync -a firstname.lastname@example.org:/var/www/file.zip /opt/
Transferring directories with
rsync over SSH is same as transferring files.
It is important to know that
rsync gives different treatment to the source directories with a trailing slash
/. When the source directory has a trailing slash,
rsync will copy only the contents of the source directory to the destination directory. When the trailing slash is omitted the source directory will be copied inside the destination directory.
For example to transfer the local
/opt/website/images/ directory to the
/var/www/images/ directory on a remote machine you would type:
rsync -a /home/linuxize/images/ email@example.com:/var/www/images/
--delete option if you want to synchronize the local and remote directory. Be careful when using this option as it will delete files in the destination directory if they don’t exist in the source directory.
rsync -a --delete /home/linuxize/images/ firstname.lastname@example.org:/var/www/images/
If SSH on the remote host is listening on a port other than the default
22, specify the port using the
-e option. For example, if SSH is listening on port
3322 you would use:
rsync -a -e "ssh -p 3322" /home/linuxize/images/ email@example.com:/var/www/images/
When transferring large amounts of data it is recommended to run the
rsync command inside a screen session or use the
-P option which tells
rsync to show a progress bar during the transfer and keep the partially transferred files:
rsync -a -P /home/linuxize/images/ firstname.lastname@example.org:/var/www/images/
We have shown you how to use
rsync over SSH to copy and synchronize files and directories.
You may also want to read how to exclude files or directories with
Feel free to leave a comment if you have any questions.