Network File System (NFS)
NFS enables a system to share directories and files with others over a network. With NFS, users and applications can access files on remote systems as if they were stored locally. Client systems mount a directory hosted on the NFS server, allowing them to access and work with the files it contains.
Pre-requisite
We are using the following configuration to set up the NFS server and client on Ubuntu-based NERC OpenStack VMs:
-
1 Linux machine for the NFS Server,
ubuntu-24.04-x86_64
,cpu-su.1
flavor with 1vCPU, 4GB RAM, 20GB storage - also assign Floating IP. Please note the NFS Server's Internal IP i.e.<NFS_SERVER_INTERNAL_IP>
i.e.192.168.0.73
in this example. -
1 Linux machine for the NFS Client,
ubuntu-24.04-x86_64
,cpu-su.1
flavor with 1vCPU, 4GB RAM, 20GB storage - also assign Floating IP. -
ssh access to both machines: Read more here on how to set up SSH on your remote VMs.
-
Create a security group with a rule that opens Port 2049 (the default NFS port) for file sharing. Update Security Group to the NFS Server VM only following this reference.
Installing and configuring NFS Server
-
Update System Packages:
sudo apt-get update && sudo apt-get upgrade -y
-
Install NFS Kernel Server:
sudo apt install nfs-kernel-server -y
-
Create and Attach an Empty Volume to the NFS Server:
3.1. Create an Empty Volume.
3.2. Attach the Volume to the NFS Server.
Verify the 100GiB (adjust based on your Storage requirements) attached volume is available on the NFS server VM:
lsblk ... NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS vda 253:0 0 20G 0 disk ├─vda1 253:1 0 19G 0 part / ├─vda14 253:14 0 4M 0 part ├─vda15 253:15 0 106M 0 part /boot/efi └─vda16 259:0 0 913M 0 part /boot vdb 253:16 0 100G 0 disk
3.3. Format And Mount The Volume.
Create a filesystem on the volume. In this example, we will create an
ext4
filesystem:sudo mkfs.ext4 /dev/vdb
3.4. Create a directory you want to share over the network:
sudo mkdir -p /mnt/nfs_share
3.5. Mount it:
sudo mount /dev/vdb /mnt/nfs_share
Verify the mount path is set correctly:
df -H ... Filesystem Size Used Avail Use% Mounted on tmpfs 411M 1.1M 410M 1% /run /dev/vda1 20G 2.0G 18G 10% / tmpfs 2.1G 0 2.1G 0% /dev/shm tmpfs 5.3M 0 5.3M 0% /run/lock /dev/vda16 924M 65M 795M 8% /boot /dev/vda15 110M 6.4M 104M 6% /boot/efi tmpfs 411M 13k 411M 1% /run/user/1000 /dev/vdb 106G 29k 100G 0% /mnt/nfs_share
-
Set the ownership and permissions to allow access (adjust based on requirements):
Since we want all the client machines to access the shared directory, remove any restrictions in the directory permissions.
sudo chown -R nobody:nogroup /mnt/nfs_share/
You can also tweak the file permissions to your preference. Here we have given the read, write and execute privileges to all the contents inside the directory.
sudo chmod 777 /mnt/nfs_share/
-
Configure NFS Exports:
Edit the
/etc/exports
file to define shared directories and permissions. Permissions for accessing the NFS server are defined in the/etc/exports
file. So open the file using your favorite text editor i.e. nano editor:sudo nano /etc/exports
Define Shared Directories and Permissions
You can provide access to a single client, multiple clients, or specify an entire subnet.
To grant access to a single client, use the syntax:
/mnt/nfs_share Client_Internal_IP_1(rw,sync,no_subtree_check)
For multiple clients, specify each client on a separate file:
/mnt/nfs_share Client_Internal_IP_1(rw,sync,no_subtree_check) /mnt/nfs_share Client_Internal_IP_2(rw,sync,no_subtree_check)
Add a line like this to share the directory with read/write permissions for a subnet (e.g., 192.168.0.0/24):
/mnt/nfs_share 192.168.0.0/24(rw,sync,no_subtree_check)
Explanation:
- rw: Read and write access.
- sync: Changes are written to disk immediately.
- no_subtree_check: Avoid permission issues for subdirectories.
Other Options for Directory Permissions for the NFS share directory
You can configure the shared directories to be exported by adding them to the
/etc/exports
file. For example:/srv *(ro,sync,subtree_check) /home *.hostname.com(rw,sync,no_subtree_check) /scratch *(rw,async,no_subtree_check,no_root_squash)
For more information read here.
-
Apply Export Settings with the shared directories:
sudo exportfs -rav
-
Retart NFS Service:
sudo systemctl restart nfs-kernel-server
Configure the NFS Client on the Client VM
-
Update System Packages:
sudo apt-get update && sudo apt-get upgrade -y
-
On the Client VM, install the required NFS package:
sudo apt install nfs-common -y
-
Create a Mount Point:
Create a local directory where the shared NFS directory will be mounted:
sudo mkdir -p /mnt/nfs_clientshare
-
Testing connectivity between the Client and Server using the
showmount
command:showmount --exports <NFS_SERVER_INTERNAL_IP>
For e.g.,
showmount --exports 192.168.0.73 ... Export list for 192.168.0.73: /mnt/nfs_share 192.168.0.0/24
-
Mount the Shared Directory:
Use the
mount
command to connect to the NFS Server and mount the directory.sudo mount -t nfs <NFS_SERVER_INTERNAL_IP>:/mnt/nfs_share /mnt/nfs_clientshare
For e.g.,
sudo mount -t nfs 192.168.0.73:/mnt/nfs_share /mnt/nfs_clientshare
-
Verify the Mount:
Check if the directory is mounted successfully.
Verify the mount path is set correctly:
df -H ... Filesystem Size Used Avail Use% Mounted on tmpfs 411M 1.1M 410M 1% /run /dev/vda1 20G 2.0G 18G 10% / tmpfs 2.1G 0 2.1G 0% /dev/shm tmpfs 5.3M 0 5.3M 0% /run/lock /dev/vda16 924M 65M 795M 8% /boot /dev/vda15 110M 6.4M 104M 6% /boot/efi tmpfs 411M 13k 411M 1% /run/user/1000 192.168.0.73:/mnt/nfs_share 106G 0 100G 0% /mnt/nfs_clientshare
You should see the NFS share listed that is mounted and accessible.
How to Unmount Shared Directory, if not needed!
When you're finished with a mount, we can unmount it with the umount
command.
sudo umount /mnt/nfs_clientshare
Make the Mount Persistent on the Client VM
An alternative method to mount an NFS share from another machine is by adding a
line to the /etc/fstab
file. This line should specify the floating IP of the
NFS Server, the directory being exported on the NFS Server, and the directory
on the local Client VM where the NFS share should be mounted. This setup ensures
the NFS share is automatically mounted at boot time, even after the Client VM is
rebooted.
The general syntax for the line in /etc/fstab
file is as follows:
example.hostname.com:/srv /opt/example nfs rsize=8192,wsize=8192,timeo=14,intr
-
Edit
/etc/fstab
Open the file:sudo nano /etc/fstab
-
Add an entry like this:
<NFS_SERVER_INTERNAL_IP>:/mnt/nfs_share /mnt/nfs_clientshare nfs defaults 0 0
For e.g.,
192.168.0.73:/mnt/nfs_share /mnt/nfs_clientshare nfs defaults 0 0
-
Test the Configuration Unmount and remount all filesystems listed in
/etc/fstab
:sudo umount /mnt/nfs_clientshare sudo mount -a
-
Verify the Mount:
Check if the directory is mounted successfully.
df -H
Test the Setup
-
On the NFS Server, write a test file:
echo "Hello from NFS Server" | sudo tee /mnt/nfs_share/test.txt
-
On the NFS Client, verify the file is accessible:
cat /mnt/nfs_clientshare/test.txt