NFS
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.
- NFS is not encrypted. Tunnel NFS through an encrypted protocol like Kerberos or (secure) VPN when dealing with sensitive data.
- Unlike Samba, NFS does not have any user authentication by default, client access is restricted by their IP-address/hostname.
- NFS expects the user and/or user group IDs are the same on both the client and server. Enable NFSv4 idmapping or overrule the UID/GID manually by using
anonuid
/anongid
together withall_squash
in/etc/exports
. - NFS does not support POSIX ACLs.
Installation
Both client and server only require the installation of the nfs-utils package.
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 (see exports(5) for details) 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 .
# mkdir -p /srv/nfs/music /mnt/music # mount --bind /mnt/music /srv/nfs/music
To make the bind mount persistent across reboots, add it to fstab:
Add directories to be shared and limit them to a range of addresses via a CIDR or hostname(s) of client machines that will be allowed to mount them in /etc/exports
, e.g.:
fsid=0
, other directories must be below it. The rootdir
option in the /etc/nfs.conf
file has no effect on this.- The
crossmnt
option makes it possible for clients to access all filesystems mounted on a filesystem marked withcrossmnt
and clients will not be required to mount every child export separately. Note this may not be desirable if a child is shared with a different range of addresses. - Instead of
crossmnt
, one can also use thenohide
option on child exports so that they can be automatically mounted when a client mounts the root export. Being different fromcrossmnt
,nohide
still respects address ranges of child exports. - Use an asterisk (
*
) to allow access from any interface.
It should be noted that modifying /etc/exports
while the server is running will require a re-export for changes to take effect:
# exportfs -arv
To view the current loaded exports state in more detail, use:
# exportfs -v
For more information about all available options see exports(5).
fsid=1
option is required.Restricting NFS to interfaces/IPs
By default, starting 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.
Restart to apply the changes immediately.
Firewall configuration
To enable access through a firewall, TCP and UDP ports , , and may need to be opened when using the default configuration; use rpcinfo -p
to examine the exact ports in use on the server:
When using NFSv4, make sure TCP port is open. No other port opening should be required:
When using an older NFS version, make sure other ports are open:
# iptables -A INPUT -p tcp -m tcp --dport 111 -j ACCEPT # iptables -A INPUT -p tcp -m tcp --dport 2049 -j ACCEPT # iptables -A INPUT -p tcp -m tcp --dport 20048 -j ACCEPT # iptables -A INPUT -p udp -m udp --dport 111 -j ACCEPT # iptables -A INPUT -p udp -m udp --dport 2049 -j ACCEPT # iptables -A INPUT -p udp -m udp --dport 20048 -j ACCEPT
To have this configuration load on every system start, edit to include the following lines:
/etc/iptables/iptables.rules
-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
The previous commands can be saved by executing:
# iptables-save > /etc/iptables/iptables.rules
If using NFSv3 and the above listed static ports for and the following ports may also need to be added to the configuration:
To apply changes, Restart .
Enabling NFSv4 idmapping
- NFSv4 idmapping does not work with the default
sec=sys
mount option. [dead link 2022-09-22 ⓘ] - NFSv4 idmapping needs to be enabled on both the client and server.
- Another option is to make sure the user and group IDs (UID and GID) match on both the client and server.
- Enabling/starting
nfs-idmapd.service
should not be needed as it has been replaced with a new id mapper:
# dmesg | grep id_resolver
[ 3238.356001] NFS: Registering the id_resolver key type [ 3238.356009] Key type id_resolver registered
The NFSv4 protocol represents the local system's UID and GID values on the wire as strings of the form . The process of translating from UID to string and string to UID is referred to as ID mapping. See for details.
Even though idmapd may be running, it may not be fully enabled. If or returns Y
on a client/server, enable it by:
On the client:
# echo N > /sys/module/nfs/parameters/nfs4_disable_idmapping
On the server:
# echo N > /sys/module/nfsd/parameters/nfs4_disable_idmapping
Set as module option to make this change permanent, i.e.:
To fully use idmapping, make sure the domain is configured in on both the server and the client:
Manual mounting
For NFSv3 use this command to show the server's exported file systems:
$ showmount -e servername
For NFSv4 mount the root NFS directory and look around for available mounts:
# mount servername:/ /mountpoint/on/client
Then mount omitting the server's NFS export root:
# mount -t nfs -o vers=4 servername:/music /mountpoint/on/client
If mount fails try including the server's export root (required for Debian/RHEL/SLES, some distributions need instead of ):
# mount -t nfs -o vers=4 servername:/srv/nfs/music /mountpoint/on/client
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 file, and add an appropriate line reflecting the setup. Again, the server's NFS export root is omitted.
Some additional mount options to consider:
- rsize and wsize
- The 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. By default, if these options are not specified, the client and server negotiate the largest values they can both support (see for details). After changing these values, it is recommended to test the performance (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 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.
- timeo
- The 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 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 option tells the system to wait until the network is up before trying to mount the share - systemd assumes this for NFS.
Mount using /etc/fstab with systemd
Another method is using the x-systemd.automount option which mounts the filesystem upon access:
To make systemd aware of the changes to fstab, reload systemd and restart .
As systemd unit
Create a new file inside , e.g. . See for details.
mnt-home.mount
can only be used if you are going to mount the share under /mnt/home
. Otherwise the following error might occur: systemd[1]: mnt-home.mount: Where= setting does not match unit name. Refusing.
. If the mountpoint contains non-ASCII characters, use systemd-escape).path to share
path to mount the share
share mounting options
- Network mount units automatically acquire
After
dependencies onremote-fs-pre.target
,network.target
andnetwork-online.target
, and gain aBefore
dependency onremote-fs.target
unlessnofail
mount option is set. Towards the latter aWants
unit is added as well. - Append
noauto
toOptions
preventing automatically mount during boot (unless it is pulled in by some other unit). - If you want to use a hostname for the server you want to share (instead of an IP address), add
nss-lookup.target
toAfter
. This might avoid mount errors at boot time that do not arise when testing the unit.
To use , start the unit and enable it to run on system boot.
automount
To automatically mount a share, one may use the following automount unit:
Disable/stop the unit, and enable/start to automount the share when the mount path is being accessed.
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 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 and wsize
. See https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/5/html/5.8_technical_notes/known_issues-kernel
It is possible to change the default max block size allowed by the server by writing to the before starting nfsd. For example, the following command restores the previous default iosize of 32k:
# echo 32768 > /proc/fs/nfsd/max_block_size
To make the change permanent, create a systemd-tmpfile:
/etc/tmpfiles.d/nfsd-block-size.conf
w /proc/fs/nfsd/max_block_size - - - - 32768
To mount with the increased and wsize
mount options:
# mount -t nfs -o rsize=32768,wsize=32768,vers=4 servername:/srv/nfs/music /mountpoint/on/client
Furthermore, despite the violation of NFS protocol, setting instead of or may potentially achieve a significant performance gain especially on spinning disks. Configure exports with this option and then execute to apply.
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 mount option .
Make sure that the NFS mount points are correctly indicated in fstab:
Create the script that will be used by cron or systemd/Timers to use ICMP ping to check if the NFS host is reachable:
/usr/local/bin/auto_share
#!/bin/bash function net_umount { umount -l -f $1 &>/dev/null } function net_mount { mountpoint -q $1 || mount $1 } NET_MOUNTS=$(sed -e '/^.*#/d' -e '/^.*:/!d' -e 's/\t/ /g' /etc/fstab | tr -s " ")
Make sure the script is executable.
Next check configure the script to run every X, in the examples below this is every minute.
Using a NetworkManager dispatcher
NetworkManager can also be configured to run a script on network status change.
The easiest method for mount shares on network status change is to symlink the script:
# ln -s /usr/local/bin/auto_share /etc/NetworkManager/dispatcher.d/30-nfs.sh
However, in that particular case unmounting will happen only after the network connection has already been disabled, which is unclean and may result in effects like freezing of KDE Plasma applets.
The following script safely unmounts the NFS shares before the relevant network connection is disabled by listening for the , and events, make sure the script is executable:
noauto
option, remove this mount option or use auto
to allow the dispatcher to manage these mounts.Create a symlink inside to catch the events:
# ln -s /etc/NetworkManager/dispatcher.d/30-nfs.sh /etc/NetworkManager/dispatcher.d/pre-down.d/30-nfs.sh
Troubleshooting
There is a dedicated article NFS/Troubleshooting.
See also
- See also Avahi, a Zeroconf implementation which allows automatic discovery of NFS shares.
- HOWTO: Diskless network boot NFS root
- Microsoft Services for Unix NFS Client info
- NFS on Snow Leopard
- http://chschneider.eu/linux/server/nfs.shtml
- How to do Linux NFS Performance Tuning and Optimization
- Linux: Tune NFS Performance