How to Set Up NFS for File Sharing Between Servers: A Step-by-Step Guide

Today, shared filesystems have become increasingly popular due to the growing emphasis on high availability and microservice applications. Therefore, in this write-up, I will guide you through the process of securely sharing your file system between servers using the NFS protocol.

Prerequisites

  • Basic knowledge of the terminal
  • 3 VMs ( 1 host and 2 clients )
  • A smile on your face (Put up that smile friend ?)

we will agree between us that the ip adresse of host is 10.50.1.1 and the clients are 10.50.1.2 and 10.50.1.3

What i mean by Host VM , is the machine that will share a directory and the client is who will could read and write in this directory.
So firstly we need to install NFS server by using the following command:

apt-get install nfs-kernel-server

And now we will create a sharedfolder by:

mkdir /sharedfolder

Now, we will proceed to setting up a configuration file that will enable the client to read and write on the shared filesystem:

nano /etc/exports

And we will add the following line:

/sharefolder 10.50.1.2(rw,sync,no_subtree_check,no_root_squash) 10.50.1.3(rw,sync,no_subtree_check,no_root_squash)

“(rw,sync,no_subtree_check,no_root_squash)” are options that define the permissions and behavior of the NFS export:

  • rw: Stands for “read-write” and allows clients to both read from and write to the exported directory.
  • sync: Ensures that data is written to disk before acknowledging the client’s write request, providing greater data consistency.
  • no_root_squash : Allows the root user on the client machine to have root-level access to the exported directory, rather than being mapped to the “nobody” user on the server.
  • no_subtree_check: Disables subtree checking, which can improve performance when exporting large directory hierarchies but may pose a security risk in certain scenarios.

we are done now with Host Vm and we should restart the service.

service nfs-kernel-server restart

Now we will move to clients VM:

we should create a folder and mount it :

mkdir /share
nano /etc/fstab
#add the following line
10.50.1.1:/sharedfolder   /share   nfs   defaults   0  0 

and mount this file by:

mount -a

REMARK

this configuration for ubuntu system and could change according to your os .

Finally, after following these steps you will able to share a filesystem between servers and manage your data easily.