NFS

Network File System (NFS) is a distributed file system protocol originally developed by Sun Microsystems in 1984, allowing a user on a client computer to access files over a network in a manner similar to how local storage is accessed.

Note:

NFS is not encrypted. Tunnel NFS through an encrypted protocol like Kerberos, or tinc when dealing with sensitive data.

Unlike Samba, NFS doesn't have any user authentication by default, client access is restricted by their IP-address/hostname.

NFS expects the user and/or user group ID's are the same on both the client and server. It is however possible to overrule the UID/GID by using anonuid/anongid with all_squash in /etc/exports.

Installation

It is highly recommended to use a time synchronization daemon to keep client/server clocks in sync. Without accurate clocks on all nodes, NFS can introduce unwanted delays.

Configuration

Server

Global configuration options are set in /etc/nfs.conf. Users of simple configurations should not need to edit this file.

The NFS server needs a list of exports (directories to share) which are defined in /etc/exports or /etc/exports.d/*.exports. These shares are relative to the so-called NFS root. A good security practice is to define a NFS root in a discrete directory tree which will keep users limited to that mount point. Bind mounts are used to link the share mount point to the actual directory elsewhere on the filesystem.

Consider this following example wherein:

The NFS root is /srv/nfs.

The export is /srv/nfs/music via a bind mount to the actual target /mnt/music.

Starting the server

Warning: A hard dependency of serving NFS (rpc-gssd.service) will wait until the random number generator pool is sufficiently initialized possibly delaying the boot process. This is particularly prevalent on headless servers. It is highly recommended to populate the entropy pool using a utility such as Rng-tools (if TPM is supported) or Haveged in these scenarios.

If you are exporting ZFS shares, also start and enable zfs-share.service. Without this, ZFS shares will no longer be exported after a reboot.

Miscellaneous

Restricting NFS to interfaces/IPs

By default, starting nfs-server.service will listen for connections on all network interfaces, regardless of /etc/exports. This can be changed by defining which IPs and/or hostnames to listen on.

Enable NFSv4 idmapping

Note: Another option is to make sure the UID's/GID's match on both the client and server.

The NFSv4 protocol represents the local system's UID and GID values on the wire as strings of the form user@domain. The process of translating from UID to string and string to UID is referred to as ID mapping[1].

Even though idmapd may be running, it may not be fully enabled. Verify if /sys/module/nfsd/parameters/nfs4_disable_idmapping returns N, on disabled run:

Static ports for NFSv3

Users needing support for NFSv3 clients, may wish to consider using static ports. By default, for NFSv3 operation rpc.statd and lockd use random ephemeral ports; in order to allow NFSv3 operations through a firewall static ports need to be defined. Edit /etc/sysconfig/nfs to set STATDARGS:

/etc/sysconfig/nfs

STATDARGS="-p 32765 -o 32766 -T 32803"

The rpc.mountd should consult /etc/services and bind to the same static port 20048 under normal operation; however, if it needs to be explicity defined edit /etc/sysconfig/nfs to set RPCMOUNTDARGS:

/etc/sysconfig/nfs

RPCMOUNTDARGS="-p 20048"

After making these changes, several services need to be restarted; the first writes the configuration options out to /run/sysconfig/nfs-utils (see /usr/lib/systemd/scripts/nfs-utils_env.sh), the second restarts rpc.statd with the new ports, the last reloads lockd (kernel module) with the new ports. Restart these services now: nfs-config, rpcbind, rpc-statd, and nfs-server.

After the restarts, use rpcinfo -p on the server to examine the static ports are as expected. Using rpcinfo -p <server IP> from the client should reveal the exact same static ports.

Note: Server name needs to be a valid hostname (not just IP address). Otherwise mounting of remote share will hang.

Mount using /etc/fstab

Using fstab is useful for a server which is always on, and the NFS shares are available whenever the client boots up. Edit /etc/fstab file, and add an appropriate line reflecting the setup. Again, the server's NFS export root is omitted.

The rsize value is the number of bytes used when reading from the server. The wsize value is the number of bytes used when writing to the server. The default for both is 1024, but using higher values such as 8192 can improve throughput. This is not universal. It is recommended to test after making this change, see #Performance tuning.

soft or hard

Determines the recovery behaviour of the NFS client after an NFS request times out. If neither option is specified (or if the hard option is specified), NFS requests are retried indefinitely. If the soft option is specified, then the NFS client fails a NFS request after retrans retransmissions have been sent, causing the NFS client to return an error to the calling application.

Warning: A so-called soft timeout can cause silent data corruption in certain cases. As such, use the soft option only when client responsiveness is more important than data integrity. Using NFS over TCP or increasing the value of the retrans option may mitigate some of the risks of using the soft option.

timeo

The timeo value is the amount of time, in tenths of a second, to wait before resending a transmission after an RPC timeout. The default value for NFS over TCP is 600 (60 seconds). After the first timeout, the timeout value is doubled for each retry for a maximum of 60 seconds or until a major timeout occurs. If connecting to a slow server or over a busy network, better stability can be achieved by increasing this timeout value.

retrans

The number of times the NFS client retries a request before it attempts further recovery action. If the retrans option is not specified, the NFS client tries each request three times. The NFS client generates a "server not responding" message after retrans retries, then attempts further recovery (depending on whether the hard mount option is in effect).

_netdev

The _netdev option tells the system to wait until the network is up before trying to mount the share - systemd assumes this for NFS, although automount may be a more preferred solution.

Note: Setting the sixth field (fs_passno) to a nonzero value may lead to unexpected behaviour, e.g. hangs when the systemd automount waits for a check which will never happen.

Mount using /etc/fstab with systemd

Another method is using the systemd automount service. This is a better option than _netdev, because it remounts the network device quickly when the connection is broken and restored. As well, it solves the problem from autofs, see the example below:

One might have to reboot the client to make systemd aware of the changes to fstab. Alternatively, try reloading systemd and restarting mountpoint-on-client.automount to reload the /etc/fstab configuration.

Tip:

The noauto mount option will not mount the NFS share until it is accessed: use auto for it to be available immediately. If experiencing any issues with the mount failing due to the network not being up/available, enableNetworkManager-wait-online.service. It will ensure that network.target has all the links available prior to being active.

The users mount option would allow user mounts, but be aware it implies further options as noexec for example.

The x-systemd.idle-timeout=1min option will unmount the NFS share automatically after 1 minute of non-use. Good for laptops which might suddenly disconnect from the network.

If shutdown/reboot holds too long because of NFS, enableNetworkManager-wait-online.service to ensure that NetworkManager is not exited before the NFS volumes are unmounted. You may also try to add the x-systemd.requires=network-online.target mount option if shutdown takes too long.

Using mount options as noatime, nodiratime, noac, nocto may be used to increase NFS performance.

Mount using autofs

Using autofs is useful when multiple machines want to connect via NFS; they could both be clients as well as servers. The reason this method is preferable over the earlier one is that if the server is switched off, the client will not throw errors about being unable to find NFS shares. See autofs#NFS network mounts for details.

Tips and tricks

Performance tuning

When using NFS on a network with a significant number of clients one may increase the default NFS threads from 8 to 16 or even a higher, depending on the server/network requirements:

/etc/nfs.conf

[nfsd]
threads=16

It may be necessary to tune the rsize and wsize mount options to meet the requirements of the network configuration.

In recent linux kernels (>2.6.18) the size of I/O operations allowed by the NFS server (default max block size) varies depending on RAM size, with a maximum of 1M (1048576 bytes), the max block size of the server will be used even if nfs clients requires bigger rsize and wsize. See https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/5.8_Technical_Notes/Known_Issues-kernel.html
It is possible to change the default max block size allowed by the server by writing to the /proc/fs/nfsd/max_block_size before starting nfsd. For example, the following command restores the previous default iosize of 32k:

Automatic mount handling

This trick is useful for NFS-shares on a wireless network and/or on a network that may be unreliable. If the NFS host becomes unreachable, the NFS share will be unmounted to hopefully prevent system hangs when using the hard mount option [4].