NFS or Network file system is a distributed filesystem protocol. It allows a remote host to mount filesystem over a network and interact that filesystem much like local storage is accessed.
It follows the client-server model. The mounted filesystem can be accessed by the client with whatever privileges assigned to each file. Like many other protocols, NFS is also built on ONC RPC (Open Network Computing Remote Procedure Calls).
The Sun microsystems developed the first version of NFS in 1984. Ahead in this article, we will see how we can configure the client and server components in Linux to use NFS in it.
Major Versions of NFS
It is important to know how many versions of NFS available till now and what is the latest. This will give you an idea of whether to upgrade or not.
Currently, there are three major versions of NFS and these are given below-
1. NFSv2 –
- Released in March 1989 and is defined in RFC 1094
- It was originally operated only over UDP
- It allows only 2GB of a file to be read due to 32-bit limitation
2. NFSv3 –
- It supports 64-bit file size and offsets It can handle the file size larger than 2GB
- Defined in RFC 1813 and released in 1995
- It supports for asynchronous writes on the server to improve the write performance
- It added a READDIRPLUS operation to get filehandles and attributes along with file names while scanning the directory
- Uses TCP as a transport layer protocol
3. NFSv4 –
- Originally it was defined in RFC 3010 in 2000 and revised two times in 2003 and 2005
- Version 4 NFS is influenced by Andrew file system(AFS) and Server message block (SMB also termed as CIFS)
- Emphasizes on performance improvements, mandate strong security and introduces a stateful protocol
- It includes new features like server-side clone & copy, an application I/O advice, ADB/Application Data Block, space reservation, etc.
Setting up NFS server
While setting up an NFS server follow the steps given below –
1. The first thing is to download all the packages that are needed on the server-side. Execute the following commands to install the required packages.
sudo apt-get install nfs-kernel-server -y
sudo apt-get install nfs-common -y
sudo apt-get install portmap -y
The nfs-common package can be used on a computer (either a server or client) that uses NFS.
It includes programs like nfsstat, lockd, statd, showmount, gssd, idmapd, and mount.nfs. Portmap or rpcbind utility maps RPC services to the port on which they are listening.
To display the RPC services along with the information like port they use, their version, etc use the following command –
rpcinfo -p
[alert color=”red”]Please note that NFSv4 doesn’t require portmap or rpcbind[/alert]
2. Create an export directory
Create a directory that is to be shared with the clients. This directory will also be known as the export directory. Execute the following command to create an export directory-
sudo mkdir -p /mnt /nfsshare
Set the permission for the created directory by using the following command –
sudo chown nobody:nogroup /mnt/nfsshare
sudo chmod 777 /mnt/nfsshare
This will allow accessing the directory by all the users from all the groups on the client system. You can check the ownership and permission of directory by using ls -al
command in your terminal-
3. Assign the server access to clients
The access of a file can be controlled from the server to do that we will have to mention the client with the permission that they will have in /etc/exports
. Execute the following command in your terminal to open exports file with a text editor –
sudo nano /etc/exports
And add the following line in this configuration file-
/mnt/nfsshare clientIP (rw, sync, nosubtree_check)
In the same way, you can add multiple clients. After making changes in the config file Its time to export the shared directory through the following command –
sudo exportfs -avrf
Finally, save the changes and restart the NFS server with the following command –
sudo /etc/init.d/nfs-kernel-server restart
Configuring client component for NFS
First, install the nfs-common and port map or rpcbind on the client computer by using the following command –
sudo apt-get install nfs-common portmap -y
Create a mount point for the NFS host’s shared directory. A directory on the client computer is needed where we can access the data shared by the export directory of the server.
sudo mkdir -p /mnt/nfsshare_client
Mount the shared directory of the client by using the following command
sudo mount -t nfs ServerIP: /mnt/nfsshare /mnt/nfsshare_client
You can check that directory is mounted or not by using $mount
command in your terminal.
Add firewall settings for NFS client
Execute the following command in order to allow a client to access a port on the server-
sudo ufw allow from clientIP to any port nfs
Now check the status of your firewall.
sudo ufw status
How to Test the NFS setup?
You can test your NFS setup by creating a file inside the /mnt/nfsshare
on the server, the same file will be available in /mnt/nfsshare_client
of the client and vice-versa.
Here I created a file sample.txt in /mnt/nfsshare
and access it from /mnt/nfsshare_client
look at the image that is given below –
Similarly, you can create a file in /mnt/nfsshare_client
and access this shared file from /mnt/nfsshare
.
Tha’s all on setting up NFS in Linux. For more of such how-to guides stay tuned.