How to Transfer Files with Rsync over SSH

By 

Updated on

6 min read

Rsync over SSH

rsync over SSH is one of the most efficient ways to copy and synchronize files between Linux systems. It combines secure transport with fast incremental transfers, making it useful for backups, deployments, and routine file sync tasks.

This guide explains how to transfer files with rsync over SSH, including local-to-remote and remote-to-local copies, custom SSH ports, dry runs, and directory synchronization.

Quick Reference

Common rsync over SSH patterns. For a printable quick reference, see the rsync cheatsheet .

CommandDescription
rsync -a file.txt user@host:/dest/Copy a file to remote
rsync -a user@host:/src/file.txt /dest/Copy a file from remote
rsync -a /src/ user@host:/dest/Sync directory contents to remote
rsync -a -n /src/ user@host:/dest/Dry run — preview changes without transferring
rsync -a -P /src/ user@host:/dest/Show progress bar during transfer
rsync -a -z /src/ user@host:/dest/Enable compression
rsync -a --delete /src/ user@host:/dest/Mirror — delete extra files on remote
rsync -a --exclude="*.log" /src/ user@host:/dest/Exclude files by pattern
rsync -a -e "ssh -p PORT" /src/ user@host:/dest/Use a custom SSH port

Requirements

  • The rsync utility 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:

    Terminal
    sudo apt install rsync

    Fedora, RHEL, and Derivatives:

    Terminal
    sudo dnf install rsync
  • SSH access to the remote computer.

  • The user running the rsync command and the remote SSH user must have appropriate permissions to read and write files.

Using rsync to Transfer Files over SSH

With 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:

txt
Local to Remote: rsync [OPTION]... -e ssh [SRC]... [USER@]HOST:DEST
Remote to Local: rsync [OPTION]... -e ssh [USER@]HOST:SRC... [DEST]

Where 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 12.12.12.12 you would run:

Terminal
rsync -a /opt/file.zip user@12.12.12.12:/var/www/

The -a option stands for archive mode which syncs directories recursively, transfers special and block devices, preserves 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:

Terminal
rsync -a /opt/file.zip user@12.12.12.12:/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:

Terminal
rsync -a user@12.12.12.12:/var/www/file.zip /opt/

Transferring directories with rsync over SSH is the same as transferring files.

One of the most important rsync behaviors to understand is how a trailing slash on the source path changes the result.

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:

Terminal
rsync -a /home/linuxize/images/ user@12.12.12.12:/var/www/images/

Use the --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 do not exist in the source directory.

Terminal
rsync -a --delete /home/linuxize/images/ user@12.12.12.12:/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:

Terminal
rsync -a -e "ssh -p 3322" /home/linuxize/images/ user@12.12.12.12:/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:

Terminal
rsync -a -P /home/linuxize/images/ user@12.12.12.12:/var/www/images/

To reduce bandwidth usage on slow connections, add the -z flag to enable compression during the transfer:

Terminal
rsync -a -z /home/linuxize/images/ user@12.12.12.12:/var/www/images/

Before running a destructive operation such as --delete, use the -n (or --dry-run) flag to preview exactly what rsync would transfer or remove without making any changes:

Terminal
rsync -a -n --delete /home/linuxize/images/ user@12.12.12.12:/var/www/images/

To exclude files or directories from the transfer, use the --exclude option.

Troubleshooting

rsync: command not found rsync is not installed on the local or remote system. Install it on both machines — see the Requirements section above.

Permission denied (publickey) The SSH key is missing or not authorized on the remote host. Set up passwordless SSH login or pass the password interactively.

ssh: connect to host port 22: Connection refused SSH is running on a non-default port. Use -e "ssh -p PORT" to specify the correct port.

Files are not syncing as expected Check the trailing slash on the source path. rsync -a /src/ dest/ copies the contents of src; rsync -a /src dest/ copies the src directory itself into dest.

Transfer is slow over a high-latency connection Add -z to enable compression. For large files this can significantly reduce transfer time.

FAQ

What does the -a flag do? Archive mode (-a) is equivalent to -rlptgoD. It syncs recursively and preserves symbolic links, permissions, timestamps, group, owner, and device files — making it the most common choice for backups and migrations.

How do I preview what rsync will do before transferring? Add -n or --dry-run to your command. rsync will print every file it would transfer or delete without making any actual changes. Combine it with -v for verbose output.

What is the difference between rsync and scp? scp always copies the full file. rsync compares source and destination and transfers only the differences, which makes repeat transfers of large directories much faster.

Can I use rsync with an SSH config file? Yes. If you have a host alias defined in ~/.ssh/config, you can use it directly — for example, rsync -a /src/ myserver:/dest/ — and rsync will pick up the port, key, and username from the config entry.

Conclusion

We have shown you how to use rsync over SSH to copy and synchronize files and directories. Use -n to preview any operation before running it, and -z to speed up transfers over slow connections.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page