Network & Connectivity

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

Create and export a directory

sudo mkdir -p /exports/shared
sudo chown nobody:nogroup /exports/shared
sudo chmod 755 /exports/shared

Edit /etc/exports:

sudo nano /etc/exports
# Syntax: /path  client(options)
/exports/shared   10.0.0.0/24(rw,sync,no_subtree_check)

Common options:

OptionDescription
rwRead and write
roRead only
syncWrite to disk before responding (safer)
asyncFaster but less safe
no_subtree_checkDisable subtree checking (recommended)
no_root_squashAllow root on client to act as root on server
root_squashMap root to nobody (default, safer)
all_squashMap all users to nobody

Apply the export:

sudo exportfs -ra
sudo exportfs -v

Firewall 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 111

Client setup

Install NFS client

sudo apt update
sudo apt install nfs-common -y

Mount manually

sudo mkdir -p /mnt/shared
sudo mount -t nfs 10.0.0.1:/exports/shared /mnt/shared

Verify:

df -h /mnt/shared
ls /mnt/shared

Mount at boot (fstab)

sudo nano /etc/fstab
10.0.0.1:/exports/shared   /mnt/shared   nfs   defaults,_netdev,auto   0   0
  • _netdev: wait for network before mounting
  • auto: mount at boot

Test fstab without rebooting:

sudo mount -a

Mount 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   0

Useful 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/shared

Automount (mount on access, unmount when idle)

Install autofs to mount NFS shares on demand:

sudo apt install autofs -y
sudo nano /etc/auto.master

Add:

/mnt/nfs   /etc/auto.nfs   --timeout=60

Create /etc/auto.nfs:

shared   -fstype=nfs,rw,sync   10.0.0.1:/exports/shared

Apply:

sudo systemctl restart autofs

The 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

On this page