Table of Contents
Developed by Sun Microsystems in 1984, NFS (Network File SystemS) is an RPC-based distributed file system structure that allows computers on the network to access a common file system as easily as their local disks.
In NFS, they do not need to know where the files to be accessed are physically. A particular partition shared with nfs on one machine can be read or written by other machines.
In this article, we will tell you how to share a folder with NFS in Ubuntu.
NFS requires a server-client configuration. First, let's show the steps to be done for the server. Next, we will describe how to define an nfs share in the client.
NFS Server Configuration
Step-1: Update the Server Packages (Optional)
First update the package repository:
foc@ubuntu22desktop:~$ sudo apt update -y
Step-2: Install NFS server package
Then install the nfs-kernel-server package if not installed already:
foc@ubuntu22desktop:~$ sudo apt install nfs-kernel-server -y
Step-3: Create a directory to be shared
Create the directory you want to share. Skip this step if you are going to use a previously created directory:
foc@ubuntu22desktop:~$ sudo mkdir /nfssharefolder
Set the permissions of the folder. For the demonstration I am giving 777 permission but you can limit the permission based on your requirement
chmod 777 /nfssharefolder
Step-4: Configure exports file
Next, define clients' access to the share directory:
foc@ubuntu22desktop:~$ sudo nano /etc/exports /nfssharefolder *(rw,sync,subtree_check)
Parameters defined when sharing the directory:
*specifies that any client is allowed to access the shared folder.
rw: Allows both read and write requests on this NFS array. The default is to disallow any request that changes the file system. This can also be made explicit using the
sync: Forces NFS to write changes to disk before responding. It provides a more stable and consistent environment, but this also reduces the speed of file operations.
no_subtree_check: This option disables subtree checking, which has mild security implications, but can improve reliability in some circumstances.
no_root_squash:Turn off root squashing. This option is mainly useful for diskless clients. This was intended as security feature to prevent a root account on the client from using the file system of the host as root.
For more parameters, you can visit man page of exports.
Step-5: Enable and start NFS Server
Enable the nfs-kernel-server service to automatically start post reboot of the Linux server:
foc@ubuntu22desktop:~$ sudo systemctl enable nfs-kernel-server Synchronizing state of nfs-kernel-server.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable nfs-kernel-server
And restart the service:
foc@ubuntu22desktop:~$ sudo systemctl restart nfs-kernel-server
Step-6: Enable firewall for NFS
Then add the firewall rules for the access of the clients that will receive the nfs service. Here we are allowing access for NFS Server from all clients under
192.168.122.0/24 subnet. If you don't want to provide client subnet details then you can just globally allow NFS access.
foc@ubuntu22desktop:~$ sudo ufw allow from 192.168.122.0/24 to any port nfs Rules updated
Enable the firewall:
foc@ubuntu22desktop:~$ sudo ufw enable Command may disrupt existing ssh connections. Proceed with operation (y|n)? y Firewall is active and enabled on system startup
The firewall rule for port 2049 should be:
foc@ubuntu22desktop:~$ sudo ufw status Status: active To Action From -- ------ ---- 2049 ALLOW 192.168.122.0/24
Server steps are complete here.
NFS Client Configuration
Start by updating the package repository on the client:
Step-1: Update NFS Client Packages (Optional)
foc@ubuntu22:~$ sudo apt update -y
Step-2: Install NFS Client
Then install the nfs-common package:
foc@ubuntu22:~$ sudo apt install nfs-common -y
Step-3: Create mount point
Create the directory to mount the NFS share:
foc@ubuntu22:~$ sudo mkdir /nfs_share_client
Step-4: Access NFS Server on the Client
Then mount the NFS server share directory and the directory created on the client:
foc@ubuntu22:~$ sudo mount -t nfs 192.168.122.75:/nfssharefolder /nfs_share_client
Verify the same using
foc@ubuntu22:~$ df -h Filesystem Size Used Avail Use% Mounted on tmpfs 393M 1.2M 392M 1% /run /dev/mapper/ubuntu--vg-ubuntu--lv 12G 9.3G 1.4G 88% / tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock /dev/vda2 2.0G 247M 1.6G 14% /boot tmpfs 393M 4.0K 393M 1% /run/user/1000 192.168.122.75:/nfssharefolder 24G 20G 3.3G 86% /nfs_share_client
Step-5: Test the NFS Client
Create a sample file on the client (this will require write permission for the NFS client on the shared folder which we created on NFS Server):
foc@ubuntu22:~$ sudo touch /nfs_share_client/example_file
You can see that the same file is created on the server:
foc@ubuntu22desktop:~$ ls /nfssharefolder/ example_file
Step-6: Make persistent changes to access NFS Server on Client
The changes which we did in previous steps are non-persistent which would mean that it will not survive post reboot of the Linux node. To make the NFS share permanent, define it in the fstab file or you can also use systemctl to update fstab file:
foc@ubuntu22:~$ sudo nano /etc/fstab 192.168.122.75:/nfssharefolder /nfs_share_client nfs4 defaults 0 0
After this step, when the client is rebooted, you will still have access to the NFS share directory:
foc@ubuntu22:~$ mount -l | grep nfs 192.168.122.75:/nfssharefolder on /nfs_share_client type nfs4 (rw,relatime,vers=4.2,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.122.34,local_lock=none,addr=192.168.122.75)
What is NEXT?
- NFS mount options | NFS exports options | Beginners Guide
- 10 practical examples to export NFS shares in Linux
- How to start systemd service after NFS mount in Linux
In Linux, there are different methods for sharing remote directories and accessing them on clients. But the easiest and fastest method is NFS. We have explained the steps for both server and client. We hope it will be useful.