Creating an NFS share

If you are trying to share between *nix systems, the Network File Servers (NFS) are really easy to set up. Note: If you are planning on sharing with Windows machines, SMB/Samba is a better option.

It should also be noted that NFS doesn’t restrict, by default, to individual users. It limits to IPs and IP ranges–which makes it ideal for secure networks, but less-than-ideal for non-secure / public networks.

It’s pretty straight forward (on debian/apt bases distributions):

Installing the NFS server

sudo apt-get update && sudo apt-get install nfs-kernel-server

Make the directory you would like to share. This directory can go anywhere but I think standard location is in the /mnt/ directory.

sudo mkdir -p /mnt/your-directory-to-share

This (below) changes the permissions to let all of the client machines access to the directory. You can change the permissions as well but for now I left it

sudo chown -R nobody:nogroup /mnt/your-directory-to-share`

Give the clients read,write, and execute permissions as well:

sudo chmod -R 777 /mnt/your-directory-to-share`

At this point your nfs-server is installed (not active) and your shared folder is setup. Now you need to grant access to the NFS share. To do that you have to edit the ‘exports’ file–which is found at /etc/exports.

sudo vim /etc/exports
/mnt/your-directory-to-share <ipaddress of the client>(rw,sync,no_subtree_check)

If you want to add specific IPs you have to create a new line for each of them. However, if you wanted to do an entire subnet/ip range you can write something like this: 192.168.1.0/24 (which would share 192.168.1.1-255)

rw = read/write ; sync = means changes have to be written to disk before they are applied ; no_subtree_check = means what it says. Apparently, subtree checking causes more problems than its worth, so most people recommend using this flag to turn it off.

According the NFS man pages and tutorials I found you are supposed to run the following in this order:

sudo exportfs -a
sudo systemctl restart nfs-kernel-server

This threw an error for me. I ran them a second time and it seems to work. I think you have to have the nfs-kernel-server running before you can do sudo exportfs -a. When you run the restart command it actually starts it for the first time, then you can follow the steps properly.

If it was up to me, I think it should be:

sudo systemctl start nfs-kernel-server

sudo exportfs -a

sudo systemctl restart nfs-kernel-server

Connect your client

So now you have the server serving up the shared drive. Now you need to connect to it.

On your laptop/pc/whatever is connecting to the NFS server you need to install the NFS client and mount the drive.

sudo apt-get install nfs-common

Make a directory/mount point for this. Again, this can be anywhere. If you wanted it to be in your home folder it can be, but we’ll stick with /mnt/nfsshare

sudo mkdir -p /mnt/nfsshare

Then you mount it:

sudo mount <ip-of-NFS-server>:/mnt/your-directory-to-share /mnt/nfsshare

And thats it. When you navigate to /mnt/nfsshare on your client machine you will see all of the contents of the nfs drive. Which at this point would be empty.

If you want to mount it permanently, just edit your /etc/fstab file by adding a line at the end:

ip-of-NFS-server>:/mnt/your-directory-to-share /mnt/nfsshare nfs defaults 0 0

This article was updated on October 17, 2021