Mastering Rsync: Fast File Sync Across LAN/WAN with Daemon and SSH Modes
This guide explains how to use rsync for rapid file synchronization over LAN or WAN, covering its algorithm, supported platforms, daemon and SSH operation modes, configuration details, practical command examples, automated cron jobs, and real‑time syncing with inotify‑tools.
1. Introduction
Rsync (remote synchronize) is a remote data synchronization tool that can quickly sync files between multiple hosts over LAN/WAN. It uses the rsync algorithm to transfer only the differences between files, making synchronization fast.
Rsync supports most Unix‑like systems (Linux, Solaris, BSD) and also has Windows versions such as cwRsync and Sync2NAS.
2. Principles
Rsync was originally created to replace rcp and is now maintained at rsync.samba.org; its configuration file format is similar to Samba’s.
It can operate via rsh or ssh, or in daemon mode. In daemon mode the rsync server listens on port 873.
When connecting, the server checks passwords; the first full transfer copies the whole file, subsequent runs transfer only incremental changes.
3. Features
Can mirror entire directory trees and file systems.
Preserves permissions, timestamps, and symbolic/hard links.
No special privileges required for installation.
Optimized workflow for high transfer efficiency.
Supports rsh, ssh, or direct socket connections.
Allows anonymous transfers.
4. SSH Mode
4.1 Local synchronization
# mkdir src
# touch src/{1,2,3,4}
# mkdir dest
# rsync -av src/ dest/ # sync contents of src to dest (excluding src itself)
# rsync -av src dest/ # sync src directory itself to dest
# rsync -avR src/ dest/ # same effect even with trailing slash4.2 LAN synchronization
# mkdir src
# touch src/{a,b,c,d}
# mkdir dest
# rsync -av 172.16.22.12:/data/test/src/ dest/ # remote to local, requires root password
# rsync -av src/ 172.16.22.12:/data/test/dest/ # local to remote
# rsync -av src 172.16.22.12:/data/test/dest/ # sync whole directory
# rm -rf src/d # delete a file
# rsync -av --delete src/ 172.16.22.12:/data/test/dest/ # delete extraneous files on target4.3 LAN sync with specific user
# useradd george
# passwd george
# mkdir /home/george/test
# touch /home/george/test/g{1,2,3,4}
# rsync -av src -e ssh -l george 172.16.22.12:/home/george # local to remote as george
# rsync -av 172.16.22.12:/home/george/test/g* -e ssh -l george -p 22 dest/5. Daemon Mode
5.1 Service start methods
5.1.1 Standalone daemon (heavy load)
# yum install rsync xinetd # install service
# /usr/bin/rsync --daemon5.1.2 xinetd managed daemon (light load)
# yum install rsync xinetd
# vim /etc/xinetd.d/rsync # set disable = no
# /etc/init.d/xinetd start
# chkconfig rsync on
# netstat -ntpl | grep 873 # verify service5.2 Configuration details
Both modes require an rsyncd.conf file, similar to Samba’s main config.
Global parameters include address, port (default 873), motd file, pid file, lockfile, log file, syslog facility, socket options, timeout, etc.
Module parameters define which directories are shared. Example module [web1]:
path = /data/test/src
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.22.12
hosts deny = *
list = yes
auth users = web
secrets file = /etc/web.passwd5.3 Server configuration
# vim /etc/rsyncd.conf # create configuration
uid = root
gid = root
use chroot = no
max connections = 5
timeout = 600
pid file = /var/run/rsyncd.pid
lockfile = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[web1]
path = /data/test/src
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.22.12
hosts deny =
list = yes
auth users = web
secrets file = /etc/web.passwd6. Testing
6.1 Client setup
# yum -y install rsync
# mkdir /data/test6.2 Sample commands
# rsync -avzP [email protected]::web1 /data/test/ # sync module to local
# rsync -avzP --delete [email protected]::web1 /data/test/ # mirror with delete
# rsync -avzP --delete /data/test/ [email protected]::web1 # upload
# rsync -avzP --delete /data/test/ [email protected]::web1/george # upload to subdir
# rsync -ir --password-file=/tmp/rsync.password [email protected]::web1 # list files
# rsync -avzP --exclude="*3*" --password-file=/tmp/rsync.password [email protected]::web1 /data/test/ # exclude pattern6.3 Using password file
# echo "123" > /tmp/rsync.password
# chmod 600 /tmp/rsync.password
# rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::web1 /data/test/6.4 Automated sync via cron
# crontab -e
10 0 * * * rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::web1 /data/test/7. Real‑time sync with Inotify‑tools
7.1 Install inotify‑tools
# yum install -y gcc
# mkdir /usr/local/inotify
# tar -xf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14
# ./configure --prefix=/usr/local/inotify/
# make && make install7.2 Set environment
# vim /root/.bash_profile # add PATH
export PATH=/usr/local/inotify/bin/:$PATH
# source /root/.bash_profile
# echo '/usr/local/inotify/lib' >> /etc/ld.so.conf
# ldconfig
# ln -s /usr/local/inotify/include /usr/include/inotify7.3 Monitoring script
#!/bin/bash
/usr/local/inotify/bin/inotifywait -mrq -e modify,create,move,delete,attrib /data/test/src |
while read events; do
rsync -a --delete /data/test/src/ 192.168.22.12:/data/test/dest
echo "`date +'%F %T'` Event: $events" >> /tmp/rsync.log 2>&1
doneMake the script executable, add it to /etc/rc.local for startup, and optionally run a similar script on the destination to achieve bidirectional synchronization.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
