VOOZH about

URL: https://thenewstack.io/linux-synchronize-local-and-remote-directories-with-rsync/

⇱ Linux: Synchronize Local and Remote Directories With Rsync - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-07-13 05:00:09
Linux: Synchronize Local and Remote Directories With Rsync
Linux / Storage

Linux: Synchronize Local and Remote Directories With Rsync

How do you synchronize two Linux directories on your local network? Using a combination of rsync and cron will never fail you. Here's how.
Jul 13th, 2024 5:00am by Jack Wallen
👁 Featued image for: Linux: Synchronize Local and Remote Directories With Rsync
Feature image background via Unsplash.

If you manage multiple Linux servers, you might find yourself in a situation where you have two machines with directories that need to remain in sync with one another. This can be used as a backup solution or maybe you have different servers assigned to different users but have that one directory housing data for which all users need to have access.

There are several use cases for such a setup and one tool to make setting it up quite simple. That tool is rsync.

Rsync stands for remote sync and has been around for a long, long time. I’ve used rsync as a reliable (scriptable) backup solution on Linux but have also found instances where it was necessary that local and remote directories are always in sync with one another.

Nearly every Linux distribution ships with rsync, so there’s nothing to install; it’s just a matter of configuring everything. Of course, if you find yourself with a distribution that doesn’t include rsync, I’ll show you how to take care of that.

Let’s get those directories in sync.

What You’ll Need

To make this work, you’ll need two Linux machines and a user with sudo privileges. Both machines should be configured with static IP addresses, so you don’t have to worry about either address changing, which would cause the sync to fail.

With that in mind, let’s get to work.

Installing Rsync

Just in case you run into a situation where rsync isn’t installed, here’s how you can do it on both Debian and Fedora-based distributions:

  • Debian – sudo apt-get install rsync -y
  • Fedora – sudo dnf install rsync -y

Once rsync is installed, make sure it’s started and enabled with the command:

sudo systemctl enable --now rsync

Rsync is now ready for configuration.

Configuring the Destination

Let’s say we have two different Linux servers:

  • 192.168.1.115 — source
  • 192.168.1.116 — destination

The first thing we’ll do is create a directory that will house the synced data. Let’s do that on the destination server with the command:

sudo mkdir /data

Still, on the destination server, we need to create an rsync configuration file. Create a new file to house the configuration with the command:

sudo nano /etc/rsyncd.conf

In that file, paste the following content:

Where DIRECTORY is our newly created/data directory (or whatever you named it) and SOURCE_IP is the IP address of the source machine.

Let’s break this down.

  • [backup] – this is the title of the block that will be used when syncing from the source.
  • path
  • path – the full path to the directory that will house the synced data.
  • hosts allow – the IP address of the source machine.
  • hosts deny – this instructs rsync that all other IP addresses are to be denied.
  • list – determines if the module is listed when a client requests.
  • uid – the user ID which can be set to root, nobody, or a Linux user.
  • gid – the group ID
  • read only – defines if clients can upload files or not.

Make sure to change DIRECTORY and SOURCE_IP. Once you’ve done that, save and close the file.

Test the Connection

What we’re going to do now is test to make sure rsync works as expected. On the source machine (the machine housing the directory that will be synced with /data on the destination server), run the command:

rsync -avz DIRECTORY DESTINATION_IP::backup

Let’s break that command down:

  • rsync – the main command.
  • -avz – options for archive, verbose output, and compress.
  • DIRECTORY – the directory on the source machine that is to be synced with the destination directory.
  • DESTINATION_IP – the IP address of the destination machine.
  • backup – the name of the job listed in the rsyncd.conf file.

After running this command, everything in the source directory should now be found in the destination directory. If that’s the case, you’ve successfully configured rsync. The only issue at the moment is that it’s currently capable of syncing manually. Let’s automate that process.

Automating Rsync

Linux has a very powerful tool for automation called cron. With cron, you can set up jobs that run at just about any interval you need. Let’s say you want those folders to be synced every five minutes. The first thing we must do is create a bash script that will be run by cron.

Create the bash script with the command:

nano rsync.sh

In that file, add the following:

#! /bin/bash
rsync -avz DIRECTORY DESTINATION_IP::backup

Where DIRECTORY to the full path of the directory and DESTINATION_IP is the IP address for the remote machine.

Once you’ve taken care of that, give the file executable permission with:

chmod u+x rsync.sh

Now, we create our cronjob. Open the cronjob editor with:

crontab -e

At the bottom of the file, add the following:

Make sure to change /path/to/rsync.sh to the exact path of the rsync.sh bash script. The > /dev/null 2>&1 section silences the output of the rsync command, otherwise, it would error out when the cronjob attempts to run.

Save and close the file.

The rysnc.sh script will now start running every five minutes, to ensure the destination directory is in sync with the source directory.

Two-Way Syncing

Let’s say you need both source and destination to be in sync with one another. To do that, it’s essentially the same, the only difference is you have to run rsync twice (which can be added to the rsync.sh script). We’ll do that with the -au options like so:

  • Source to destination – rsync -avz DIRECTORY DESTINATION_IP::backup
  • Destination to source – rsync -avz DESTINATION_IP::backup DIRECTORY

For this, our rsync.sh script will be:

Where DIRECTORY to the full path of the directory and DESTINATION_IP is the IP address for the remote machine.

Now, both local and remote directories will be in sync every five minutes (or whatever interval you’ve set for your cron job).

And that’s how you can use rsync to synchronize two Linux directories on your local network. Yes, there are easier ways to do this, but using rsync and cron is a quick and flexible method that will never fail you.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
The Linux Foundation is a sponsor of The New Stack. 
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.