Rsync on Ubuntu: Complete Guide to Fast File Sync, Backup, and Remote Transfer

目次

1. Introduction

What Is rsync?

rsync is a command-line tool designed for fast and efficient file and directory synchronization and copying. It is widely used on Linux and Unix-like systems, and thanks to its delta-transfer algorithm, it transfers only the differences between source and destination. This makes it highly efficient even when handling large numbers of files. On Ubuntu and many other Linux distributions, rsync is often installed by default, making it an essential tool for system administrators and developers.

Benefits of Using rsync on Ubuntu

On Ubuntu, rsync is extremely effective for daily file backups and directory synchronization tasks. Without relying on graphical interfaces, you can achieve fast file transfers—both local and remote—simply by entering a few commands in the terminal.

It is especially useful in the following situations:

  • Regular backups to external HDDs or NAS devices
  • Synchronizing project folders during development
  • Deploying files to remote servers
  • Improving availability through file mirroring

How rsync Differs from Other Commands

Compared to the cp command, rsync transfers only the differences between the source and destination, significantly reducing processing time. Compared to scp, rsync offers resume capabilities after errors and flexible bandwidth control. Additionally, it can preserve file attributes such as ownership, permissions, and timestamps, which is a major advantage.

Purpose of This Article

This article explains how to install and effectively use rsync in an Ubuntu environment, using practical command examples and real-world use cases. To ensure beginners can get started smoothly, we also cover GUI tools and troubleshooting tips while learning rsync step by step.

2. Installing rsync on Ubuntu

Checking Whether rsync Is Preinstalled

In most Ubuntu environments, rsync is already installed by default. You can check its availability with the following command:

rsync --version

If version information is displayed, rsync is already installed and ready to use.

Installing rsync If It Is Not Installed

If you see a message such as “command not found” or “rsync: command not found,” follow these steps to install it:

  1. Update the package list:
sudo apt update
  1. Install rsync:
sudo apt install rsync
  1. Verify the installation:
rsync --version

If version information appears, the installation was successful.

Is Installation via Snap or Other Methods Necessary?

On Ubuntu, installing rsync via APT is the most recommended approach. While Snap packages exist, the APT version is generally sufficient and offers better stability and compatibility for typical use cases. Unless you have a specific reason, APT is the preferred method.

Installation Notes

  • On older Ubuntu versions, repository updates may be required.
  • Root privileges are necessary, so do not forget to use sudo.
  • Unstable network connections may cause installation errors; a stable connection is recommended.

3. Basic Usage of rsync

Basic rsync Syntax

The rsync command syntax is simple yet highly flexible. The basic form is:

rsync [options] source destination

For example, to copy the contents of /home/user/documents/ to /mnt/backup/:

rsync -av /home/user/documents/ /mnt/backup/

This command uses the following options:

  • -a: Archive mode (preserves directory structure, permissions, ownership, etc.)
  • -v: Verbose output (shows which files are copied)

Trailing Slash Behavior

In rsync, whether you include a trailing slash in the source path changes the behavior:

  • /home/user/documents/ (with slash)
    → Only the contents are copied.
  • /home/user/documents (without slash)
    → The directory itself is copied.

This is a critical rsync-specific behavior that should be used appropriately depending on your goal.

Commonly Used Options

OptionDescription
-aArchive mode (recursive and preserves attributes)
-vDisplays detailed transfer output
-nDry run (shows what would happen without executing)
--progressShows transfer progress
--deleteDeletes files in the destination that no longer exist in the source

Previewing Actions Before Execution

To check what rsync will do before actually transferring files, use the --dry-run option:

rsync -av --dry-run /home/user/documents/ /mnt/backup/

This helps prevent accidental data loss.

Handling Hidden Files

By default, rsync includes hidden files (those starting with .), allowing configuration files and environment settings to be synchronized as well.

4. Synchronizing with Remote Servers

Remote Synchronization via SSH

One of rsync’s biggest advantages is its ability to synchronize files with remote servers over SSH, ensuring secure data transfers.

rsync -avz source user@remote_host:destination

Example:

rsync -avz /home/user/documents/ user@example.com:/var/www/backup/
  • -z enables compression during transfer.
  • user@host specifies the SSH login.

First-Time Connection and SSH Key Authentication

On first connection, you may be asked to confirm the host authenticity. Enter “yes” if it is correct.

For frequent use, SSH key authentication is recommended to avoid entering passwords:

ssh-keygen -t rsa
ssh-copy-id user@example.com

Specifying a Custom SSH Port

If the remote server uses a non-default SSH port, specify it with -e:

rsync -avz -e "ssh -p 2222" /home/user/documents/ user@example.com:/var/www/backup/

Important Notes on Bidirectional Sync

rsync performs one-way synchronization only. For true bidirectional sync, consider tools such as unison.

5. Useful rsync Options and Practical Examples

Advanced Options Beyond the Basics

–delete: Remove Files Not Present in the Source

rsync -av --delete /home/user/source/ /mnt/backup/

Always test with --dry-run before using this option.

–exclude: Exclude Specific Files or Directories

rsync -av --exclude '*.log' --exclude 'node_modules' /project/ /backup/

–bwlimit: Bandwidth Limiting

rsync -av --bwlimit=5000 /data/ user@remote:/data/

Real-World Use Cases

Using rsync for Backups

rsync -a --delete /home/user/ /media/usb/backup/

Automating with cron

0 2 * * * rsync -a /home/user/ /mnt/backup/ >> /var/log/rsync.log 2>&1

Balancing Flexibility and Efficiency

rsync combines exceptional flexibility with high transfer efficiency, making it a powerful tool for safe and precise file synchronization.

6. GUI Tool: Grsync

Recommended for Users Uncomfortable with the Command Line

Grsync is a graphical front-end for rsync that allows users to configure and execute synchronization tasks visually.

Installing Grsync on Ubuntu

sudo apt update
sudo apt install grsync

Basic Usage

  • Specify source and destination paths
  • Select rsync options via checkboxes
  • Configure exclusion patterns
  • Run dry runs safely

Profile-Based Management

Grsync allows saving configurations as profiles, making it easy to manage multiple backup or synchronization tasks.

Pros and Cons

ProsCons
Intuitive GUI, no commands requiredSome advanced options are not available
Easy to understand optionsLess detailed output than terminal usage
Safe for beginnersLess flexible than direct rsync usage

7. Common Problems and Solutions

File Permissions Are Not Preserved

sudo rsync -a /source/ /destination/

Symbolic Links Are Not Handled as Expected

  • Preserve links: use -a
  • Copy actual files: use --copy-links

“Permission denied” Errors

sudo rsync -a /source/ /destination/

Connection Errors During Remote Sync

  • Verify SSH connectivity
  • Check destination paths and permissions

“Argument list too long” Errors

  • Use include/exclude rules
  • Split synchronization into smaller batches

8. FAQ

Q1. How do I preserve ownership and permissions?

A. Use the -a option.

Q2. How can I exclude specific files?

A. Use --exclude.

Q3. How do I limit bandwidth usage?

A. Use --bwlimit.

Q4. Can I preview actions before execution?

A. Use --dry-run.

Q5. Are hidden files copied?

A. Yes, by default.

Q6. What is the difference between rsync and scp?

A. rsync supports delta transfers and resume capabilities, making it more efficient.

9. Summary

This article covered rsync installation, basic and advanced usage, remote synchronization, GUI tools, troubleshooting, and FAQs.

Why rsync Is Essential

  • Fast delta-based transfers
  • Preserves permissions and ownership
  • Secure remote synchronization
  • Highly customizable with many options
  • Supports automation via cron
  • Beginner-friendly with GUI tools

rsync is a fundamental tool for Linux users. By mastering its basics and gradually applying advanced options, you can achieve professional-level file management and backup workflows.