Network File System (NFS) is a protocol that allows you to share files and directories between Linux/Unix systems over a network. You can set up an NFS server to share files with other systems on the network, and those systems (clients) can mount the shared directories as if they were on their local machine.
The first step is to install the required packages for the NFS server. You can use your distribution’s package manager for this.
For Debian/Ubuntu:
sudo apt update
sudo apt install nfs-kernel-server
For CentOS/RHEL/Fedora:
sudo yum install nfs-utils
Decide which directory you want to share over the network and create it if it doesn’t exist.
sudo mkdir -p /mnt/nfs_share
Assign appropriate permissions to the directory. For example, allow all users to read and write:
sudo chown nobody:nogroup /mnt/nfs_share
sudo chmod 777 /mnt/nfs_share
You need to specify which directories to share and with whom in the /etc/exports
file.
Open /etc/exports
in a text editor:
sudo nano /etc/exports
Add an entry for the directory you want to share, specifying the IP address (or subnet) of the client and the permissions. For example:
/mnt/nfs_share 192.168.1.100(rw,sync,no_subtree_check)
This entry shares /mnt/nfs_share
with the client at IP 192.168.1.100
with read-write access. You can also specify an entire subnet like 192.168.1.0/24
.
Here are some options you can use:
rw
: Read and write access.ro
: Read-only access.sync
: Data is written to disk immediately.no_subtree_check
: Prevents subtree checking, which can improve performance.To apply the changes made to the /etc/exports
file, run:
sudo exportfs -a
Now, start the NFS service and enable it to start on boot.
For Debian/Ubuntu:
sudo systemctl start nfs-kernel-server
sudo systemctl enable nfs-kernel-server
For CentOS/RHEL/Fedora:
sudo systemctl start nfs-server
sudo systemctl enable nfs-server
If you have a firewall enabled, you’ll need to allow NFS services.
For UFW (Debian/Ubuntu):
sudo ufw allow from 192.168.1.0/24 to any port nfs
For FirewallD (CentOS/RHEL):
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --reload
To access the shared directory from a client, install the NFS client package.
For Debian/Ubuntu:
sudo apt install nfs-common
For CentOS/RHEL/Fedora:
sudo yum install nfs-utils
Mount the shared directory on the client:
sudo mount 192.168.1.100:/mnt/nfs_share /mnt/client_nfs
The client can now access the shared files in /mnt/client_nfs
.
To make the mount persistent across reboots, add it to /etc/fstab
on the client:
192.168.1.100:/mnt/nfs_share /mnt/client_nfs nfs defaults 0 0