================================================================================
                  RSYNC SERVER SETUP GUIDE
================================================================================
Author      : Tushar Sonawane - Nakshatra Systems
Date        : 09 September 2026

============================================================


Document Purpose : Step-by-step guide to install, configure, and run an
                    rsync server.

================================================================================
TABLE OF CONTENTS
================================================================================
 1. Overview
 2. Prerequisites
 3. Verifying / Installing rsync
 4. Configuring the rsync Daemon (rsyncd.conf)
 5. Creating the Secrets File (Authentication)
 6. Creating an rc.d Init Script
 7. Starting the rsync Service
 8. Enabling rsync at Boot
 9. Client Usage Examples
10. Running rsync over SSH (Alternative Method)
11. Security Best Practices
12. Troubleshooting
13. Quick Reference Commands

================================================================================
1. OVERVIEW
================================================================================
rsync is a fast, versatile file-copying and synchronization tool. It can run
in two modes:

    a) Daemon Mode  - rsync runs as a background service (rsyncd) and
                       clients connect using the syntax: rsync://server/module
    b) SSH Mode     - rsync tunnels through SSH, requiring no dedicated
                       daemon, using the syntax: user@server:/path

================================================================================
2. PREREQUISITES
================================================================================
    - Open network port 873 (default rsync daemon port) or SSH port 22

================================================================================
3. VERIFYING / INSTALLING RSYNC
================================================================================
rsync ships as part of "n" (networking) package series, so it is usually already
installed. Verify first:

    rsync --version
    which rsync

If it is missing, install it from your Slackware installation media/ISO:

    installpkg rsync-*.tgz


================================================================================
4. CONFIGURING THE RSYNC DAEMON (rsyncd.conf)
================================================================================
Create/edit the main configuration file:

    vi /etc/rsyncd.conf

Sample configuration:

    --------------------------------------------------------------------
    # Global settings
    uid = nobody
    gid = nogroup
    use chroot = yes
    max connections = 10
    pid file = /var/run/rsyncd.pid
    log file = /var/log/rsyncd.log

    # Module definition
    [backup]
        path = /srv/rsync/backup
        comment = Backup Directory
        read only = no
        list = yes
        auth users = rsyncuser
        secrets file = /etc/rsyncd.secrets
        hosts allow = 192.168.1.0/24
        hosts deny  = *
    --------------------------------------------------------------------

Field Reference:
    path          -> Directory shared by this module
    comment       -> Description shown to clients on listing
    read only     -> "no" allows clients to upload files
    auth users    -> Restricts access to specified users
    secrets file  -> File containing username:password pairs
    hosts allow   -> Whitelist of permitted client IPs/subnets
    hosts deny    -> Blacklist (use * to deny all others)

Create the shared directory:

    mkdir -p /srv/rsync/backup
    chown nobody:nogroup /srv/rsync/backup

================================================================================
5. CREATING THE SECRETS FILE (AUTHENTICATION)
================================================================================
    vi /etc/rsyncd.secrets

Add credentials in the format  username:password

    rsyncuser:StrongPassword123

Secure the file (rsync refuses to run if permissions are too open):

    chmod 600 /etc/rsyncd.secrets

================================================================================
6. CREATING AN RC.D INIT SCRIPT
================================================================================
Slackware controls daemons via scripts in /etc/rc.d/. Create the script:

    vi /etc/rc.d/rc.rsyncd

Contents:

    --------------------------------------------------------------------
    #!/bin/sh
    # /etc/rc.d/rc.rsyncd - start/stop/restart the rsync daemon

    RSYNC_PID=/var/run/rsyncd.pid

    rsyncd_start() {
        echo "Starting rsync daemon..."
        /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
    }

    rsyncd_stop() {
        echo "Stopping rsync daemon..."
        [ -f $RSYNC_PID ] && kill $(cat $RSYNC_PID)
    }

    rsyncd_restart() {
        rsyncd_stop
        sleep 1
        rsyncd_start
    }

    case "$1" in
        start)
            rsyncd_start
            ;;
        stop)
            rsyncd_stop
            ;;
        restart)
            rsyncd_restart
            ;;
        *)
            echo "Usage: $0 {start|stop|restart}"
            exit 1
            ;;
    esac
    --------------------------------------------------------------------

Make the script executable:

    chmod 755 /etc/rc.d/rc.rsyncd

================================================================================
7. STARTING THE RSYNC SERVICE
================================================================================
Start manually:

    /etc/rc.d/rc.rsyncd start

Stop:

    /etc/rc.d/rc.rsyncd stop

Restart (after config changes):

    /etc/rc.d/rc.rsyncd restart

Confirm it is listening:

    netstat -tulnp | grep 873
    (or)  ss -tulnp | grep 873

================================================================================
8. ENABLING RSYNC AT BOOT
================================================================================

Make rsyncd script executable:
chmod +x /etc/rc.d/rc.rsyncd

Verify:
ls -l /etc/rc.d/rc.rsyncd
-rwxr-xr-x 1 root root 610 Jul 17 05:59 /etc/rc.d/rc.rsyncd*



================================================================================
9. CLIENT USAGE EXAMPLES
================================================================================
List available modules on the server:

    rsync rsync://server_ip_or_hostname/

Sync FROM server TO local machine:

    rsync -avz rsyncuser@server_ip::backup /local/destination/

Sync FROM local machine TO server:

    rsync -avz /local/source/ rsyncuser@server_ip::backup

Using a password file (avoids interactive prompt):

    echo "StrongPassword123" > ~/.rsync_pass
    chmod 600 ~/.rsync_pass
    rsync -avz --password-file=/home/tushars/.rsync_pass /local/source/ rsyncuser@server_ip::backup

Common Flags:
    -a   Archive mode (preserves permissions, timestamps, symlinks, etc.)
    -v   Verbose output
    -z   Compress data during transfer
    -P   Show progress and enable resume of partial transfers
    -n   Dry run (simulate without making changes)
    --delete   Remove files at destination that no longer exist at source

================================================================================
10. RUNNING RSYNC OVER SSH (ALTERNATIVE METHOD)
================================================================================
No daemon required; uses existing SSH access 
    rsync -avz -e ssh /local/source/ user@server_ip:/remote/destination/

With a custom SSH port:

    rsync -avz -e "ssh -p 2222" /local/source/ user@server_ip:/remote/destination/

Using SSH keys (passwordless) is strongly recommended:

    ssh-keygen -t ed25519
    ssh-copy-id user@server_ip

Ensure sshd is enabled at boot (check /etc/rc.d/rc.sshd is executable):

    chmod 755 /etc/rc.d/rc.sshd
    /etc/rc.d/rc.sshd start

================================================================================
11. SECURITY BEST PRACTICES
================================================================================
    - Always set "hosts allow" to restrict client IP ranges
    - Use "auth users" and a secrets file rather than anonymous access
    - Keep secrets file permissions at 600
    - Prefer SSH-tunneled rsync for internet-facing transfers
    - Enable logging (log file directive) and monitor /var/log/rsyncd.log
    - Use "read only = yes" for modules that should not accept uploads
    - Consider a chroot jail (use chroot = yes) to limit filesystem exposure
    - Keep rsync updated via slackpkg or manual source builds
    - Restrict port 873 at the iptables level to trusted networks only

================================================================================
12. TROUBLESHOOTING
================================================================================
Problem: "rsync: failed to connect to server: Connection refused"
Fix    : Ensure rc.rsyncd was started and port 873 is open in iptables.

Problem: "@ERROR: auth failed on module"
Fix    : Verify username/password in rsyncd.secrets match client credentials.

Problem: "@ERROR: chdir failed"
Fix    : Check that the "path" directory in rsyncd.conf exists and has
         correct ownership/permissions.

Problem: Permission denied errors during sync
Fix    : Confirm uid/gid settings in rsyncd.conf and directory ownership.

Problem: rsyncd does not start automatically after reboot
Fix    : Confirm the rc.rsyncd call was added to /etc/rc.local and that
         both scripts are executable (chmod 755).

Check logs for details:

    tail -f /var/log/rsyncd.log

================================================================================
13. QUICK REFERENCE COMMANDS
================================================================================
    Start service        : /etc/rc.d/rc.rsyncd start
    Stop service          : /etc/rc.d/rc.rsyncd stop
    Restart service       : /etc/rc.d/rc.rsyncd restart
    Test config syntax    : rsync --daemon --config=/etc/rsyncd.conf --no-detach
    View logs             : tail -f /var/log/rsyncd.log
    Check listening port  : netstat -tulnp | grep 873

================================================================================