First, install the NFS kernel server:
sudo apt install nfs-kernel-server
Start the NFS service:
sudo systemctl start nfs-kernel-server.service
Edit the /etc/exports file to define shared directories:
sudo nano /etc/exports
# Publicly accessible directory
/srv/nfs/public *(rw,sync,no_subtree_check)
# Directory accessible only to internal IP addresses (example: 192.168.1.0/24 subnet)
# Replace with your actual internal IP range
/srv/nfs/private 192.168.1.0/24(rw,sync,no_subtree_check)
#or set of IPs
# Allow access to two specific IPs for the /srv/nfs/private folder
/srv/nfs/private 192.168.1.10(rw,sync,no_subtree_check) 192.168.1.20(rw,sync,no_subtree_check)
/srv/nfs/public
is the directory being shared,
192.168.1.0/24
is the subnet of IP addresses allowed to access,
rw
is Read and write access, sync
is data is written to disk before responding &
no_subtree_check
Improves performance by not checking if the file being accessed is within a subdirectory of the exported directory.
sudo exportfs -a
Make sure to allow port 2049
, which is the main port used by NFS for file sharing.
sudo ufw allow 2049/tcp comment "NFS Server"
On other Ubuntu machines (worker nodes) that need to mount the NFS shares, install the NFS common package:
sudo apt install nfs-common
Now your worker nodes can mount the NFS shares!