How to Use inotifywait and rsync for Live File Sync Between Linux Servers
When managing multiple Linux servers, keeping files synchronized in real time can be challenging. Manual syncing or running periodic cron jobs may not be efficient, especially if your files change often. Fortunately, you can automate this process using two powerful Linux tools: inotifywait and rsync.
In this guide, we’ll show you how to set up a real-time file synchronization system between two Linux servers, ensuring that whenever a file changes on the first server, it’s instantly updated on the second one. This solution is lightweight, reliable, and easy to implement.
What You’ll Need for This Setup
Before we dive into the steps, let’s make sure you have everything ready.
- Two Linux Servers (VPS1 and VPS2): One server will be the source, where files will be modified or created, and the other will be the destination, where those changes will be synced.
- Root or Sudo Access: You’ll need root permissions on both servers.
- SSH Access: SSH must be set up between the servers to allow file syncing without passwords.
Why inotifywait + rsync?
While tools like rsync alone are great for copying files, they don’t “watch” for changes. That’s where inotifywait shines—it monitors filesystem events. Pair the two, and you’ve got an automatic, real-time sync setup without heavy software.
Step 1 – Set up SSH keys so you don’t need passwords
We want Server A to send files to Server B without typing a password every time.
On Server A (source):
1. Generate an SSH key pair if you don’t already have one:
ssh-keygen -t rsa
Just hit Enter through the prompts unless you want to change the default location.
2. Copy the key to Server B:
ssh-copy-id root@your_server_b_ip
You’ll type the password for Server B one last time.
3. Test it:
ssh root@your_server_b_ip
If you log in without a password, you’re good to go.
Step 2 – Install inotify-tools on Server A
This package gives you the inotifywait command.
- For Ubuntu/Debian:
apt update && apt install inotify-tools -y - For CentOS/RHEL/AlmaLinux:
yum install inotify-tools -y
Step 3 – Create the sync script
This script will watch a folder and run rsync whenever something changes.
1. Create the script file:
nano /usr/local/bin/realtime-sync.sh
2. Paste this inside:
#!/bin/bash
# Folder to watch on Server A
SOURCE="/var/www/html/"
# Destination on Server B (change the IP!)
DEST="root@your_server_b_ip:/var/www/html/"
# Watch for changes and sync when they happen
while inotifywait -r -e modify,create,delete,move "$SOURCE"; do
rsync -az --delete "$SOURCE" "$DEST"
done
What’s happening here?
- SOURCE is the folder you want to monitor (e.g., /var/www/html).
- DEST is where files go on Server B (update the IP!).
- inotifywait watches for edits, new files, deletions, and moves.
- rsync -az –delete syncs with compression, preserves permissions, and removes files on Server B if they’re deleted on Server A.
3. Make it executable:
chmod +x /usr/local/bin/realtime-sync.sh
Step 4 – Run it as a service (so it survives reboots)
Let’s create a systemd service to keep the script running.
- Create the service file:
nano /etc/systemd/system/realtime-sync.service
- Add this:
[Unit]
Description=Real-Time File Sync from Server A to Server B
After=network.target
[Service]
ExecStart=/usr/local/bin/realtime-sync.sh
Restart=always
User=root
[Install]
WantedBy=multi-user.target
- Start and enable it:
systemctl daemon-reload
systemctl enable realtime-sync
systemctl start realtime-sync
- Check if it’s running:
systemctl status realtime-sync
You should see active (running).
Step 5 – Test it!
On Server A, create a test file:
- echo “Hello from Server A!” > /var/www/html/test.txt
Now hop over to Server B and check the same folder:
- ls /var/www/html/
You should see test.txt there almost immediately.
How it all works under the hood
- inotifywait sits and watches your source folder.
- The moment a change happens, it triggers the rsync command.
- rsync copies only what’s changed (efficiently).
- The systemd service keeps the script running 24/7, and it restarts if anything goes wrong.
Wrapping up
And that’s it! You’ve now got a real-time, light-weight sync running between your Linux servers. No more manual updates or waiting for cron jobs—it just works.
Want to customize it further? You could:
- Exclude certain files or folders (like cache/ or *.log).
- Add two-way sync (by setting up a similar process in reverse).
- Log changes to a file for auditing.
- Watch multiple directories.
Ideal for rsync, backups, automation, and real-time sync workloads.