How To Setup an NFS Server and Client For File Sharing

How To Setup an NFS Server and Client For File Sharing

In this tutorial, I will explain how to set up an NFS server and client configurations for mounting an NFS share across systems in a network.[alert style="e.g. white,

[alert style="e.g. white, grey, red, yellow, green"] Note: This tutorial is based on Ubuntu 14.04 server. You can create a VM easily on the cloud or using vagrant. [/alert]

Setup an NFS server

In this setup, I have a ubuntu server with IP address 192.168.0.2.

  1. Refresh the apt package index
sudo apt-get update
  1. Install the NFS package.
sudo apt-get install nfs-kernel-server
  1. Next, we need to create a directory that can be shared with other hosts in the network. I am going to create a folder in var directory.
sudo mkdir /var/nfs
  1. Change the ownership of the NFS folder to β€œnobody” and β€œnogroup”.
sudo chown nobody:nogroup /var/nfs

The "nobody" is a user present in most of the Linux distros which belong to the "nogroup" which does not have any privileges on the system programs or files.

  1. All the NFS configurations are set in the /etc/exports file. In which we can give specific permissions for a client to access the files in the share.

In this example, I have a client with IP 192.168.0.3. Open the exports file and make an entry as shown below.

/var/nfs    192.168.0.3(rw,sync,no_subtree_check)
  1. NFS table holds all the exports of the shares. You can create one using the following command.
sudo exportfs -a
  1. Now, let’s start the NFS service.
sudo service nfs-kernel-server start

That's it! We have an NFS server up and running.

Setup NFS client Node

All the servers which need access to the NFS share need to install the NFS client packages.

  1. Refresh the apt list and install the client package.
sudo apt-get update 
sudo apt-get install nfs-common
  1. Let’s create a folder that will be mounted to the remote NFS share.
sudo mkdir /mnt/nfs/
  1. Now, mount the remote NFS directory with out local /mnt/nfs directory.
sudo mount 192.168.0.2:/var/nfs  /mnt/nfs
  1. Verify the mount using the following command. You will see NFS share listed in the file system.
df -h

The output looks like the following.

192.168.0.2:/var/nfs   40G  1.4G   37G   4% /mnt/nfs
  1. Now you can test the share by creating a test file in /var/nfs folder of the NFS server and try to list that in your clients /mnt/nfs folder.

That's it! You now have an NFS share server and client up and running. You can add more clients in the same way we set up our first client.

About the author
devopscube

devopscube

Since 2014, we're a thriving DevOps community dedicated to helping DevOps Engineers grow with high-quality, in-depth articles, comprehensive learning paths, expert insights, and practical resources.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to DevOpsCube – Easy DevOps, SRE Guides & Reviews.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.