This simple and brief blog post is going to show you how to create NFS shares in Ubuntu and mount those shares from external NFS clients.
NFS stands for Network File System and is a popular filesystem protocol that allows for remote clients to mount network shares on NFS server. With this, users (clients) can mount a single directory on a NFS server from multiple locations easily.
In this post, we’re going to install NFS server, a create folder to share and mount that folder remotely using NFS protocol. It sounds complicated but easy to implement.
There are many good tutorials and articles online that describe in details what NFS protocol is and how to use it in production environments. This post isn’t going to go into details here. It will show you how to do the basic setup to get it working. Once you have the idea, it should be easy reading and implementing more advanced setup.
Ubuntu NFS
- Installing NFS Server in Ubuntu
For Ubuntu NFS to work, we’re must install NFS server on a host machine that we’ll be using to share directories. This host machine will host files and directories the clients will mount.
To install NFS server packages in Ubuntu, run the commands below.
sudo apt-get install nfs-kernel-server
Next, create a directory to share with NFS clients. You can choose to share existing directories, but for this tutorial, we’re going to create a new folder called nfs in the /var directory.
To do that, run the commands below to a NFS folder in the /var directory.
sudo mkdir /var/nfs
Next, think of who will be accessing this folder. By default, this folder is owned by root or the administrator on the server computer. To make it available to other users or a particular user, you must change the permission on the server computer. The accounts accessing it must live on the server computer.
To do that, run the commands below to give user richard access to the folder remotely.
sudo chown richard:richard /var/nfs
- Installing NFS client in Ubuntu
Now, hop on to the client computer and install NFS client. This package provides functionality to mount NFS shares on remote clients.
To install it, run the commands below.
sudo apt-get install nfs-common
At this point, you should have NFS server packages installed on the server computer, NFS client components installed on the client computer and NFS directory to be shared created on the host (server computer).
- Configuring NFS Exports (Sharing) on the server
To make a shared resource available, NFS server looks into its /etc/exports file. This file contains definitions and controls of how resources are shared and what permissions are assigned them.,
Each line in the file represents a shared resource, The format to share a resource is shown below
directory_to_share client_IP(share_options...., more options)
For this tutorial, our server computer has an IP address of 192.168.0.1. Our client computer has an IP address or 192.168.0.2. You can give access to a single computer via its IP or a IP SubNet.
So, to allow the client computer with IP address 192.168.0.2 to access the /var/nfs folder on the server, the line below should be entered into the server /etc/exports file.
Edit the file using the commands below
sudo vi /etc/exports
Then enter the line below:
/var/nfs 192.168.0.2(rw,sync,no_root_squash,no_subtree_check)
Next, export the shared directory using the commands below. the commands below make all NFS shares visible to clients.
sudo exportfs -a
Then go to the client computer to mount the shared directory above.
- Mounting NFS shares in Ubuntu
Now that the directory is shared via NFS, it’s not time to mount the share. At this point you should already have nfs-common installed.
First create a mount point on the client computer. A mount point is where you mount stuff. For this post, I am going to create a mount in the /mnt directory called NFS. To do that, run the commands below.
sudo mkdir /mnt/nfs
Next, test to see if you can mount directory by running the commands below.
sudo mount 192.168.0.1:/var/nfs /mnt/nfs
You should see it mounted in the /mnt/nfs directory on the client machine. To test it, create a file and see if it appears on the server side and vice versa.
If the test was successful, then you can move to the next step to permanently mount directory on the client computer. To do that, open the /etc/fstab file by running the commands below.
sudo vi /etc/fstab
Then add the line below in the file and save it.
192.168.0.1:/var/nfs /mnt/nfs nfs auto,noatime,nolock,bg,nfsvers=4,sec=krb5p,intr,tcp,actimeo=1800 0 0
That’s it!