> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/ask/flat/chapter-37-servers/di-37.1-jie-rsync-shu-ju-tong-bu.md).

# 37.1 Rsync Data Synchronization

Rsync (Remote Sync) is an efficient file synchronization tool co-developed by Andrew Tridgell and Paul Mackerras. Rsync uses an incremental transfer algorithm that only transmits the differences between files, which can significantly reduce network bandwidth consumption and shorten synchronization time. It is a commonly used tool for system backup and data mirroring.

## Environment Overview

Before starting the configuration, first clarify the roles of the two servers in this section.

![Rsync Architecture Diagram](/files/8V8vuNy0qcgR8q2nx9FM)

* Server A (initiator, client): **192.168.179.128**
* Server B (sync source, server): **192.168.179.150**

Design objective: synchronize data from Server B to Server A, i.e., B (server) → A (client), to achieve a complete data mirror.

## Server B (Sync Source) Configuration

First, configure the sync source server, i.e., Server B.

### Installing rsync

Install rsync on Server B. There are two installation options:

* Install using pkg:

```sh
# pkg install rsync
```

* Or install using Ports:

```sh
# cd /usr/ports/net/rsync/
# make install clean
```

* View installation information

```sh
# pkg info -D rsync
```

### Sync Directory Preparation

```sh
# mkdir -p /home/test # Create a backup folder named test
# chown root /home/test/ # Set the folder owner to root
# cd /home/test/ # Change directory
# touch txt001 txt002 # Create test files in the folder
```

File structure:

```sh
/
├── home
│   ├── test                              # Sync source directory on Server B
│   │   ├── txt001                         # Test file 1
│   │   └── txt002                         # Test file 2
│   └── testBackUp                         # Local backup directory on Server A
├── usr
│   └── local
│       └── etc
│           └── rsync
│               └── rsyncd.conf           # rsyncd server main configuration file
├── var
│   ├── log
│   │   └── rsyncd.log                     # rsyncd service log
│   └── run
│       └── rsyncd.pid                     # rsyncd process ID file
└── etc
    └── rsyncd_users.db                     # rsync user authentication password file
```

### Server Main Configuration File

Edit the **/usr/local/etc/rsync/rsyncd.conf** file and write:

```ini
# System user running the service
uid = root

# System user group running the service
gid = wheel

# Lock within the source directory
use chroot = yes

# Listen address
address = 192.168.179.150

# Listen port, default is 873
port = 873

# Log file path
log file = /var/log/rsyncd.log

# PID file path
pid file = /var/run/rsyncd.pid

# Allowed client addresses
hosts allow = 192.168.179.128

# Shared module name, customizable
[testcom]

# Sync directory path, must match the user and group specified by uid/gid
path = /home/test

# Module description
comment = testcombackup

# Read-only or not
read only = yes

# File types not to compress during transfer
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# Authorized account
auth users = root

# User authentication password file path
secrets file = /etc/rsyncd_users.db
```

> **Tip**
>
> The **192.168.179.128** and **192.168.179.150** in the above example are placeholders and need to be replaced with actual values.

### Creating the Password File for Authorized Backup Account Authentication (Server)

* Edit the **/etc/rsyncd\_users.db** file and write:

```sh
root:12345678   # Supports multiple users, one per line
```

> **Note**
>
> The server password file should contain the mapping between usernames and passwords. The format is `authorized_account_username:password`.

* Restrict the data file permissions, otherwise it will cause errors:

```sh
# chmod 600 /etc/rsyncd_users.db
```

Set the rsync user database file permissions to be readable and writable only by the owner.

### Service Startup Configuration

```sh
# service rsyncd enable   # Set rsync service to start automatically at boot
# service rsyncd start    # Start the rsync service
```

### Service Port Verification

View the network ports used by the rsync service and the corresponding processes:

```sh
# sockstat | grep rsync
root     rsync       1198 5   tcp4   192.168.179.150:873   *:*
```

## Server A (Initiator) Configuration

Please refer to the steps above to install rsync.

### Local Backup Directory Configuration

Create the local backup directory **/home/testBackUp/** and set the appropriate permissions:

```sh
# mkdir -p /home/testBackUp                 # Create backup directory
# chown root:wheel /home/testBackUp/       # Set directory owner to root, group to wheel
```

### Sync Operation (Password Input Method)

Synchronize files to the local backup directory **/home/testBackUp/**, requiring manual password input.

```sh
# rsync -avz root@192.168.179.150::testcom /home/testBackUp  # Use rsync to sync files from the remote testcom module to the local backup directory
Password: # Enter the password set on Server B
receiving incremental file list
./
txt001
txt002

sent 65 bytes  received 151 bytes  86.40 bytes/sec
total size is 0  speedup is 0.00
```

`testcom` is the sync module name defined in the **/usr/local/etc/rsync/rsyncd.conf** file, corresponding to the directory on the server.

#### Appendix: Specifying a Password File Method

Create a password file for authorized backup account authentication (client).

* Edit the client's **/etc/rsyncd\_users.db** file, writing only the password:

```sh
12345678
```

* Restrict permissions, otherwise it will cause the error `ERROR: password file must not be other-accessible`.

```sh
# chmod 600 /etc/rsyncd_users.db
```

Set the rsync user database file permissions to be readable and writable only by the owner.

> **Note**
>
> The client password file should contain only the password itself, one per line.

Execute the sync command.

Use rsync to synchronize the remote `testcom` module to the local backup directory, display sync progress, and specify the password file.

```sh
# rsync -auvz --progress --password-file=/etc/rsyncd_users.db root@192.168.179.150::testcom /home/testBackUp
receiving incremental file list
./
txt001
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=1/3)
txt002
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=0/3)

sent 65 bytes  received 151 bytes  432.00 bytes/sec
total size is 0  speedup is 0.00
```

### Sync Result Verification

List detailed file information in the local backup directory:

```sh
# ls -l  /home/testBackUp
total 1
-rw-r--r--  1 root wheel 0 Apr 17 18:33 txt001
-rw-r--r--  1 root wheel 0 Apr 17 18:33 txt002
```

## References

* Tridgell A, Mackerras P. The rsync algorithm\[EB/OL]. (1998-11-09)\[2026-04-17]. <https://rsync.samba.org/tech_report/>. The original technical report on the rsync algorithm, co-authored by the two authors.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/ask/flat/chapter-37-servers/di-37.1-jie-rsync-shu-ju-tong-bu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
