NFS stands for Network File System is a network file-sharing protocol that allows you to share files and directories over the network. You can mount the file systems over a network and use them as your local drive. NFS server is a client-server architecture where multiple clients can mount the shared drive from the NFS server and share resources between Linux systems. By using NFS, you can save space and the cost of storage, especially when you are using cloud instances.
In this guide, You will learn how to set up the NFS server on Ubuntu 24.04.
Prerequisites
1. A server running Ubuntu and having a static IP address with root privileges.
2. A client running Ubuntu with sudo privileges.
Step 1: Update OS
Before starting the NFS server installation, update your base system by executing the following command:
sudo apt update -y && sudo apt upgrade -y
Step 2: Install and configure the NFS Server
On your server system, install nfs-kernel-server package by running the following command:
sudo apt install nfs-kernel-server -y
Now, start the NFS service and enable it to auto-start at system boot time. Then, verify the NFS service status with the following command:
sudo systemctl start nfs-server.service
sudo systemctl enable nfs-server.service
sudo systemctl status nfs-server.service
Output:
sohan@nfs-server:~# sudo systemctl status nfs-server.service
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; preset: enabled)
Active: active (exited) since Fri 2024-05-24 13:31:44 UTC; 10min ago
Main PID: 8454 (code=exited, status=0/SUCCESS)
CPU: 15ms
Step 3: Create and Export NFS share Directory
Next, you need to create an NFS share directory on your NFS server that you want to access on your client’s system. Execute the below command to create a shared directory:
sudo mkdir -p /mnt/nfsshare
Now, create some files in that shared directory:
sudo touch /mnt/nfsshare/file2.txt
sudo touch /mnt/nfsshare/file2.txt
Next, Set proper permissions on NFS share directory:
sudo chown -R nobody:nogroup /mnt/nfsshare/
Now, set the permission to read, write, and execute permissions to all the files inside the shared directory.
sudo chmod 777 /mnt/nfsshare/
Step 4: Configure the Export Directory
Now, edit the NFS server configuration file and define the path of the directory that you want to share. To edit the file, run the following command:
sudo vim /etc/exports
Now add the following line:
/mnt/nfsshare 164.92.211.185(rw,sync,no_subtree_check)
Here, You need to replace the IP address with your client’s IP address. For multiple clients, you need to specify the separate line as shown below:
/mnt/nfsshare 164.92.211.185(rw,sync,no_subtree_check)
/mnt/nfsshare 164.92.211.187(rw,sync,no_subtree_check)
You have to define permission for each client.
rw: The client can read and write on the shared directory.
Sync: This will reply to requests only after the changes have been committed to stable storage.
no_subtree_check: Do not check subtree.
After setting up the access permission to each client, you need to export the NFS share directory.
sudo exportfs -a
To apply the changes, you have made, you need to restart the service using the command below:
sudo systemctl restart nfs-kernel-server
Step 5: Install and Configure NFS Client
You have set up and configured the NFS service on the server but, you need to install the following packages on your client system to configure the NFS client:
sudo sudo apt update -y && sudo apt install nfs-common -y
Now, start the nfs-client service, enable it at system reboot, and then check the service status by running the following command in the terminal:
sudo systemctl start nfs-client.target
sudo systemctl enable nfs-client.target
sudo systemctl status nfs-client.target
Output:
sohan@nfs-client:~$ sudo systemctl start nfs-client.target
[sudo] password for sohan:
sohan@nfs-client:~$ sudo systemctl enable nfs-client.target
sohan@nfs-client:~$
sohan@nfs-client:~$ sudo systemctl status nfs-client.target
● nfs-client.target - NFS client services
Loaded: loaded (/usr/lib/systemd/system/nfs-client.target; enabled; preset: enabled)
Active: active since Fri 2024-05-24 14:34:55 UTC; 17min ago
sohan@nfs-client:~$
Now, on the client system, you can see the mount information of the NFS server using the following command:
showmount -e 206.189.96.81
Here, you need to replace the IP address with your NFS server IP address.
Output:
sohan@nfs-client:~$ showmount -e 206.189.96.81
Export list for 206.189.96.81:
/mnt/nfsshare 164.92.211.185
sohan@nfs-client:~$
Step 6: Create and mount NFS share on the client
Now, create a directory on the client system using the command below:
sudo mkdir -p /mnt/nfs_share_mount
Next, mount the NFS server shared directory on the NFS client by running the command below:
sudo mount 206.189.96.81:/mnt/nfsshare /mnt/nfs_share_mount
Here, replace the IP address with your NFS server IP address.
To mount the NFS file system permanently even after a system reboot, then update /etc/fstab the file on your NFS client system.
sudo vim /etc/fstab
Then add the below line with your NFS server IP address and NFS directory name:
206.189.96.81:/mnt/nfsshare /mnt/nfs_share_mount nfs defaults 0 0
Output:
sohan@nfs-client:~$ cat /etc/fstab
LABEL=cloudimg-rootfs / ext4 discard,commit=30,errors=remount-ro 0 1
LABEL=BOOT /boot ext4 defaults 0 2
LABEL=UEFI /boot/efi vfat umask=0077 0 1
206.189.96.81:/mnt/nfsshare /mnt/nfs_share_mount nfs defaults 0 0
Now, the NFS file system is automatically mounted at system reboot.
Step 7: Verify the NFS share on the client system
Let’s verify the NFS mount point on the NFS client system by using the df -h command:
df -h
Output:
sohan@nfs-client:~$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 46M 1.1M 45M 3% /run
/dev/vda1 8.7G 1.4G 7.3G 16% /
tmpfs 230M 0 230M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/vda16 881M 61M 758M 8% /boot
/dev/vda15 105M 6.1M 99M 6% /boot/efi
206.189.96.81:/mnt/nfsshare 8.7G 1.4G 7.3G 16% /mnt/nfs_share_mount
tmpfs 46M 12K 46M 1% /run/user/0
sohan@nfs-client:~$
You can see your server IP address followed by the shared directory path in your mount point as highlighted in the above output.
Let’s now create a file on the NFS mount directory /mnt/nfs_share_mount from the NFS client system.
touch /mnt/nfs_share_mount/file3
Here, we have created file3 from the NFS client system, Now login into the NFS-server and check the NFS directory list:
ll /mnt/nfsshare/
Output:
sohan@nfs-server:~$ ll /mnt/nfsshare/
total 8
drwxrwxrwx 2 nobody nogroup 4096 May 24 15:40 ./
drwxr-xr-x 3 root root 4096 May 24 13:51 ../
-rw-r--r-- 1 nobody nogroup 0 May 24 13:53 file1.txt
-rw-r--r-- 1 nobody nogroup 0 May 24 13:54 file2.txt
-rw-rw-r-- 1 sohan sohan 0 May 24 15:40 file3
sohan@nfs-server:~$
You can see the same file has been accessible on both systems and the NFS server is working fine.
Conclusion
Congratulations! You have successfully set up the NFS server on Ubuntu, and you also learned how to access the NFS share from the client machine. Please have no hesitation to ask me if you have any queries.