In this tutorial you will learn how to set up a NFS server (NFSv4).

Table of Contents

Install NFS server utilities

The required package names depends on the Linux distribution:

  • nfs-kernel-server in Debian/Ubuntu.
  • nfs-utils in Arch Linux.

Add a network share (a shared folder)

Edit /etc/exports with a root user or sudo. Add a line for every network share you want to add:

export_path allowed_hostname(options)
# An example:
/srv/nfs 192.168.1.0/24(ro)
  • You can add several hostnames (separated by spaces). Every hostname will need its options.
  • Some options you can use are:
    • ro: read-only.
    • rw: write permissions.
    • no-root-squash: root user on the client will have same permissions as root user on the server. Use with caution.
    • root-squash: root user on the client is assigned UID of nobody or nfsnobody user on the server, so they will not have special permissions on the server.
    • all_squash: NFS assigns UID for the user on the client (including root) to the UID of nobody or nfsnobody users on the server, so the client does not have special permissions if the UID on the client and the server are the same.

When adding a shared folder, it is recommended to use a bind mount instead of share the file directly. For example, if you want to share /home/user/music:

# as root
mkdir /srv/nfs/music
mount --bind /home/user/music /srv/nfs/music
# /etc/exports
/srv/nfs/music 192.168.1.0/24(rw)

Reload NFS server

Reload nfs-server service. Some systems don’t start and enable the service when nfs package is installed:

systemctl reload nfs-server
# Arch Linux
systemctl start nfs-server
systemctl enable nfs-server

NFS client

Check if your OS requires a package to be installed in order to be able to mount NFS shares (like nfs-utils in Arch Linux).

You can mount a NFS share like any other device:

# as root
mkdir /mnt/nfs
mount 192.168.1.8:/srv/music /mnt/nfs

Firewalls

If you need to access your NFS server outside your local network, you need to open port 2049 (TCP) on the server network.