Objective

Install and configure NFS on Ubuntu 18.04

Distributions

Ubuntu 18.04

Requirements

A working install of Ubuntu 18.04 with root privileges

Difficulty

Easy

Conventions

  • # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - requires given linux commands to be executed as a regular non-privileged user

Introduction

Sharing files is a central function of Linux systems. It's so central, in fact, that file sharing functionality is built directy into the kernel itself. It's also important enough that the developers of Debian, and Ubuntu in turn, have made NFS file sharing very simple.

Server Setup

You'll be doing the majority of the configuration on the server. That's the system that's going to be doing the sharing. Even still, there really isn't that much. You essentially just need to tell NFS what to share.

Install The Server

There is only one package that you need for Ubuntu to run an NFS server. Install it with Apt.
$ sudo apt install nfs-kernel-server

Configure Your Exports

Telling NFS what to share is very simple. Everything is listed in the /etc/exports file. In that file, you'll list the directories that you want shared. Across from each, you'll list which IP address you want to share it with and the options that you want it to use. The lines look something like this:
/home/username/share		192.168.1.0/24(rw,sync,no_subtree_check)
That line will share the directory at /home/username/share with all other computers on the same subnet of the network. It will allow users to read and write from the share, and it will keep the share in sync, preventing potential conflicts. There are a number of options that you can choose from, dependig onhow you want to set up your share.
  • ro: specifies that the directory may only be mounted as read only
  • rw: grants both read and write permissions on the directory
  • no_root_squash: is an extremely dangerous option that allows remote “root” users the same privilege as the “root” user of the host machine
  • subtree_check: specifies that, in the case of a directory is exported instead of an entire filesystem, the host should verify the location of files and directories on the host filesystem
  • no_subtree_check: specifies that the host should not check the location of the files being accessed withing the host filesystem
  • sync: this just ensures that the host keeps any changes uploaded to the shared directory in sync
  • async: ignores synchronization checks in favor of increased speed
When you're done, save the file and exit.

Restart

The last thing that you need to do is reastart the service. It will automatically reload and share the files that you specified.
$ sudo systemctl restart nfs-kernel-server

Client Setup

The client setup is even simpler. There is no configuration needed. You only have to install the right package and mount the volume.

Install The Client

You don't need the kernel server on the client. Only the common NFS functionality. Install that package.
$ sudo apt install nfs-common

Mount The Share

Now, you can mount your share as root or with sudo. You do so by passing the IP address of the server followed by the path to the share.
$ sudo mount 192.168.1.110:/home/username/share /mnt/nfs
You can mount the share just about anywhere. It's a good idea, thought to designate the directories that you're going to use, though, if you plan to mount the share regularly.

Mount On Startup

Of course, you can choose to automatically mount the share whenever the client computer starts up. You can do so easily using the /etc/fstab file. An entry in /etc/fstab would look like the following:
192.168.1.110:/home/username/share		/mnt/nfs	nfs defaults,user,exec		0 0
Again, it follows more or less the same exact structure as a regular entry, but you need to specify the remote location with the server's IP, and tell fstab that the share is an NFS share.

Closing Thoughts

NFS is a very streamlined approach to sharing files across Linux systems. On Ubuntu, it's simple to use and can work virtually transparently once configured. The only drawback to NFS is that it's a Linux-only sharing system. Don't expect it to play nice with Windows machines.