Network File System (NFS) is a protocol that allows one system to share directories and files with others over a network. It's commonly used in Linux environments for file sharing between systems. In this guide, we'll walk you through the steps to install and set up an NFS server on a Linux system.
Prerequisites
Before you start, make sure you have:
- A Linux system distros (e.g., Ubuntu, CentOS, Debian, etc.)
- Root or sudo privileges on the system.
- A network connection between the server (NFS server) and clients (machines that will access the shared directories).
1. Install NFS Server Package
On Ubuntu / Debian based Linux systems:
a. First, update the package list
# apt update
b. Install the NFS server package
# apt install nfs-kernel-server
On CentOS/REL-based systems:
2. Install the NFS server package
# yum install nfs-utils
Once the package is installed, ensure that the necessary services are enabled.
3. Create Shared Directory for file sharing
Decide which directory you want to share over NFS. If the directory doesn't exist, you can create one. For example:
# mkdir -p /nfs_srv_dir/nfs_share
Make sure the directory has the appropriate permissions so that the nfs clients can access it.
# chown nobody:nogroup /nfs_srv_dir/nfs_share
# chmod 755 /nfs_srv_dir/nfs_share
4. Configure NFS Exports ( /etc/exports file)
The NFS exports file (/etc/exports) is perhaps most important file you will have to create and deal with regularly to define the expored shares, this file contains the configuration settings for directories you want to share with other systems.
a. Open the /etc/exports file for editing:
vi /etc/exports
Add an entry for the directory you want to share. For example, if you're sharing /nfs_srv_dir/nfs_share and allowing access to all systems on the network (192.168.1.0/24), add the following line:
/nfs_srv_dir/nfs_share 192.168.1.0/24(rw,sync,no_subtree_check)
Here’s what each option means:
- rw: Read and write access.
- sync: Ensures that changes are written to disk before responding to the client.
Here is few lines of example of my working /etc/exports on my home running NFS server
/var/www 192.168.0.209/32(rw,no_root_squash,async,subtree_check)
/home/jordan 192.168.0.209/32(rw,no_root_squash,async,subtree_check)
/mnt/sda1/icons-frescoes/ 192.168.0.209/32(rw,no_root_squash,async,subtree_check)
/home/mobfiles 192.168.0.209/32(rw,no_root_squash,async,subtree_check)
/mnt/sda1/icons-frescoes/ 192.168.0.200/32(rw,no_root_squash,async,subtree_check)
/home/hipo/public_html 192.168.0.209/32(rw,no_root_squash,async,subtree_check)
/home/alex/public_html 192.168.0.209/32(rw,no_root_squash,async,subtree_check)
/home/necroleak/public_html 192.168.0.209/32(rw,no_root_squash,async,subtree_check)
/bashscripts 192.168.0.209/32(rw,no_root_squash,async,subtree_check)
/backups/Family-Videos 192.168.0.200/32(ro,no_root_squash,async,subtree_check)
5. Export the NFS Shares with exportfs command
Once the export file is configured, you need to inform the NFS server to start sharing the directory:
# exportfs -a
The -a flag will make it export all the sharings.
6. Start and Enable NFS Services
You need to start and enable the NFS server so it will run on system boot.
On Ubuntu / Debian Linux run the following commands:
# systemctl start nfs-kernel-server
# systemctl enable nfs-kernel-server
On CentOS / RHEL Linux:
# systemctl start nfs-server
# systemctl enable nfs-server
7. Allow NFS Traffic Through the Firewall
If your server has a firewall configured / enabled, you will need to allow NFS-related ports through the firewall.
These ports include 2049 TCP protocol Ports (NFS) and 111 (RPCbind) UDP and TCP protocol , and some additional ports.
On Ubuntu/Debian (assuming you are using ufw [UNCOMPLICATED FIREWALL]):
# ufw allow from 192.168.1.0/24 to any port nfs sudo ufw reload
On CentOS / RHEL Linux:
# firewall-cmd –permanent –add-service=nfs sudo firewall-cmd –permanent –add-service=mountd sudo firewall-cmd –permanent –add-service=rpc-bind sudo firewall-cmd –reload
8. Verify NFS Server is Running
To ensure the NFS server is running properly, use the following command:
# systemctl status nfs-kernel-server
or
# systemctl status nfs-server
You should see output indicating that the service is active and running.
9. Test the NFS Share (Client-Side)
To test the NFS share, you will need to mount it on a client machine. Here's how to mount it:
On the client machine, install the NFS client utilities:
Ubuntu / Debian Linux
# apt install nfs-common
For CentOS / RHEL Linux
# yum install nfs-utils
Create a mount point (Nomatter the distro),:
# mkdir -p /mnt/nfs_share
Mount the NFS share:
# mount -t nfs <nfs_server_ip>:/nfs_srv_dir/nfs_share /mnt/nfs_share
Replace <nfs_server_ip> with the IP address of the NFS server or DNS host alias if you have one defined in /etc/hosts file.
Verify that the share is mounted:
# df -h
You should see the NFS share listed under the mounted file systems.
10. Configure Auto-Mount at Boot (Optional)
To have the NFS share automatically mounted at boot, you can add an entry to the /etc/fstab file on the client machine.
Open /etc/fstab for editing:
# vi /etc/fstab
Add the following line:
<server-ip>:/nfs_srv_dir/nfs_share /mnt/nfs_share nfs defaults 0 0
Save and close the file.
The NFS share will now be automatically mounted whenever the system reboots.
Debug NFS configuration issues (basics)
You can continue to modify the /etc/exports file to share more directories or set specific access restrictions depending on your needs.
If you encounter any issues, checking the server logs or using
# exportfs -v
/var/www 192.168.0.209/32(async,wdelay,hide,sec=sys,rw,secure,no_root_squash,no_all_squash)
/home/var_data 192.168.0.205/32(async,wdelay,hide,sec=sys,rw,secure,no_root_squash,no_all_squash)
/mnt/sda1/
192.168.0.209/32(async,wdelay,hide,sec=sys,rw,secure,no_root_squash,no_all_squash)
/mnt/sda2/info
192.168.0.200/32(async,wdelay,hide,sec=sys,rw,secure,no_root_squash,no_all_squash)
/home/mobfiles 192.168.0.209/32(async,wdelay,hide,sec=sys,rw,secure,no_root_squash,no_all_squash)
/home/var_data/public_html
192.168.0.209/32(async,wdelay,hide,sec=sys,rw,secure,no_root_squash,no_all_squash)
/var/public
192.168.0.209/32(async,wdelay,hide,sec=sys,rw,secure,no_root_squash,no_all_squash)
/neon/data
192.168.0.209/32(async,wdelay,hide,sec=sys,rw,secure,no_root_squash,no_all_squash)
/scripts 192.168.0.209/32(async,wdelay,hide,sec=sys,rw,secure,no_root_squash,no_all_squash)
/backups/data-limited
192.168.0.200/32(async,wdelay,hide,sec=sys,ro,secure,no_root_squash,no_all_squash)
/disk/filetransfer
192.168.0.200/23(async,wdelay,hide,sec=sys,ro,secure,no_root_squash,no_all_squash)
/public_shared/data
192.168.0.200/23(async,wdelay,hide,sec=sys,ro,secure,no_root_squash,no_all_squash)
Of course there is much more to be said on that you can for example, check /var/log/messages /var/log/syslog and other logs that can give you hints about issues, as well as manually try to mount / unmount a NFS stuck share to know more on what is going on, but for a starter that should be enough.
command can help severely in troubleshooting the NFS configuration.
Sum it up what learned ?
We learned how to set up basic NFS server and mounted its shared directory on a client machine.
This is a great solution for centralized file sharing and collaboration on Linux systems (even though many companies are trying to not use it due to its lack of connection encryption for historical reasons NFS has been widely used over the years and has helped dramatically for the Internet as we know it to become the World Wide Web of today. Thus for a well secured network and perhaps not a critical files infrastructure, still NFS is a key player in file sharing among heterogenous networks for multitudes of Gigabytes or Terra Pentabytes of data you would like to share amoung your Personal Computers / Servers / Phones / Tablets and generally all kind of digital computer equipment devices.