Skip to content

NFS replication setup between two Linux servers

NFS remains one of the simplest protocols for sharing directories between Linux machines, but reliable replication requires careful export configuration, mount options, and data synchronization. This note outlines a step-by-step setup of NFS replication between two servers, using rsync for initial sync and systemd for automatic mount management.

Preparation of hosts and package installation

On both servers (server and client) update package lists and install the corresponding packages. The server side needs nfs-kernel-server, the client — nfs-common.

# On the server
sudo apt update && sudo apt install -y nfs-kernel-server

# On the client
sudo apt update && sudo apt install -y nfs-common

Ensure ports 111 (rpcbind), 2049 (nfs) and 20048 (mountd) are open in the firewall. A typical UFW configuration:

sudo ufw allow from 10.0.0.0/8 to any port 111 proto tcp
sudo ufw allow from 10.0.0.0/8 to any port 111 proto udp
sudo ufw allow from 10.0.0.0/8 to any port 2049 proto tcp
sudo ufw allow from 10.0.0.0/8 to any port 2049 proto udp
sudo ufw reload

NFS server (exports) setup

The main NFS server configuration is in /etc/exports. Each line specifies a directory, client list, and mount options. A typical record for replication:

/srv/nfs/share 10.0.0.0/24(rw,sync,no_subtree_check,no_root_squash)

Common flags in /etc/exports:

FlagDescription
rwRead and write access
syncResponse to client only after write to server disk
no_subtree_checkDisables subtree check (recommended for exports not being subdirectories of other filesystems)
no_root_squashPreserves client root rights on server (insecure for multi‑user)
all_squashMaps all user requests to nobody (useful for anonymous access)
no_wdelayDisables write delay merging (rarely needed in modern kernels)

After changes, restart the service and verify exports:

sudo exportfs -a
sudo exportfs -v

NFS client setup and mounting

On the client create a mount point and mount the export. Export options rw,sync,no_subtree_check on the server correspond to mount options, but hard or soft are often added for behavior on network failures.

sudo mkdir -p /mnt/nfs/share
sudo mount -t nfs 192.0.2.1:/srv/nfs/share /mnt/nfs/share

A temporary filesystem can be mounted alongside NFS (e.g., for a cache):

sudo mount -t tmpfs -o size=1G tmpfs /mnt/nfs/cache

Verify accessibility:

showmount -e 192.0.2.1

Initial synchronization via rsync

NFS provides filesystem access, but reliable replication often requires content synchronization. rsync is efficient for moving data and subsequent incremental updates.

rsync -av --progress /local/path/ /mnt/nfs/share/

The -a flag preserves attributes, owner, and timestamps; --delete can be added to remove files no longer present in the source. After the first sync, cron jobs or systemd timers can be configured for rsync sync every N minutes.

Automatic mounting via systemd

To restore mounting after reboot, create a systemd unit file on the client. Example at /etc/systemd/system/nfs-share.mount:

[Unit]
Description=NFS Share Mount
After=network.target rpcbind.service
Wants=rpcbind.service

[Mount]
Type=nfs
Options=rw,sync,no_subtree_check
Device=192.0.2.1:/srv/nfs/share
Directory=/mnt/nfs/share

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable --now nfs-share.mount

Check status:

systemctl status nfs-share.mount

Integrity check and typical errors

After configuration verify data integrity. A common method — comparing file checksums between source and mount:

find /data/source -type f -exec md5sum {} \; > /tmp/source_checksums.txt
find /mnt/nfs/share -type f -exec md5sum {} \; > /tmp/mounted_checksums.txt
diff /tmp/source_checksums.txt /tmp/mounted_checksums.txt

Typical errors and ways to resolve them:

  1. uid/gid mismatch between hosts — if users have different numeric IDs on server and client, permissions may appear different. Use all_squash on server or explicitly set anonuid/anongid in mount options.
  2. fstab errors on reboot — always test systemd mount before commenting out /etc/fstab. Incorrect options can cause boot delays or mount.nfs errors.
  3. Ignoring firewall — NFS depends on rpcbind and many dynamic ports. Use showmount -e for diagnostics and keep UFW/Selinux rules current.
  4. subtree_check issues — exporting a subdirectory may cause false “permission denied” on file access. Add no_subtree_check in /etc/exports if observed.

Automated integrity checks can be wrapped in a systemd timer for rsync sync:

[Unit]
Description=NFS rsync sync

[Timer]
OnBootSec=5min
Persistent=true

[Service]
Type=oneshot
ExecStart=/usr/bin/rsync -av --delete /data/source/ /mnt/nfs/share/

Activate the timer:

systemctl enable --now nfs-sync.timer

NFS replication between two servers is now configured. Primary risks (uid/gid mismatch, firewall misconfiguration, fstab errors) are acknowledged, and data synchronization is maintained via rsync combined with systemd automation.