How to Set Up an Rsync Daemon on Your Linux Server

Setting up an Rsync Daemon

First, using your preferred text editor, you’ll need to create the configuration file /etc/rsyncd.conf, if you do not have one already. Below is an example of our basic configuration parameters and explanations of each one.
transfer logging = true

log format = %h %o %f %l %b
log file = /var/log/rsyncd.log
motd file = /etc/motd
[etc]
        path = /etc
        comment = An Example
        auth users = root
        secrets file = /etc/rsyncd.secrets
        read only = no
        uid = root
        gid = root
        use chroot = yes
[home]
        path = /home/
        comment = An Example
        auth users = root
        secrets file = /etc/rsyncd.secrets
        read only = no
        uid = root
        gid = root
        use chroot = yes
[usr]
        path = /usr/
        comment = An Example
        auth users = root
        secrets file = /etc/rsyncd.secrets
        read only = no
        uid = root
        gid = root
        use chroot = yes
[var]
        path = /var/
        comment = An Example
        auth users = root
        secrets file = /etc/rsyncd.secrets
        read only = no
        uid = root
        gid = root
        use chroot = yes
[mnt]
        path = /mnt/
        comment = An Example
        auth users = root
        secrets file = /etc/rsyncd.secrets
        read only = no
        uid = root
        gid = root
        use chroot = yes
We can divide this file in two sections, the global parameters and the modules section. The global parameters define the overall behavior of rsync. Besides the three parameters that I use here and which I explain below, we can also configure things such as the port rsync will listen too, but we are going to go with the default 873.
  • lock file is the file that rsync uses to handle the maximum number of connections
  • log file is where rsync will save any information about it’s activity; when it started running, when and from where does other computers connect, and any errors it encounters.
  • pid file is where the rsync daemon will write the process id that has been assigned to it, this is useful because we can use this process id to stop the daemon.
After the global parameters, we have the modules section, every module is a folder that we share with rsync, the important parts here are:
  • [name] is the name that we assign to the module. Each module exports a directory tree. The module name can not contain slashes or a closing square bracket.
  • path is the path of the folder that we are making available with rsync
  • comment is a comment that appears next to the module name when a client obtain the list of all available modules
  • uid When the rsync daemon is run as root, we can specify which user owns the files that are transferred from and to.
  • gid This allows us to set the group that owns the files that are transferred if the daemon is run as root
  • read only determines if the clients who connect to rsync can upload files or not, the default of this parameter is true for all modules.
  • list allows the module to be listed when clients ask for a list of available modules, setting this to false hides the module from the listing.
  • auth users is a list of users allowed to access the content of this module, the users are separated by comas. The users don’t need to exist in the system, they are defined by the secrets file.
  • secrets file defines the file that contains the usernames and passwords of the valid users for rsync
  • hosts allow are the addresses allowed to connect to the system. Without this parameter, all hosts are allowed to connect.

Creating the secrets file

Once rsyncd.conf is properly set, we need to create the secrets file. This file contains all of the usernames and passwords that will be able to log in to the rsync daemon, this usernames and passwords are independent of the user that exist in the system, so we can create users whom already exist in the system without problems. As we specified the file /etc/rsyncd.secrets in rsyncd.conf, we will create and edit this file it in our favorite text editor:sudo gedit /etc/rsyncd.secrets
In this file we add the usernames and the passwords, one per line, separated by a colon (I don’t actually use passwords that are this simple, and you shouldn’t either):root:kailash
username:PassWord
backup:Password
user:password
Finally, change the permission of this file so it can’t be read or modified by other users, rsync will fail if the permissions of this file are not appropriately set:sudo chmod 600 /etc/rsyncd.secrets

Launching rsync with the –daemon attribute

Once everything is set, one of the ways to use rsync as a daemon is launching it with the --daemon parameter, if you followed the previous instructions you can simply use this command:sudo rsync --daemon
We can check if it is running by seeing the log file that we defined in rsyncd.conf, in our example this is located in /var/log/rsyncd.log. Additionally, if the daemon is running, the file /var/run/rsyncd.pid will contain the process ID of rsync.
If we launched rsync in this manner, we can stop it by killing its process. We can obtaining the process ID by reading the contents of the file /var/run/rsyncd.pid and then invoke kill with this process ID. We can pass it directly to kill using:sudo kill `cat /var/run/rsyncd.pid`

Connecting to the rsync daemon

To connect to rsync when it is running as a Daemon, instead of use a colon as we do when using SSH, we need to use a double colon, followed by the module name, and the file or folder that we want to copy or synchronize, we can use:rsync -rtv user@host::module/source/ destination/
Another way to access the file would be using rsync:// followed by the host’s address, the module, and finally the location of file or folder that we want to access:rsync -rtv rsync://user@host/module/source/ destination/
For example, taking the parameters given in the example of rsyncd.conf that I posted, a way to transfer a folder called “source” inside the folder /home/juan/Documents of the host computer, would be using any of this two parameters, assuming the host is located at 192.168.1.100rsync -rtv juan@192.168.1.100::documents/source/ destination/
rsync -rtv rsync://juan@192.168.1.100/documents/source/ destination/
Just remember that the user that appears there is one of the users that we defined in /etc/rsyncd.secrets and not a user of the host computer.
Very useful for scripting is to use --password-file command line option.
  • Create empty file called rsync_pass
  • write in password to this file (nothing more)
  • chmod 600 rsync_pass
  • rsync $args --password-file=rsync_pass user@rsynchost::/share localdirectory
This can be used for scripting and allows to be more secure that just exporting password to system variable.
rsync -avz  –password-file=/backup_script/Seha_137 -u  root@192.227.112.106::etc/ /backup3/allbackup/INDIA/192.227.112.106/DAILY/etc/ > /ba
ckup_script/INDIA/192.227.112.106/logs/etc.log  2>&1
rsync -avz –password-file=rsync_pass -u root@35.200.202.46::home

——————————————————————————————————————————–

Rsync daemon

1. /etc/rsync.conf

2./etc/rsync.secrets

3. create password file in the machine where you are going to pull files

4. rsync –daemon

Leave a Comment

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

To Resolve this issue at just 5$ from ServerExpert

https://serverexpert.io/

For Hosting please visit Hostingwebsite.io

You have Successfully Subscribed!

Open chat