Introduction
Rsync over SSH is a powerful tool for transferring files between two computers. It is a secure and efficient way to transfer files, as it only transfers the differences between the two files, rather than the entire file. This makes it much faster than other methods of file transfer. Additionally, it is secure, as it uses SSH encryption to protect the data being transferred. In this guide, we will discuss how to use Rsync over SSH to transfer files between two computers.
How to Transfer Files with Rsync over SSH
1. Install Rsync on both the local and remote machines.
2. Generate an SSH key pair on the local machine.
3. Copy the public key to the remote machine.
4. Create a configuration file on the local machine.
5. Run the Rsync command to transfer files.
6. Verify the files have been transferred.
Introduction
Rsync stands for “Remote Sync.” The rsync
command lets you transfer and synchronize data between different machines and directories. Using the Secure Shell (SSH) protocol, you can copy your files securely to another location.
The rsync tool has many benefits when compared to other methods for copying files. It uses both compression and decompression while sending and receiving files. The tool only transfers new or updated files saving on bandwidth and bringing faster transfer times.
Follow this guide to learn how to use rsync over SSH to transfer or copy files.
Prerequisites
- User with sudo or root privileges
- SSH access to a server via command line/terminal window
- Rsync installed on the local and destination machine
Verify Rsync Installation
Most recent Linux distributions have rsync by default. To verify your system has rsync installed, run the installation command.
For Debian and Ubuntu machines, use apt-get
:
sudo apt-get install rsync
If rsync is already installed, the output shows the current version of the tool.
On RPM-based machines, such as CentOS, use:
sudo yum install rsync
Transfer Files with Rsync over SSH
Before you can start transferring files and directories with rsync over SSH, make sure you can use SSH to connect to a remote server. Once verified, you can begin backing up your data. Ensure your destination system has sufficient storage space.
The syntax for copying files to a remote server over SSH with the rsync
command is:
rsync OPTION SourceDirectory_or_filePath [email protected]_or_name:Target
Note: You need to enter your password every time you run the rsync command. To avoid doing so, you can set SSH key-based authentication. You can omit the username from the command if you want to use the currently logged in user.
rsync Command Examples
Follow the rest of the guide to see the examples of the most common rsync use cases.
Transfer a Specific File with Rsync
To transfer a single file to a remote machine, enter the full path of the source file. For example:
rsync ~/Dir1/source.pdf [email protected]:~/Desktop/test
Make sure you use the quotes for files that contain spaces in the name. For example:
rsync ~/Desktop/Dir1/"source pdf sample.pdf" [email protected]:~/Desktop/test
Transfer Contents of a Directory with Rsync
There are a few options when syncing directory contents with rsync over SSH.
To transfer all files from a source directory to target on a remote host, enter:
rsync ~/SourceDirectory/* [email protected]:~/Destination
The asterisk (*) instructs the tool to include all files in the source directory. Subdirectories are not transferred.
For example:
The output shows the directories that rsync skipped during the transfer.
To include all subdirectories from the source directory, use the -r
(recursive) or -a
(archive) option. The -a
flag is what we recommend. This option syncs recursively and keeps all permission and file settings. This time do not use the asterisk in the source path.
rsync -a ~/Desktop/Dir1/ [email protected]:~/Desktop/test
When you run the command in this format, rsync transfers all files and subdirectories from the source directory to the target location. From our example above, the test folder will have the contents of the Dir1 source when the transfer completes.
To omit files from being copied check out our guide on how to exclude files and directories in data transfer using rsync command.
Note: The trailing slash in the source path plays an important role. If you enter a source directory path without the slash at the end, rsync first transfers the source directory and then its contents. To demonstrate, we will remove the trailing slash from the example above:
rsync -a ~/Desktop/Dir1 [email protected]:~/Desktop/test
When we open the test directory, it contains the Dir1 directory and then the rest of the files in Dir1.
Check Rsync File Transfer Progress
To check the status of rsync transfers, use the -P
option. This option displays the transfer times, as well as the names of the files and directories that are synced.
If there is an issue with your connection and the sync is interrupted, -P
resumes your transfers.
Run the command in this format to sync recursively and check the status of the transfer:
rsync -aP ~/SourceDirectory/ [email protected]:~/Destination
For example:
If you rerun the same command when there haven’t been any changes in the source directory, the transfer does not occur. The reason is that rsync only transfers modifications in files and new files. In this case, the output shows only the following line:
When there is a change even in one source file, rsync detects it and syncs it with the target.
We made a change in one file and re-ran the same command:
The output shows the source sample2 file was transferred since rsync detected that we modified the file.
Conclusion
With the Linux commands in this guide, you should be able to transfer files with rsync over SSH. We listed the most common options with rsync so you can successfully backup your data to a remote location.
How to Transfer Files with Rsync over SSH
Rsync is a powerful tool for transferring files between two computers. It is especially useful when transferring large files over a secure connection, such as SSH. In this tutorial, we will show you how to use rsync to transfer files over SSH.
Prerequisites
- Two computers with SSH access
- Rsync installed on both computers
Step 1: Generate SSH Key
Before you can use rsync over SSH, you need to generate an SSH key. To do this, open a terminal window on the computer you want to transfer files from and run the following command:
ssh-keygen -t rsa -b 4096
This will generate an SSH key pair. The public key will be stored in the ~/.ssh/id_rsa.pub
file and the private key will be stored in the ~/.ssh/id_rsa
file.
Step 2: Copy the Public Key to the Remote Computer
Once you have generated the SSH key, you need to copy the public key to the remote computer. To do this, run the following command:
ssh-copy-id user@remote_host
Replace user
with the username of the remote computer and remote_host
with the IP address or hostname of the remote computer.
Step 3: Transfer Files with Rsync
Now that you have set up the SSH connection, you can use rsync to transfer files. To do this, run the following command:
rsync -avz -e ssh user@remote_host:/path/to/source/ /path/to/destination/
Replace user
with the username of the remote computer, remote_host
with the IP address or hostname of the remote computer, /path/to/source/
with the path to the source directory, and /path/to/destination/
with the path to the destination directory.
Conclusion
In this tutorial, we have shown you how to use rsync to transfer files over SSH. We have also shown you how to generate an SSH key and copy it to the remote computer. Now you should be able to transfer files securely with rsync.