# NFS replication setup between two Linux servers

LLMS index: [llms.txt](/en/llms.txt)

---

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`.

```bash
# 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:

```bash
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`:

| 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:

```bash
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.

```bash
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):

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

Verify accessibility:

```bash
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.

```bash
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`:

```ini
[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:

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

Check status:

```bash
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:

```bash
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:

```ini
[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:

```bash
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.
