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.
Ensure ports 111 (rpcbind), 2049 (nfs) and 20048 (mountd) are open in the firewall. A typical UFW configuration:
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:
Common flags in /etc/exports:
| Flag | Description |
|---|---|
rw | Read and write access |
sync | Response to client only after write to server disk |
no_subtree_check | Disables subtree check (recommended for exports not being subdirectories of other filesystems) |
no_root_squash | Preserves client root rights on server (insecure for multi‑user) |
all_squash | Maps all user requests to nobody (useful for anonymous access) |
no_wdelay | Disables write delay merging (rarely needed in modern kernels) |
After changes, restart the service and verify exports:
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.
A temporary filesystem can be mounted alongside NFS (e.g., for a cache):
Verify accessibility:
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.
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:
Enable and start the service:
Check status:
Integrity check and typical errors
After configuration verify data integrity. A common method — comparing file checksums between source and mount:
Typical errors and ways to resolve them:
- uid/gid mismatch between hosts — if users have different numeric IDs on server and client, permissions may appear different. Use
all_squashon server or explicitly setanonuid/anongidin mount options. - fstab errors on reboot — always test
systemd mountbefore commenting out/etc/fstab. Incorrect options can cause boot delays ormount.nfserrors. - Ignoring firewall — NFS depends on rpcbind and many dynamic ports. Use
showmount -efor diagnostics and keep UFW/Selinux rules current. - subtree_check issues — exporting a subdirectory may cause false “permission denied” on file access. Add
no_subtree_checkin/etc/exportsif observed.
Automated integrity checks can be wrapped in a systemd timer for rsync sync:
Activate the 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.