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
- Two or more linux machines
- Shared network space | All machines should connected to a same network
- 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
/path/to/directory <client-ipv4address>(host policies)
|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)|
- 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 126.96.36.199 Anywhere DENY 188.8.131.52 184.108.40.206 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.