Let's have a look today at how to setup an NFS share on a Linux machine. An NFS share allows you to share a folder on your Linux machine with other machines. It e.g. allows you to mount a shared folder from your Linux server on your local mac.

We'll start with installing and setting up the server part first. So, on the server which should host the shares (we're using Ubuntu here), execute:

$ sudo apt update
$ sudo apt install nfs-kernel-server

This updates the list of available packages and then installs the nfs-kernel-server package. This is the package which will allow us to share parts of our drive using NFS.

Now that we have the server installed, let's create a share. To create a share, it always follows the same procedure: create the folder, set the permissions, export (share) the folder and restart the NFS server. Creating the folder and setting the permissions is easy:

$ mkdir -p /var/nfs/my_shared_folder
$ chmod -R 0777 /var/nfs/my_shared_folder

To export (share) the folder, we need to declare it in a file called /etc/exports. Since we only have a single share defined, the file will contain:

/etc/exports

/var/nfs/my_shared_folder *(rw,sync,no_subtree_check,insecure,no_root_squash)

If you setup multiple shares, you'll have a line for each share. Once you added the configuration, all you need to do is to restart the NFS server so that it exports the volume:

$ sudo systemctl restart nfs-kernel-server

To mount this export on another Linux machine, we need to install another package:

$ sudo apt update
$ sudo apt install nfs-common

We can then create a folder which will be used as mount point and mount the export in there:

$ mkdir /nfs/my_shared_folder
$ sudo mount -t nfs my-other-server:/var/nfs/my_shared_folder my_shared_folder/

If you want the share to be mounted even after rebooting the machine, you should add it to the /etc/fstab file:

/etc/fstab

# <file system>                           <dir>                  <type>   <options>   <dump>  <pass>
my-other-server:/var/nfs/my_shared_folder /mnt/my_shared_folder  nfs      defaults    0       0

You can test if the fstab file is configured correctly by running one of these commands:

mount /mnt/my_shared_folder
mount my-other-server:/var/nfs/my_shared_folder

The mount command will read the content of the /etc/fstab and mount the share.

Next time you reboot the system the NFS share will be mounted automatically.

The umount command detaches (unmounts) the mounted file system from the directory tree.

To detach a mounted NFS share, use the umount command followed by either the directory where it has been mounted or remote share:

umount /mnt/my_shared_folder
umount my-other-server:/var/nfs/my_shared_folder

One more trick, if you want to know which exports are available on a server, you can use the showmount command:

$ showmount -e my-other-server
Export list for my-other-server:
/var/nfs/my_shared_folder *