1. Home
  2. Linux

How To Host NFS Shares On Linux

If you’re a moderately skilled Linux user, or a beginner with a server, the main way you access files is with Samba. Many do this because of how difficult NFS appears to be. There’s so many steps, and little things you’ve got to get right compared to something like Samba or even FTP. The fact that many Linux users pass over NFS is a real shame, as it has a lot of great features and uses. That’s why we’ve decided to take the difficulty out of setting up an NFS server. We’ll break down how you can host NFS shares on Linux, and explain it in depth how you can get a server up and running

Installation

Generally, each Linux distribution has the same NFS tools with a few differences here and there. To get everything working, open a terminal and install the following packages to your system. These packages will install the default technology needed to run the server, generate the default configuration files, and sometimes even configure other things too.

Ubuntu

sudo apt install nfs-kernel-server

Fedora

sudo dnf install nfs-utils system-config-nfs

Arch Linux

sudo pacman -S nfs-utils

Debian

sudo apt-get install nfs-kernel-server

OpenSUSE

sudo zypper install nfs-kernel-server

Other Linuxes

The NFS file system, client, and server is not new technology. As a result, you’ll easily be able to get this working on pretty much any Linux distribution out there. If your operating system wasn’t covered, consider searching for “nfs utils” in the package manager, or by referring to your operating system’s Wiki.

Systemd services

Setting up an NFS server can be different, depending on what Linux distribution you are using. For example, if you install the Ubuntu or Debian NFS package, the systemd services are automatically set up and configured for you. However, on something like Arch Linux, SUSE or Fedora and etc, you’ll need to do it manually. Here’s how to enable these services.

If you’re running Arch Linux, Open SUSE or Fedora (or any Linux distribution that doesn’t automatically set up the systemd services for NFS) open up a terminal and enter these commands.

sudo systemctl enable rpcbind
sudo systemctl enable nfs-server 

sudo service rpcbind start 

sudo service nfs-server start

Configuration

By installing the NFS packages, the server is “set up”, but that doesn’t mean users can access it yet. For this, users need to set up exports, file locations, etc. To start off, gain a root shell in the terminal. Do this with: sudo -s

The export file can handle any type of directory. In fact, users can easily write out a direct location to a folder they wish to share right on the file system. The problem with this is that it is a huge security risk. It’s much safer to take the contents of one directory and bind it to another.  This ensures that permissions for folders and directories in the /srv/ folder don’t need to be modified making everything secure.

For example, to set up an NFS share of a user’s Pictures folder, do the following:

mkdir -p /srv/nfs/pictures /home/username/Pictures/

mount --bind /home/username/Pictures/ /srv/nfs/pictures/

Use the example above to make as many bind mounts as you’d like. Keep in mind that it’s not required to mount directories from home folders. Instead use that example and get creative.

With all that sorted out, run these commands to make the mounts stick:

echo '#NFS Pictures Bind Mount' >> /etc/fstab
echo '/home/username/Pictures/ /srv/nfs/pictures/ none bind 0 0' >> /etc/fstab

Note: you’ll need to change the echo commands to suit your own mounts.

With the mounts set up, it’s time to configure the exports configuration file. The exports file is very important, as this is where you’ll need to specify each file share for NFS to utilize.

nano /etc/exports

Inside the export file, go all the way to the bottom, and write the following:

/srv/nfs/pictures/ 192.168.0.0/255.255.255.0(rw,sync,no_subtree_check)

Feel free to write out as many lines as you want. Keep in mind that each share needs its own mount, fstab entry, and /etc/exports entry.

Note: save the exports file with CTRL + O

Updating Changes

The NFS server is up and running, the files are in the right place, and everything is ready to use.  The only thing left is to update the changes made to the exports file. This is an important step since the NFS server wont update changes made while it’s running. Do changes with: exportfs -rav

If need be, restart the server process within systemd itself by using the systemctl restart command.

sudo systemctl restart nfs-server.

Conclusion

The Network File System is a useful tool. With it, users can easily take remote files and directories, and work with them locally just like a physical hard drive. This is great, as competing network file system tools do not give users the same ubiquity and freedom.

Despite it’s usefulness, most people prefer to use Samba instead. This is understandable, because compared to Samba, NFS can be a bit annoying and tedious to set up. However,  if you deal with the difficult setup process, it’s much more worth it.

Leave a comment