NFS exports and mounts: file sharing between Linux machines
NFS is the simplest sharing between Linux machines: no account system, just IPs and UID mapping. Measured nfs-utils 2.9.2.
Server: install and export
Section titled “Server: install and export”sudo dnf install -y nfs-utilsPrepare a directory and write the export rule — prefer /etc/exports.d/*.exports fragments over the monolithic exports file (upgrades will not clobber them):
sudo mkdir -p /srv/nfstestecho "nfs-hello" | sudo tee /srv/nfstest/n.txtecho "/srv/nfstest 127.0.0.1(ro)" | \ sudo tee /etc/exports.d/test.exportssudo systemctl enable --now nfs-serversudo exportfs -ra # apply all rulessudo exportfs -v # show effective rulesMeasured -v output (every default spelled out):
/srv/nfstest 127.0.0.1(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)The important ones:
| Option | Meaning | When to change |
|---|---|---|
ro / rw | read-only / read-write | media libraries ro, collaboration rw |
root_squash | client root maps to nobody | default on, keep it |
sync | acknowledge after disk write | default; async risks data loss |
no_subtree_check | skip subtree checks | default on, fewer false alarms |
secure | clients must use a privileged port | keep when clients are system services |
Client: mount
Section titled “Client: mount”sudo mkdir -p /mnt/nfstestsudo mount -t nfs4 127.0.0.1:/srv/nfstest /mnt/nfstestcat /mnt/nfstest/n.txtsudo umount /mnt/nfstestMeasured: cat prints nfs-hello — the exported content is readable, chain verified.
For fstab (_netdev waits for the network; soft keeps the client from hanging if the server dies):
server-ip:/srv/nfstest /mnt/nfstest nfs4 _netdev,soft,timeo=50 0 0autofs: mount on demand (optional)
Section titled “autofs: mount on demand (optional)”Fixed fstab entries hold connections open even when unused. autofs mounts only when you enter the directory:
sudo dnf install -y autofs# /etc/auto.master.d/nfs.autofs: /mnt/nfs /etc/auto.nfssudo systemctl enable --now autofsls /mnt/nfs/nfstest # actual mount happens hereWith many servers and dozens of export points, autofs scales far better than fstab.
Firewall
Section titled “Firewall”sudo firewall-cmd --add-service=nfs --permanentsudo firewall-cmd --add-service=rpc-bind --permanentsudo firewall-cmd --add-service=mountd --permanentsudo firewall-cmd --reloadAll three: nfs (2049), rpc-bind (111), mountd (dynamic port). Missing mountd is the classic cause of “showmount works but mount hangs”.
root_squash: why nobody cannot write
Section titled “root_squash: why nobody cannot write”Default root_squash maps client root to server nobody — a feature: a compromised client does not get server root. To let a directory accept root writes (fully trusted LANs only):
/srv/nfstest 127.0.0.1(rw,no_root_squash)Regular users are unaffected by squashing: client UID 1000 reads and writes server UID 1000’s files naturally. Environments with mismatched UIDs should unify accounts (FreeIPA/LDAP) before using NFS, otherwise file ownership becomes chaos.
Common traps
Section titled “Common traps”| Symptom | Cause |
|---|---|
| mount hangs forever | mountd port blocked by firewall (allow all three services) |
| write Permission denied | server directory permissions, or export is ro |
| root cannot write | root_squash in effect (default); needs explicit no_root_squash |
| exports change does nothing | forgot exportfs -ra |
| client boot hangs on mount | fstab entry lacks _netdev and soft |
| files owned by nobody | UIDs differ between the two machines |
Related pages
Section titled “Related pages”- Samba sharing — the Windows/macOS alternative
- firewalld — allowing the three services
- Storage and btrfs — server-side filesystem choice
- Upstream: nfs-utils