How to set up an NFS server on Ubuntu 16

Try it in our public cloud & Get $50 Credit
CLAIM NOW

NFS, short for Network File System, is a protocol that allows remote access of files over a network in the same manner that you would access your local file system. It’s an older protocol, first being developed in 1984, but it’s been regularly updated and maintained since then, with the latest version, 4.1, coming out in 2010.

This guide will walk you through setting up and mounting an NFS server of your own.

Getting Started

To follow the steps in this guide, make sure you have this first:
• 2 servers (Cloud Server or Dedicated Server), each with Ubuntu 16
• Root access to both machines

Tutorial

Here’s the LAN addresses we’ll be working with:
Server 1: client – 10.10.0.134
Server 2: nfs-server – 10.10.0.135

First we’ll do some network setup. Edit the hosts file on both servers and add these entries.

nano /etc/hosts

code class=”gris”>10.0.0.134 client
10.0.0.135 nfs-server

Let’s deal with the NFS server first. Install the following package, after making sure the server is fully up to date.

apt-get install nfs-kernel-server -y

Create the shared directory for NFS. This is where you’ll store the files that you want the client server to access.

mkdir /sharednfs

Now we can create the exports file. This file identifies which directories you’ll be sharing. We can add the directory we created in the previous step.

nano /etc/exports
/sharednfs 10.0.0.134(rw,sync,no_subtree_check)

Restart the NFS service so that it loads the export file.

systemctl restart nfs-kernel-server.service
exportfs -a

Let’s deal with the client server next. Install the NFS package.

apt-get install nfs-common -y

We need to create a shared directory for mounting the nfs server.

mkdir /sharednfs/

Now it’s a simple task to mount the shared directory.

mount nfs-server:/sharednfs /sharednfs/

To explain the NFS addresses we use:

nfs-server:/sharednfs is located on the nfs-server
/sharednfs is located on the client server

Double check that the NFS shared directory is indeed mounted.

df -h

root@ubuntu16-client:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 491M 0 491M 0% /dev
tmpfs 100M 3.2M 97M 4% /run
/dev/sda1 9.7G 952M 8.7G 10% /
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 497M 0 497M 0% /sys/fs/cgroup
tmpfs 100K 0 100K 0% /run/lxcfs/controllers
nfs-server:/sharednfs 9.7G 953M 8.7G 10% /sharednfs <---------------------- Shared NFS Server tmpfs 100M 0 100M 0% /run/user/0 root@ubuntu16-client:~#

To mount it automatically on boot, add the mount point to the fstab on the client machine.

nano /etc/fstab
nfs-server:/sharednfs /sharednfs nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

Let's try it out! Go to the NFS server and add a file in the shared directory.

cd /sharednfs
touch test.txt

Go to the Client server. Voila, you should see the file that you added on the NFS server.

cd /sharednfs
ls

test.txt

Conclusion

NFS is a versatile protocol with many, many use cases. Now that you know how to set it up, you can experiment to see just how you can incorporate it into your projects. If you found this article helpful, feel free to share it with your friends and let us know in the comments below!