Setting up an NFS server on Ubuntu 20.04
Network File System (NFS) was developed by Sun Microsystems in 1984. It's an RPC-based distributed file system that lets machines on a network access a shared file system as if it were local disk.
In practice, NFS lets multiple machines work with the same set of files as if they all lived on a single drive. You don't have to think about where the bytes physically live. A common use case in larger orgs: instead of every machine carrying its own copy of the same binaries or assets, you put them on one server and let the rest of the fleet mount the shared directory. The server exporting the share is the NFS server; the machines mounting it are NFS clients.
1. Install the NFS server
Install nfs-kernel-server:
apt updateapt install nfs-kernel-serverCheck which NFS protocol versions the server supports:
cat /proc/fs/nfsd/versions-2 +3 +4 +4.1 +4.2Ubuntu 20.04 ships with NFSv2 disabled. v2 is ancient and effectively unused in the wild — leave it off.
Make the directory you want to export:
mkdir -p /mnt/sharedfolder/Open the exports file:
nano /etc/exportsAdd the share line:
/mnt/sharedfolder/ 192.168.10.0/24(rw,sync,no_root_squash,no_subtree_check)I'm giving full read/write to the 192.168.10.0/24 subnet here. You can mix and match — different IPs/subnets with different option sets — depending on who should be able to read/write what. The full list of export options lives in the exports(5) man page.
Apply and start the server:
exportfs -asystemctl restart nfs-kernel-serverThat's the server side. Now we mount it from a client.
2. Set up the NFS client
Install nfs-common:
apt install nfs-commonMake a local mount point:
mkdir -p /mnt/nfsfolderYou can mount the share temporarily (lost on reboot) or persistently via /etc/fstab.
Persistent mount
nano /etc/fstabAdd:
192.168.10.171:/mnt/sharedfolder/ /mnt/nfsfolder/ nfs rw,hard,intr,rsize=8192,wsize=8192,timeo=14 0 0Apply without a reboot:
mount -aTemporary mount
mount -t nfs -o vers=4 192.168.10.171:/mnt/sharedfolder /mnt/nfsfolderThat's it — the same files now appear on both hosts.
On the server:
ll /mnt/sharedfolderOn the client:
ll /mnt/nfsfolder