How to Install and Configure LSYNCD on RHEL 8, RHEL 7, CentOS 7,  Debian & Ubuntu

How to Install and Configure LSYNCD on RHEL 8, RHEL 7, CentOS 7, Debian & Ubuntu

How to Install and Configure LSYNCD on RHEL 8, RHEL 7, CentOS 7, Debian & Ubuntu

What is lsyncd?

Lyncd is used to sync (replicate) your local directory with remote machines. It can also be used to sync other local directory on your machine. Lsyncd stands for “Live Syncing Daemon”. It uses rsync and ssh at the backend.

For doing sync we require source and destination folders. If any changes detects on source directory lsyncd will watch and will copy the same to destination directory using rsync.

Lets start with our quick tutorial How to install and configure Lsyncd on RHEL 8, RHEL 7, CentOS 7, Debian & Ubuntu.

Lets take a example we have 2 webservers, our master server or source server will be websrv1 and directory we will be syncing “/var/www/html”

Our servers details:

> Master server → websrv01.linuxproguru.com: 192.168.60.10
> Slave server → websrv02.linuxproguru.com: 192.168.60.11

Directory to sync: /var/www/html

Step1: Configure SSH Key Based Authentication

Before getting start with lsyncd, we will configure password less ssh access from Master server to Slave server.

Login to master server and generate ssh keys from root user:

[mdmunazir@mdmunazir ~]# ssh root@srv1.linuxproguru.com
[root@websrv01 ~]# yum install lsyncd

If you want to rsync your directory using different user generate ssh keys from <username>:

[mdmunazir@mdmunazir ~]# ssh prodsync@srv1.linuxproguru.com
[prodsync@websrv01 ~]# ssh-keygen -t rsa

Copy generated SSH Public key to slave server using ssh-copy-id:

Root user:

[root@websrv01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@websrv02.linuxproguru.com

Other user:

[prodsync@websrv01 ~]# ssh-copy-id -i /home/prodsync/.ssh/id_rsa.pub prodsync@websrv02.linuxproguru.com

Step2: Installing Lsyncd

RHEL and CentOS:

Adding epel repository

[root@websrv01 ~]# yum install epel-release

Install Lsyncd

[root@websrv01 ~]# yum install lsyncd

Debian & Ubuntu:

Install Lsyncd:

[root@websrv01 ~]# apt-get update
[root@websrv01 ~]# apt-get install lsyncd

Step3: Configuration of Lsyncd

After successfully configuring ssh keys, and installation we will start with configuration of lsyncd.conf or lsyncd.conf.lua (ubunu). We will start with create logs file and status file for lsyncd in lsynd respective directories.

[root@websrv01 ~]# touch /var/log/lsyncd/lsyncd.log
[root@websrv01 ~]# touch /var/log/lsyncd/lsyncd-status.log

Create lsyncd.conf or lsyncd.conf.lua for lsync configuration:

lsyncd.conf for root user:

----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
-- 
settings {   logfile = "/var/log/lsyncd/lsyncd.log",
    statusFile = "/var/log/lsyncd/lsyncd-status.log",     statusInterval = 20 } sync {     default.rsync,     source="/var/www/html",     target="root@192.168.60.11:/var/www/html",
    rsync = {         archive = false,         acls = false,
        chmod = "D2755,F644",
        compress = true,
        links = false,
        owner = false,         perms = false,
        verbose = true,         rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"     }
}   

lsyncd.conf for prodsync user:


----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--

     settings {
                 logfile = "/var/log/lsyncd/lsyncd.log",
                 statusFile = "/var/log/lsyncd/lsyncd.status"
              }
     
     sync {
                 default.rsyncssh,
                 source = "/var/www/html",
                 host = "prodsync@192.168.60.11",
                 targetdir = "/var/www/html",

          rsync = {
                rsh = "/usr/bin/ssh -l prodsync -i /home/prodsync/.ssh/id_rsa -o StrictHostKeyChecking=no"
         }
}

Step 4: Service enable and start

RHEL and CentOS:

[root@server01 ~]# systemctl enable lsyncd
[root@server01 ~]# systemctl start lsyncd

Ubuntu:

[root@server01 ~]# systemctl enable lsyncd
[root@server01 ~]# systemctl start lsyncd

Final Step:

We can check status of syncing of required directories get success or not and logs.

[root@server01 ~]# tail -f /var/log/lsyncd/lsyncd.log
[root@server01 ~]# tail -f /var/log/lsyncd/lsyncd-status.log

Summary

We have successfully completed "How to Install and Configure LSYNCD on RHEL 8, RHEL 7, CentOS 7, Debian & Ubuntu". If you have any questions or comments please leave them here, or in linuxproguru.com comments section of this site.

Leave a Reply

Your email address will not be published. Required fields are marked *