How to setup NFS server in 5 minutes on Linux

Reading Time: 2 minutes

NFS (Network File System) allows you to share directories and files with other Linux clients over the same network space. Shared directories are created where the NFS server is running. Admins add files and directories in an exported directory, which are shared with other NFS clients that are other Linux systems. Users in other Linux systems can access those files and folders which are shared by NFS host over a network.

Demo – NFS server

PreRequestisites for NFS server

  1. Two or more linux machines
  2. Shared network space | All machines should connected to a same network
  3. Designate 1 linux machine as host and others as clients

On Host Machine Install server utils

1. Install NFS host server utils

sudo apt update
sudo apt install nfs-kernel-server

2. Make a directory to export over the network

sudo mkdir -p /srv/nfs/datanfs
sudo chmod -R 777 /srv/nfs/datanfs

3. Grant NFS share access to client systems

sudo vi /etc/exports

Add your directory and policies in /etc/exports file

/srv/nfs/datanfs        *(rw,sync,no_subtree_check)

Syntax is:

/path/to/directory <client-ipv4address>(host policies)

/srv/nfs/data 192.168.1.3(rw,sync,no_subtree_check)

Rules:

UsageSyntax/Values
To enable a single client /srv/nfs/data 192.168.1.3(rw,sync,no_subtree_check)
To enable several clients/srv/nfs/data 192.168.1.3(rw,sync,no_subtree_check) 192.168.1.4(rw,sync,no_subtree_check)
To enable the entire subnet/srv/nfs/data <subnetIP>/<subnetMask>(rw,sync,no_subtree_check)
To enable all clients/srv/nfs/data *(rw,sync,no_subtree_check)

Policies:

  • rw : It enables both read and write.
  • sync : It writes changes to disk before allowing users to access the modified file.
  • no_subtree_check : which means NFS doesn’t check if each subdirectory is accessible to the user.

See more details about policies on man exports page

5. Export NFS share directory and restart NFS service

sudo exportfs -a
sudo systemctl restart nfs-kernel-server

6. Allow NFS clients access through the firewall

sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw enable
sudo ufw status

The output will look like

To                         Action      From
--                         ------      ----
Anywhere                   DENY        157.240.198.35            
Anywhere                   DENY        157.240.198.36            
157.240.198.35             DENY        Anywhere                  
2049                       ALLOW       192.168.1.0/24           

On client machines

1 Install NFS common package for clients

sudo apt update
sudo apt install nfs-common

2. Mount a directory on the exported directory

sudo mount 192.168.1.8:/srv/nfs/datanfs /mnt

Now you can play with NFS file system. Create some files in /mnt directory and you will see files get synced with host machines.

Written by 

Rahul Soni is a Software Intern at Knoldus Software. He is always charged up for new things & learnings. He is dedicated to his work and believes in quality output. He loves to take deep dives into cloud technologies & different tools.