How to Set Up an NFS Server on Ubuntu
In this tutorial, we will set up an NFS (Network File System) server on Ubuntu and configure the client to connect to it. The NFS protocol allows a user on a client computer to access files over a network in the same way as if the storage were local. This setup uses the IP range 192.168.1.0/24
as the example network.
Step 1: Install the NFS Server
First, update your package index and install the NFS server on the host machine:
1
2
sudo apt update
sudo apt install nfs-kernel-server
Step 2: Create a Shared Directory
Next, create the directory you want to share via NFS. In this example, we’ll create a directory at /mnt/nfs_share
.
1
sudo mkdir -p /mnt/nfs_share
Set the appropriate permissions so the NFS clients can access the directory:
1
2
sudo chown nobody:nogroup /mnt/nfs_share
sudo chmod 777 /mnt/nfs_share
Step 3: Configure NFS Exports
Now, you need to configure the directory to be shared over NFS. Edit the NFS exports file:
1
sudo nano /etc/exports
To allow access to the entire local network (IP range 192.168.1.0/24
), add the following line to export the /mnt/nfs_share
directory:
1
/mnt/nfs_share 192.168.1.0/24(rw,sync,no_subtree_check)
This configuration will permit any machine in the 192.168.1.0/24
subnet to access the NFS share. If you want to restrict access to a specific IP (for example, 192.168.1.7
), you can modify the line accordingly:
1
/mnt/nfs_share 192.168.1.23(rw,sync,no_subtree_check)
Save and close the file.
Step 4: Apply NFS Export Configuration
To apply the changes, restart the NFS service:
1
2
sudo exportfs -a
sudo systemctl restart nfs-kernel-server
Step 5: Configure Firewall (Optional)
If your server has a firewall running, allow NFS traffic through by enabling the necessary ports:
1
sudo ufw allow from 192.168.1.0/24 to any port nfs
Step 6: Mount the NFS Share on the Client
On the client machine (within the 192.168.1.0/24
network), you need to install the NFS client utilities:
1
2
sudo apt update
sudo apt install nfs-common
Then, create a mount point for the NFS share:
1
sudo mkdir -p /mnt/nfs_clientshare
Mount the NFS share from the server:
1
sudo mount 192.168.1.7:/mnt/nfs_share /mnt/nfs_clientshare
Step 7: Verify the NFS Mount
You can verify the NFS share is mounted by checking the contents of the mount point:
1
df -h /mnt/nfs_clientshare
This should show that the NFS share from 192.168.1.7
is mounted on the client.
Step 8: Make the NFS Mount Permanent
To ensure the NFS share is mounted automatically after a reboot, add it to the /etc/fstab
file on the client:
1
sudo nano /etc/fstab
Add the following line:
1
192.168.1.7:/mnt/nfs_share /mnt/nfs_clientshare nfs defaults 0 0
Save and close the file. The NFS share will now mount automatically after every reboot.
Conclusion
By following these steps, you have successfully set up an NFS server on Ubuntu and configured a client to access the shared directory. This allows seamless file sharing between machines over the network. You also have the flexibility to allow access to an entire subnet (192.168.1.0/24
) or restrict access to specific IP addresses.
References: