:''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.''

+

: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.

−

==Installing==

+

{{Note|

−

Both client and server only require the [[Pacman|installation]] of the {{Pkg|nfs-utils}} package.

+

*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 [[group]] ID's are the same on both the client and server. It is however possible to overrule the UID/GID by using {{ic|anonuid}}/{{ic|anongid}} with {{ic|all_squash}} in {{ic|/etc/exports}}.

+

}}

−

{{Note|It is HIGHLY recommended to use a time sync daemon on ALL nodes of your network to keep client/server clocks in sync. Without accurate clocks on all nodes, NFS can introduce unwanted delays! The [[NTP]] system is recommended to sync both the server and the clients to the highly accurate NTP servers available on the Internet.}}

+

== Installation ==

+

+

Both client and server only require the [[install|installation]] of the {{Pkg|nfs-utils}} package.

+

+

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

==Configuration==

==Configuration==

===Server===

===Server===

−

==== ID mapping ====

+

The NFS server needs a list of exports (shared directories) which are defined in {{ic|/etc/exports}}. NFS shares defined in {{ic|/etc/exports}} are relative to the so-called NFS root. A good security practice is to define an NFS root in a discrete directory tree under the server's root file system 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.

−

Edit {{ic|/etc/idmapd.conf}} and set the {{ic|Domain}} field to your domain name.

+

+

Consider this following example wherein:

+

# The NFS root is {{ic|/srv/nfs}}.

+

# The export is {{ic|/srv/nfs/music}} via a bind mount to the actual target {{ic|/mnt/music}}.

+

+

# mkdir -p /srv/nfs/music /mnt/music

+

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

+

+

{{Note|[[ZFS]] filesystems require special handling of bindmounts, see [[ZFS#Bind mount]].}}

+

+

To make it stick across reboots, add the bind mount to {{ic|fstab}}:

+

+

{{hc|/etc/fstab|

+

/mnt/music /srv/nfs/music none bind 0 0

+

}}

+

+

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 {{ic|/etc/exports}}:

It should be noted that modifying {{ic|/etc/exports}} while the server is running will require a re-export for changes to take effect:

+

# exportfs -rav

+

+

To view the current loaded exports state in more detail, use:

+

# exportfs -v

+

+

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

+

{{Tip|[http://ip2cidr.com/ ip2cidr] is a tool to convert an IP ranges to correctly structured CDIR specification.}}

+

{{Note|If the target export is a tmpfs filesystem, the {{ic|1=fsid=1}} option is required.}}

+

+

==== Starting the server ====

+

+

[[Start]] and [[enable]] {{ic|nfs-server.service}}.

−

{{hc|/etc/idmapd.conf|<nowiki>

+

==== Miscellaneous ====

−

[General]

−

−

Verbosity = 1

−

Pipefs-Directory = /var/lib/nfs/rpc_pipefs

−

Domain = atomic

−

[Mapping]

+

===== Optional configuration =====

−

Nobody-User = nobody

+

Advanced configuration options can be set in {{ic|/etc/nfs.conf}}. Users setting up a simple configuration may not need to edit this file.

−

Nobody-Group = nobody

+

+

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

+

+

===== Restricting NFS to interfaces/IPs =====

+

+

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

+

+

{{hc|/etc/nfs.conf|2=

+

[nfsd]

+

host=192.168.1.123

+

# Alternatively, you can use your hostname.

+

# host=myhostname

+

}}

+

+

Restarting the service will apply the changes immediately.

+

+

# systemctl restart nfs-server.service

+

+

===== 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 {{ic|user@domain}}. The process of translating from UID to string and string to UID is referred to as ''ID mapping'' [http://man7.org/linux/man-pages/man5/nfsidmap.5.html].

+

+

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

+

+

# echo "N" | tee /sys/module/nfsd/parameters/nfs4_disable_idmapping

+

+

Set as [[Kernel modules#Setting module options|module option]] to make this change permanent, e.g.:

+

+

{{hc|/etc/modprobe.d/nfsd.conf|<nowiki>

+

options nfsd nfs4_disable_idmapping=0

</nowiki>}}

</nowiki>}}

−

==== 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 {{ic|/etc/exports}} which are relative to the NFS root. In this example, the NFS root will be {{ic|/srv/nfs4}} and we will be sharing {{ic|/mnt/music}}.

+

To fully use ''idmapping'', make sure the domain is configured in {{ic|/etc/idmapd.conf}} on '''both''' the server and the client.

+

+

===== Static ports for NFSv3 =====

+

{{Out of date|Configuration should be done in /etc/nfs.conf since {{Pkg|nfs-utils}} 2.1.1.[https://sourceforge.net/projects/nfs/files/nfs-utils/2.1.1/]}}

+

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

+

+

{{hc|/etc/sysconfig/nfs|2=

+

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

+

}}

+

+

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

−

{{bc|# mkdir -p /srv/nfs4/music}}

+

{{hc|/etc/sysconfig/nfs|2=

+

RPCMOUNTDARGS="-p 20048"

+

}}

−

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

+

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

−

Now mount the actual target share, {{ic|/mnt/music}} to the NFS share via the mount command:

+

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

−

{{bc|# mount --bind /mnt/music /srv/nfs4/music}}

+

===== NFSv2 compatibility =====

+

{{Out of date|Configuration should be done in /etc/nfs.conf since {{Pkg|nfs-utils}} 2.1.1.[https://sourceforge.net/projects/nfs/files/nfs-utils/2.1.1/]}}

+

Users needing to support clients using NFSv2 (for example U-Boot), should set {{ic|1=RPCNFSDARGS="-V 2"}} in {{ic|/etc/sysconfig/nfs}}.

−

To make it stick across server reboots, add the bind mount to {{ic|fstab}}:

+

===== Firewall configuration =====

−

{{hc|/etc/fstab|

+

−

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

+

To enable access through a [[firewall]], TCP and UDP ports {{ic|111}}, {{ic|2049}}, and {{ic|20048}} may need to be opened when using the default configuration; use {{ic|rpcinfo -p}} to examine the exact ports in use on the server:

+

+

{{hc|<nowiki>$ rpcinfo -p | grep nfs</nowiki>|

+

100003 3 tcp 2049 nfs

+

100003 4 tcp 2049 nfs

+

100227 3 tcp 2049 nfs_acl

}}

}}

−

==== Exports ====

+

When using NFSv4, make sure TCP port {{ic|2049}} is open. No other port opening should be required:

−

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

+

−

{{hc|/etc/exports|<nowiki>

+

{{hc|/etc/iptables/iptables.rules|<nowiki>

−

/srv/nfs4/ 192.168.0.1/24(rw,fsid=root,no_subtree_check)

+

-A INPUT -p tcp -m tcp --dport 2049 -j ACCEPT

−

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

+

</nowiki>}}

+

+

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 {{ic|/etc/iptables/iptables.rules}} to include the following lines:

+

+

{{hc|/etc/iptables/iptables.rules|<nowiki>

+

-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

+

</nowiki>}}

+

+

The previous commands can be saved by executing:

+

+

# iptables-save > /etc/iptables/iptables.rules

+

+

{{Warning|This command will '''override''' the current iptables start configuration with the current iptables configuration!}}

+

+

If using NFSv3 and the above listed static ports for {{ic|rpc.statd}} and {{ic|lockd}} the following ports may also need to be added to the configuration:

+

+

{{hc|/etc/iptables/iptables.rules|<nowiki>

+

-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

</nowiki>}}

</nowiki>}}

−

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

+

To apply changes, [[Restart]] {{ic|iptables.service}}.

+

+

=== Client ===

+

Users intending to use NFS4 with [[Kerberos]], also need to [[start]] and [[enable]] {{ic|nfs-client.target}}, which starts {{ic|rpc-gssd.service}}. However, due to bug {{Bug|50663}} in glibc, {{ic|rpc-gssd.service}} currently fails to start. Adding the "-f" (foreground) flag in the service is a workaround:

−

For more information about all available options see {{ic|man 5 exports}}.

+

{{hc|# systemctl edit rpc-gssd.service|2=

+

[Unit]

+

Requires=network-online.target

+

After=network-online.target

−

If you modify {{ic|/etc/exports}} while the server is running, you must re-export them for changes to take effect:

+

[Service]

−

{{bc|# exportfs -ra}}

+

Type=simple

+

ExecStart=

+

ExecStart=/usr/sbin/rpc.gssd -f

+

}}

−

====Starting the server====

+

==== Manual mounting ====

−

[[Daemons|Start/enable]] {{ic|rpc-idmapd.service}} and {{ic|rpc-mountd.service}}. Note that these units require other services, which are launched automatically by [[systemd]].

+

For NFSv3 use this command to show the server's exported file systems:

−

===Client===

+

$ showmount -e servername

−

Clients need {{Pkg|nfs-utils}} to connect, but no special setup is required when connecting to NFS 4 servers.

−

====Mounting from Linux====

+

For NFSv4 mount the root NFS directory and look around for available mounts:

{{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 {{ic|/etc/fstab}} file, and add an appropriate line reflecting the setup. Again, the server's NFS export root is omitted.

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

{{Note|Additional mount options can be specified here. Consult the NFS man page for further information.}}

+

{{Note|Consult {{man|5|nfs}} and {{man|8|mount}} for more mount options.}}

−

Some additional mount options to consider are include:

−

* {{ic|1=rsize=8192}} and {{ic|1=wsize=8192}}

+

Some additional mount options to consider:

−

* {{ic|1=timeo=14}}

−

* {{ic|1=intr}}

−

* {{ic|1=_netdev}}

−

The {{ic|rsize}} value is the number of bytes used when reading from the server. The {{ic|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.

+

; rsize and wsize: The {{ic|rsize}} value is the number of bytes used when reading from the server. The {{ic|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 {{ic|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.

+

; 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 {{ic|hard}} option is specified), NFS requests are retried indefinitely. If the {{ic|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.

−

The {{ic|intr}} option allows signals to interrupt the file operation if a major timeout occurs for a hard-mounted share.

+

; timeo: The {{ic|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.

−

The {{ic|_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's good practice to use it for all types of networked filesystems

+

; retrans: The number of times the NFS client retries a request before it attempts further recovery action. If the {{ic|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).

−

=====Using autofs=====

+

; _netdev: The {{ic|_netdev}} option tells the system to wait until the network is up before trying to mount the share - [[systemd]] assumes this for NFS, although [[#Mount using .2Fetc.2Ffstab with systemd|automount]] may be a more preferred solution.

−

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|Setting the sixth field ({{ic|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.}}

−

{{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.

+

==== Mount using /etc/fstab with systemd ====

−

{{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.[https://social.technet.microsoft.com/Forums/en-CA/w7itpronetworking/thread/40cc01e3-65e4-4bb6-855e-cef1364a60ac]}}

+

Another method is using the systemd {{ic|automount}} service. This is a better option than {{ic|_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 [[Systemd#Using_units|reloading]] systemd and restarting {{ic|''mountpoint-on-client''.automount}} to reload the {{ic|/etc/fstab}} configuration.

+

+

{{Tip|

+

* The {{ic|noauto}} mount option will not mount the NFS share until it is accessed: use {{ic|auto}} for it to be available immediately. <br> If experiencing any issues with the mount failing due to the network not being up/available, [[enable]] {{ic|NetworkManager-wait-online.service}}. It will ensure that {{ic|network.target}} has all the links available prior to being active.

+

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

+

* The {{ic|<nowiki>x-systemd.idle-timeout=1min</nowiki>}} 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, [[enable]] {{ic|NetworkManager-wait-online.service}} to ensure that NetworkManager is not exited before the NFS volumes are unmounted. You may also try to add the {{ic|<nowiki>x-systemd.requires=network-online.target</nowiki>}} mount option if shutdown takes too long.

+

* Using mount options as {{ic|noatime}}, {{ic|nodiratime}}, {{ic|noac}}, {{ic|nocto}} may be used to increase NFS performance.}}

+

+

{{Note|Users trying to automount a NFS-share via systemd which is mounted the same way on the server may experience a freeze when handling larger amounts of data.}}

+

+

==== 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.

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

−

{{note|OS X by default uses an insecure (>1024) port to mount a share.}}

−

Either export the share with the {{ic|insecure}} flag, and mount using Finder:

−

{{ic|Go}} > {{ic|Connect to Server}} > {{ic|nfs://servername/}}

+

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 {{ic|rsize}} and {{ic|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 {{ic|/proc/fs/nfsd/max_block_size}} before starting ''nfsd''. For example, the following command restores the previous default iosize of 32k:

Users making use of systemd-networkd might notice nfs mounts the fstab are not mounted when booting; errors like the following are common:

+

+

mount[311]: mount.nfs4: Network is unreachable

+

+

The solution is simple; force systemd to wait for the network to be completely configured by [[enabling]] {{ic|systemd-networkd-wait-online.service}}. In theory this slows down the boot-process because less services run in parallel.

+

+

=== 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 https://bbs.archlinux.org/viewtopic.php?pid=1260240#p1260240

+

+

Make sure that the NFS mount points are correctly indicated in {{ic|/etc/fstab}}:

{{Note|You must use hostnames in {{ic|/etc/fstab}} for this to work, not IP addresses.}}

+

+

The {{ic|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 shares with non-root users the {{ic|user}} option has to be added.

+

+

Create the {{ic|auto_share}} script that will be used by ''cron'' or ''systemd/Timers'' to use ICMP ping to check if the NFS host is reachable:

{{Note|If you want to test using a TCP probe instead of ICMP ping (default is tcp port 2049 in NFS4) then replace the line:

+

+

# Check if the server is reachable

+

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

+

+

with:

+

+

# Check if the server is reachable

+

timeout 1 bash -c ": < /dev/tcp/${SERVER}/2049"

+

+

in the {{ic|auto_share}} script above.}}

+

+

# chmod +x /usr/local/bin/auto_share

+

+

Create a cron entry or a systemd/Timers timer to check every minute if the server of the shares are reachable.

+

+

==== Cron ====

+

+

{{hc|# crontab -e|<nowiki>

+

* * * * * /usr/local/bin/auto_share

+

</nowiki>}}

+

+

==== systemd/Timers ====

+

+

{{hc|# /etc/systemd/system/auto_share.timer|<nowiki>

+

[Unit]

+

Description=Check the network mounts

+

+

[Timer]

+

OnCalendar=*-*-* *:*:00

+

+

[Install]

+

WantedBy=timer.target

+

</nowiki>}}

+

+

{{hc|# /etc/systemd/system/auto_share.service|<nowiki>

+

[Unit]

+

Description=Check the network mounts

+

+

[Service]

+

Type=simple

+

ExecStart=/usr/local/bin/auto_share

+

</nowiki>}}

+

+

# systemctl enable auto_share.timer

+

+

==== Mount at startup via systemd ====

+

+

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]].

+

+

{{hc|/etc/systemd/system/auto_share.service|<nowiki>

+

[Unit]

+

Description=NFS automount

+

After=syslog.target network.target

+

+

[Service]

+

Type=oneshot

+

RemainAfterExit=yes

+

ExecStart=/usr/local/bin/auto_share

+

+

[Install]

+

WantedBy=multi-user.target

+

</nowiki>}}

+

+

Now [[enable]] the {{ic|auto_share.service}}.

+

+

==== NetworkManager dispatcher ====

+

+

In addition to the method described previously, [[NetworkManager#Network_services_with_NetworkManager_dispatcher|NetworkManager]] can also be configured to run a script on network status change: [[Enable]] and [[start]] the {{ic|NetworkManager-dispatcher.service}}.

+

+

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

Latest revision as of 09:39, 14 February 2018

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 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 sync daemon to keep client/server clocks in sync. Without accurate clocks on all nodes, NFS can introduce unwanted delays.

Configuration

Server

The NFS server needs a list of exports (shared directories) which are defined in /etc/exports. NFS shares defined in /etc/exports are relative to the so-called NFS root. A good security practice is to define an NFS root in a discrete directory tree under the server's root file system 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.

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.

Client

Users intending to use NFS4 with Kerberos, also need to start and enablenfs-client.target, which starts rpc-gssd.service. However, due to bug FS#50663 in glibc, rpc-gssd.service currently fails to start. Adding the "-f" (foreground) flag in the service is a workaround:

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.

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.

Note: Users trying to automount a NFS-share via systemd which is mounted the same way on the server may experience a freeze when handling larger amounts of data.

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

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.

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:

Automounting shares with systemd-networkd

Users making use of systemd-networkd might notice nfs mounts the fstab are not mounted when booting; errors like the following are common:

mount[311]: mount.nfs4: Network is unreachable

The solution is simple; force systemd to wait for the network to be completely configured by enablingsystemd-networkd-wait-online.service. In theory this slows down the boot-process because less services run in parallel.

Note: You must use hostnames in /etc/fstab for this to work, not IP addresses.

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 shares with non-root users the user option has to be added.

Create the auto_share script that will be used by cron or systemd/Timers to use ICMP ping to check if the NFS host is reachable:

Mount at startup via systemd

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.