Skip to main content

Build NFS

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

7. Test the NFS Share

From another machine (which will act as the client), test the NFS share.

Mount the NFS share to a local directory (replace nfs-server-ip with the IP of the NFS server):

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.