Home Linux Commandline Tools Lsyncd – Synchronize Local Directories with Remote Linux

Lsyncd – Synchronize Local Directories with Remote Linux

Lsyncd (Live Syncing Mirror Daemon) is a lightweight Linux solution for synchronizing remote and local directories. The use of this Linux solution does not affect the performance of your local filesystem in any way.

[ You might also like: How to Sync Two Web Servers in Linux Automatically ]

Once you fully install and configure Lsyncd, your local and remote directories will be synced such that any data modification on the local directory is noted and updated on the remote server directory. The frequent directory updates and synchronization actions ensure that the targeted local and remote directories remain identical.

Install Lsyncd in Linux Server

This Lsyncd synchronization solution is supported across various Linux operating system platforms. Depending on the Linux distribution you are using, you can install Lsyncd from one of the following commands:

Install Lsyncd in RHEL-based Distributions

$ sudo yum install epel-release
$ sudo yum install lsyncd

Install Lsyncd in Fedora Linux

$ sudo dnf install lsyncd 

Install Lsyncd in Debian, Ubuntu & Mint

$ sudo apt install lsyncd

Install Lsyncd in OpenSUSE

$ sudo zypper install lsyncd

Install Lsyncd in Arch Linux

$ sudo pacman -S git base-devel
$ git clone https://aur.archlinux.org/lsyncd.git
$ cd lsyncd
$ makepkg -sri

Using Lsyncd to Synchronize Local and Remote Directories in Linux

You need to be on your local machine and have authenticated access to a remote machine or server for two-way directory syncing to be successful. On my end, the remote server I am using has the IP address 18.118.208.XX.

On the local machine, we need to set up passwordless ssh login to the mentioned remote machine/server.

Passwordless ssh login enables the automation of Lsyncd’s local-to-remote directories synchronization to be seamless.

Set Up SSH Passwordless Login to Remote Linux

1. Generate the SSH key on the local machine for passwordless access to the remote server.

$ sudo ssh-keygen -t rsa  

When prompted to enter a passphrase, leave the field blank by hitting [Enter] on your keyboard.

Create SSH Key in Linux
Create SSH Key in Linux

2. Copy the generated SSH public key to your remote machine. We now need to transfer a copy of the generated SSH public key to the mentioned remote server/machine.

$ sudo ssh-copy-id [email protected]
Copy SSH Key to Remote Linux
Copy SSH Key to Remote Linux

With this achievement, any future access to this remote server (18.118.208.79) via the SSH command (ssh [email protected]) should be passwordless.

$ ssh [email protected]
SSH Passwordless Login in Linux
SSH Passwordless Login in Linux

You might also want to make sure your remote machine’s /etc/ssh/sshd_config file has PubKeyAuthentication set to yes and PermitRootLogin set to without-password.

Configure SSH in Linux
Configure SSH in Linux

Restart the sshd service on the remote machine.

$ sudo systemctl restart ssh.service

If the PubKeyAuthentication and PermitRootLogin fields were not enabled on your server and you managed to enable them, begin from step 1 to re-generate the public SSH key and then re-copy it (step 2) to the remote server.

Synchronize Local Directories with Remote Using Lsyncd

Create the needed remote/destination directory on your remote machine

$ mkdir linuxshelltips_remotesync

Create the needed source directory on your local machine.

$ mkdir linuxshelltips_localsync

Populate this source directory with some files:

$ sudo touch linuxshelltips_localsync/file{1..16}
Create Files in Linux
Create Files in Linux

Create Lsyncd Log and status files.

$ sudo mkdir /var/log/lsyncd
$ sudo touch /var/log/lsyncd/lsyncd.{log,status}

Create associated lsyncd configuration directory.

$ sudo mkdir /etc/lsyncd 

Under this directory, create the lsyncd.conf.lua configuration file.

$ sudo nano /etc/lsyncd/lsyncd.conf.lua

Populate it with data similar to the following screen capture

----
-- User configuration file for lsyncd.
----
settings {
        logfile = "/var/log/lsyncd/lsyncd.log",
        statusFile = "/var/log/lsyncd/lsyncd.status"
}

sync  {
        default.rsyncssh,
        source = "/home/dnyce/linuxshelltips_localsync",
        host = "[email protected]",
        targetdir = "/home/ubuntu/linuxshelltips_remotesync"
}
Lsyncd Configuration
Lsyncd Configuration

On this file, the source is the path to the files we want to sync on the local machine, host points to the username+IP address of the remote server, and targetdir is the destination directory on the remote server.

Next, start and enable Lsyncd.

$ sudo systemctl restart lsyncd 
$ sudo systemctl enable lsyncd

Access remote server to confirm the presence of the synced directory files. See if any file synchronization took place.

$ sudo ssh [email protected]
Confirm File Synchronization in Linux
Confirm File Synchronization in Linux

As you can see, the ls command before starting lsyncd on local machine displayed zero files and the one after running it displays the copied files from the local machine.

You only need to install Lsyncd on the host/source machine with the directory files you need to synchronize. It is easy to implement and gives you a file synchronization solution that works for local-to-remote machines and also local-to-local directories.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

4 thoughts on “Lsyncd – Synchronize Local Directories with Remote Linux”

  1. If lsyncd is a daemon running all time, why the need for a cron job? At startup the lysncd process could read in the equivalent information for the remote filesystem and time interval from a configuration file and then do the syncing to the remote filesystem at the interval specified without cron being involved.

    Alternatively, why is lysncd needed when one could just put an rsync command to synchronize the remove filesystem at a specified interval in cron.d entry? Nowhere in your article do you explain what is the benefit of running lsyncd over that method or its deficiency in having to be activated by a cron job.

    In fact when I visit the lsyncd website and read the documentation, nowhere is there mention of using cron — just set up the configuration file with the target director, the time interval, and also the transfer method to use eg rsync or rsyncssh.

    Reply
    • Hi Rhea,

      The article has been updated. The use of a cron job has been dismissed and replaced by a Lsyncd configuration file where the local-to-remote file transfer method is rsyncssh. The Lsyncd Daemon now handles the automation of local-to-remote file synchronization tasks.

      Reply
  2. So what exactly is the benefit of this program over a plain.

    */5 * * * * rsync -avh /home/dnyce/linuxshelltips_localsync/ [email protected]:/home/ubuntu/linuxshelltips_remotesync/ 1>/dev/null 2>&1 &

    The daemon is not syncing bi-directionally and one would expect a daemon to handle its own scheduling, but even that is farmed out to corn.

    So once again, What is the point of this command?

    Reply
    • Hi Fredrick, the article has been updated. The cron job approach has been removed and replaced with a Lsyncd configuration file that answers to a Daemon. We can comfortably conclude that Lsyncd’s local-to-remote directory synchronization is now fully automated and working as it should be.

      Reply

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.