NFS, Network File System
Share directories between Linux servers using NFS, mount remote folders as if they were local
NFS (Network File System) lets you share directories between Linux servers over the network. Ideal for sharing storage between multiple VPS instances, mounting a backup target, or sharing files between an app server and a web server.
NFS traffic is unencrypted by default. Use it only on a private network (LAN or VPN). Never expose NFS ports to the public internet.
Architecture
Server (exports /data) ──private network──► Client (mounts /data at /mnt/data)Server setup
Install NFS server
sudo apt update
sudo apt install nfs-kernel-server -y
sudo systemctl enable nfs-server
sudo systemctl start nfs-serverCreate and export a directory
sudo mkdir -p /exports/shared
sudo chown nobody:nogroup /exports/shared
sudo chmod 755 /exports/sharedEdit /etc/exports:
sudo nano /etc/exports# Syntax: /path client(options)
/exports/shared 10.0.0.0/24(rw,sync,no_subtree_check)Common options:
| Option | Description |
|---|---|
rw | Read and write |
ro | Read only |
sync | Write to disk before responding (safer) |
async | Faster but less safe |
no_subtree_check | Disable subtree checking (recommended) |
no_root_squash | Allow root on client to act as root on server |
root_squash | Map root to nobody (default, safer) |
all_squash | Map all users to nobody |
Apply the export:
sudo exportfs -ra
sudo exportfs -vFirewall rules (server)
sudo ufw allow from 10.0.0.0/24 to any port nfs
sudo ufw allow from 10.0.0.0/24 to any port 111Client setup
Install NFS client
sudo apt update
sudo apt install nfs-common -yMount manually
sudo mkdir -p /mnt/shared
sudo mount -t nfs 10.0.0.1:/exports/shared /mnt/sharedVerify:
df -h /mnt/shared
ls /mnt/sharedMount at boot (fstab)
sudo nano /etc/fstab10.0.0.1:/exports/shared /mnt/shared nfs defaults,_netdev,auto 0 0_netdev: wait for network before mountingauto: mount at boot
Test fstab without rebooting:
sudo mount -aMount with performance options
For better performance over LAN:
10.0.0.1:/exports/shared /mnt/shared nfs rw,sync,hard,intr,rsize=8192,wsize=8192,_netdev 0 0Useful commands
# On server: list active exports
sudo exportfs -v
# On server: list connected clients
sudo showmount -a
# On client: list available exports from server
showmount -e 10.0.0.1
# Unmount
sudo umount /mnt/shared
# Force unmount if busy
sudo umount -l /mnt/sharedAutomount (mount on access, unmount when idle)
Install autofs to mount NFS shares on demand:
sudo apt install autofs -y
sudo nano /etc/auto.masterAdd:
/mnt/nfs /etc/auto.nfs --timeout=60Create /etc/auto.nfs:
shared -fstype=nfs,rw,sync 10.0.0.1:/exports/sharedApply:
sudo systemctl restart autofsThe share will mount automatically when you ls /mnt/nfs/shared and unmount after 60 seconds of inactivity.
Troubleshoot
# Check NFS server status
sudo systemctl status nfs-server
sudo journalctl -u nfs-server -n 30
# Check RPC services
rpcinfo -p
# Debug mount errors on client
sudo mount -v -t nfs 10.0.0.1:/exports/shared /mnt/shared
# Check firewall
sudo ufw status | grep nfs