From ArchWiki
Revision as of 15:05, 20 July 2014 by TE (Talk | contribs) (add static ports info for firewalls and NFSv3)

Jump to: navigation, search

Related articles

From Wikipedia:

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.


Both client and server only require the installation of the nfs-utils package.

Note: It is HIGHLY recommended to use a time sync daemon on ALL nodes to keep client/server clocks in sync. Without accurate clocks on all nodes, NFS can introduce unwanted delays. The Network Time Protocol daemon is recommended to sync both the server and the clients to the highly accurate NTP servers available on the Internet.



ID mapping

Edit /etc/idmapd.conf and set the Domain.

Verbosity = 1
Pipefs-Directory = /var/lib/nfs/rpc_pipefs
Domain = atomic


Nobody-User = nobody
Nobody-Group = nobody

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/conf.d/nfs-common.conf to set STATD_OPTS:

STATD_OPTS="-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/conf.d/nfs-server.conf to set MOUNTD_OPTS:

MOUNTD_OPTS="-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/, the second restarts rpc.statd with the new ports, the last reloads lockd (kernel module) with the new ports.

# systemctl restart nfs-config
# systemctl restart rpc-statd
# systemctl restart 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.

Optional configuration

/etc/conf.d/nfs-server.conf holds optional configurations for options to pass to rpc.nfsd, rpc.mountd, or rpc.svcgssd. Users setting up a simple configuration may not need to edit this file.

File system

Note: For security reasons, it is recommended to use an NFS export root which will keep users limited to that mount point only. The following example illustrates this concept.

Define any NFS shares in /etc/exports which are relative to the NFS root. In this example, the NFS root will be /srv/nfs4 and we will be sharing /mnt/music.

# mkdir -p /srv/nfs4/music

Read/Write permissions must be set on the music directory so clients may write to it.

Now mount the actual target share, /mnt/music to the NFS share via the mount command:

# mount --bind /mnt/music /srv/nfs4/music

To make it stick across server reboots, add the bind mount to fstab:

/mnt/music /srv/nfs4/music  none   bind   0   0


Add directories to be shared and an ip address or hostname(s) of client machines that will be allowed to mount them in exports:

/srv/nfs4/music,no_subtree_check,nohide) # note the nohide option which is applied to mounted directories on the file system.

Users need-not open the share to the entire subnet; one can specify a single IP address or hostname as well.

For more information about all available options see man 5 exports.

Note: Modifying /etc/exports while the server is running will require a re-export for changes to take effect.
# exportfs -rav

NFSv2 compatibility

If you need to support clients using NFSv2 (for example U-Boot), set NFSD_OPTS="-V 2" in /etc/conf.d/nfs-server.conf.

Starting the server

Start nfs-server.service using systemd. It is recommended to also enable the service to allow systemd to start it at boot time. Note that this unit requires other services, which are launched automatically by systemd.

Firewall configuration

To enable access through a firewall, tcp and udp ports 111, 2049, and 20048 need to be opened when using the default configuration; use rpcinfo -p to examine the exact ports in use on your server. To configure this for iptables, edit /etc/iptables/iptables.rules to include the following lines:

-A INPUT -p tcp -m tcp --dport 111 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2049 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 20048 -j ACCEPT
-A INPUT -p udp -m udp --dport 111 -j ACCEPT
-A INPUT -p udp -m udp --dport 2049 -j ACCEPT
-A INPUT -p udp -m udp --dport 20048 -j ACCEPT

If using NFSv3 and the above listed static ports for rpc.statd and lockd these also need to be added to the configuration:

-A INPUT -p tcp -m tcp --dport 32765 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 32803 -j ACCEPT
-A INPUT -p udp -m udp --dport 32765 -j ACCEPT
-A INPUT -p udp -m udp --dport 32803 -j ACCEPT

To apply changes, restart iptables service.


Start rpcbind.service and using systemd. It is recommended to also enable the target to allow systemd to start it at boot time. Note that this unit requires other services, which are launched automatically by systemd.

Mounting from Linux

Show the server's exported file systems:

$ showmount -e servername

Then mount omitting the server's NFS export root:

# mount -t nfs servername:/music /mountpoint/on/client

If mount fails try including the server's export root (required for Debian/RHEL/SLES, some distributions need -t nfs4 instead of -t nfs):

# mount -t nfs servername:/full/path/to/music /mountpoint/on/client
Note: Server name needs to be a valid hostname (not just IP address). Otherwise mounting of remote share will hang.
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.

servername:/music   /mountpoint/on/client   nfs4   rsize=8192,wsize=8192,timeo=14,_netdev	0 0
Note: Consult the NFS and mount man pages for more mount options.

Some additional mount options to consider are include:

rsize and wsize
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.
The timeo value is the amount of time, in tenths of a second, to wait before resending a transmission after an RPC timeout. 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 performance can be achieved by increasing this timeout value.
The _netdev option tells the system to wait until the network is up before trying to mount the share. systemd assumes this for NFS, but anyway it is good practice to use it for all types of networked file systems
Use minorversion=1 for mounting a Windows Server 2012 (Essentials) share with NFS version 4.1
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.
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:

servername :/home   /mountpoint/on/client  nfs  users,noauto,x-systemd.automount,x-systemd.device-timeout=10,timeo=14,soft,intr,noatime 0 0
Tip: noauto above will not mount the NFS share until it is accessed: use auto for it to be available immediately.
If you have any issues with the mount failing due to the network not being up/available, enable NetworkManager-wait-online.service: this will ensure that has all the links available prior to being active.
Note: If you try to automount a NFS-share via systemd which is mounted the same way on the server, the system and all applications who try to use the share may freeze when handling larger amounts of data.
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.

Mounting from Windows

Note: Only the Ultimate and Enterprise editions of Windows 7 and the Enterprise edition of Windows 8 include "Client for NFS".

NFS shares can be mounted from Windows if the "Client for NFS" service is activated (which it is not by default). To install the service go to "Programs and features" in the Control Panel and click on "Turn Windows features on or off". Locate "Services for NFS" and activate it as well as both subservices ("Administrative tools" and "Client for NFS").

Some global options can be set by opening the "Services for Network File System" (locate it with the search box) and right click on client > properties.

Warning: Serious performance issues may occur (it randomly takes 30-60 seconds to display a folder, 2 MB/s file copy speed on gigabit LAN, ...) to which Microsoft does not have a solution yet.[1]

To mount a share using Explorer:

Computer > Map network drive > servername:/srv/nfs4/music

Mounting from OS X

Note: OS X by default uses an insecure (>1024) port to mount a share.

Either export the share with the insecure flag, and mount using Finder:

Go > Connect to Server > nfs://servername/

Or, mount the share using a secure port using the terminal:

# mount -t nfs -o resvport,nolocks,locallocks servername:/srv/nfs4 /Volumes/servername

See for an explanation on why to use the nolocks and locallocks options.

Tips and tricks

Performance tuning

In order to get the most out of NFS, it is necessary to tune the rsize and wsize mount options to meet the requirements of the network configuration.

Automatic mount handling

This trick is useful for laptops that require nfs shares from a local wireless network. If the nfs host becomes unreachable, the nfs share will be unmounted to hopefully prevent system hangs when using the hard mount option. See

Make sure that the NFS mount points are correctly indicated in /etc/fstab:

$ cat /etc/fstab
lithium:/mnt/data           /mnt/data	        nfs noauto,noatime,rsize=32768,wsize=32768,intr,soft 0 0
lithium:/var/cache/pacman   /var/cache/pacman	nfs noauto,noatime,rsize=32768,wsize=32768,intr,soft 0 0

The noauto mount option tells systemd not to automatically mount the shares at boot. systemd would otherwise attempt to mount the nfs shares that may or may not exist on the network causing the boot process to appear to stall on a blank screen.

In order to mount NFS share by non-root user user may be required to be added to fstab entry.

Create the auto_share script that will be used by cron to check if the NFS host is reachable,



MOUNT_POINTS=$(sed -e '/^.*#/d' -e '/^.*:/!d' -e 's/\t/ /g' /etc/fstab | tr -s " " | cut -f2 -d" ")

ping -c 1 "${SERVER}" &>/dev/null

if [ $? -ne 0 ]; then
    # The server could not be reached, unmount the shares
    for umntpnt in ${MOUNT_POINTS}; do
        umount -l -f $umntpnt &>/dev/null
    # The server is up, make sure the shares are mounted
    for mntpnt in ${MOUNT_POINTS}; do
        mountpoint -q $mntpnt || mount $mntpnt
# chmod +x /root/bin/auto_share

Create the root cron entry to run auto_share every minute:

# crontab -e
* * * * * /root/bin/auto_share

A systemd unit file can also be used to mount the NFS shares at startup. The unit file is not necessary if NetworkManager is installed and configured on the client system. See #NetworkManager dispatcher.

Description=NFS automount



Now enable auto_share.

NetworkManager dispatcher

In addition to the method described previously, NetworkManager can also be configured to run a script on network status change.

Enable and start the NetworkManager-dispatcher service.

The easiest method for mount shares on network status change is to just symlink to the auto_share script:

# ln -s /root/bin/auto_share /etc/NetworkManager/dispatcher.d/

Or use the following mounting script that checks for network availability:



MOUNT_POINTS=$(sed -e '/^.*#/d' -e '/^.*:/!d' -e 's/\t/ /g' /etc/fstab | tr -s " " | cut -f2 -d" ")

ISNETUP=$(nmcli dev wifi | \grep $SSID | tr -s ' ' | cut -f 10 -d ' ') 2>/dev/null

# echo "$ISNETUP" >> /tmp/nm_dispatch_log

if [[ "$ISNETUP" == "yes" ]]; then
    for mntpnt in ${MOUNT_POINTS}; do
        mountpoint -q $mntpnt || mount $mntpnt
    for srvexp in ${MOUNT_POINTS}; do
        umount -l -f $srvexp &>/dev/null

Now when the wireless SSID "CHANGE_ME" goes up or down, the script will be called to mount or unmount the shares as soon as possible.


There is a dedicated article NFS Troubleshooting.

See also