Steps to Build an NFS Server
1. Install NFS Server on Linux (VM or Kubernetes Node)
If you're using a VM or any Linux-based system as the NFS server, you first need to install the NFS server package.
For Ubuntu/Debian:
sudo apt update
sudo apt install -y nfs-kernel-server
2. Create a Directory to Share
Create a directory that will be shared over NFS. For example:
sudo mkdir -p /mnt/data
sudo chmod 777 /mnt/data
3. Configure NFS Exports
You'll need to configure the NFS exports to specify which directories you want to share and with which permissions.
Edit the NFS exports file:
sudo nano /etc/exports
Add the following line to the file, allowing any IP to access the directory (replace *
with specific IP ranges for security):
/mnt/data *(rw,sync,no_subtree_check)
-
rw
: Allows read-write access. -
sync
: Ensures data is written to disk before returning a response. -
no_subtree_check
: Prevents checking the file system for each request (to improve performance).
4. Export the Shared Directory
After editing the /etc/exports file
, export the shared directories with the following command:
sudo exportfs -a
To confirm that the export was successful, run:
sudo exportfs -v
5. Start NFS Server
Start and enable the NFS server to run on boot:
For Ubuntu/Debian:
sudo systemctl enable nfs-kernel-server
sudo systemctl start nfs-kernel-server
6. Check if the NFS Server Service is Running
Log in to the NFS server and verify that the required services are active.
For most systems:
sudo systemctl status nfs-server
Test Port Accessibility
Ensure that the required NFS ports are open on the NFS server and accessible from the client.
Check the NFS Ports:
sudo rpcinfo -p
Look for services like nfs
, mountd
, and portmapper
in the output.
Example:
program vers proto port service
100000 4 tcp 111 portmapper
100005 1 udp 20048 mountd
100003 3 tcp 2049 nfs
7. Open Ports in Firewall (if applicable)
If you're using a firewall, open the necessary ports to allow NFS traffic. The typical ports used for NFS are 2049
(NFS) and 111
(RPC).
For UFW (Ubuntu firewall):
sudo ufw allow from any to any port nfs
8. Test the NFS Share
From another machine (which will act as the client), test the NFS share.
sudo mount -t nfs nfs-server-ip:/mnt/data /mnt
You should now be able to read and write files to the /mnt/data
directory over NFS.