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!

Frequently Asked Questions

How to install NFS server in Ubuntu?

To install NFS server in Ubuntu, you can run the command 'sudo apt-get install nfs-kernel-server'. This will install the necessary packages for setting up the NFS server.

What is NFS in Ubuntu?

NFS in Ubuntu stands for Network File System, which is a filesystem protocol that allows remote clients to mount network shares on an NFS server. It enables users to access a single directory on an NFS server from multiple locations.

How to create NFS shares in Ubuntu?

To create NFS shares in Ubuntu, you need to first create a directory that you want to share with NFS clients. You can do this by running the command 'sudo mkdir /var/nfs' to create a new folder called nfs in the /var directory.

How to mount NFS shares in Ubuntu from external clients?

To mount NFS shares in Ubuntu from external clients, you need to have the NFS server set up and running. Then, on the client side, you can use the 'mount' command to connect to the NFS server and access the shared directory.

What is the purpose of NFS protocol?

The purpose of NFS protocol is to allow for remote clients to mount network shares on an NFS server, enabling easy access to shared directories from multiple locations. It simplifies file sharing and collaboration across networked devices.

Can NFS shares be accessed from multiple locations?

Yes, NFS shares can be accessed from multiple locations. The NFS protocol allows users to mount a single directory on an NFS server from different clients, making it convenient for sharing files and data across networked devices.

How to make NFS shares available to specific users in Ubuntu?

To make NFS shares available to specific users in Ubuntu, you can adjust the permissions of the shared directory. By setting appropriate ownership and permissions, you can control which users have access to the NFS shares.

Are there detailed tutorials available for using NFS protocol in production environments?

Yes, there are many detailed tutorials and articles online that provide in-depth information on using the NFS protocol in production environments. These resources can help you understand advanced setups and configurations for efficient NFS usage.