NFS Documentation

NFS

Introduction

Network File System (NFS) allows a system to share directories and files with others over a network. Users and programs can access files on remote systems almost as if they were local files. MidnightBSD includes NFS support derived from FreeBSD 13.x, supporting NFSv3 and NFSv4.

NFS is commonly used to share home directories across multiple machines, provide centralized storage, or mount read-only media such as installation trees. MidnightBSD supports both the traditional kernel-based NFS server and client as well as integration with ZFS dataset sharing.

NFS Server Setup

To enable the NFS server at boot, add the following lines to /etc/rc.conf:

rpcbind_enable="YES"
nfs_server_enable="YES"
mountd_enable="YES"
mountd_flags="-r"
nfsv4_server_enable="YES"

The rpcbind service is required for NFSv3. If you only intend to use NFSv4, it is optional. The mountd daemon manages mount requests from clients and reads /etc/exports to determine which file systems may be exported.

You can also enable the NFS server status monitor, which helps clients recover after a server reboot:

rpc_statd_enable="YES"
rpc_lockd_enable="YES"

After editing /etc/rc.conf, start the services without rebooting:

service rpcbind start
service nfsd start
service mountd start

Configuring Exports

The /etc/exports file controls which file systems the NFS server exports and to which hosts. Each line specifies a file system path followed by export options and a list of allowed hosts or networks.

A simple export allowing full access from a single host:

/usr/home/shared -alldirs 192.168.1.10

Export a directory read-only to an entire network:

/usr/local/share/data -ro -network 192.168.1.0 -mask 255.255.255.0

Export a directory with read-write access to multiple hosts:

/tank/media -alldirs 192.168.1.10 192.168.1.11 192.168.1.12

For NFSv4, you must define an export root using the V4: keyword and specify the NFSv4 root path:

V4: /tank
/tank/home -alldirs 192.168.1.0/24
/tank/media -ro 192.168.1.0/24

Common export options include:

After modifying /etc/exports, reload the export table without restarting the server:

service mountd reload

NFS Client Setup

To enable the NFS client at boot, add the following to /etc/rc.conf:

rpcbind_enable="YES"
nfs_client_enable="YES"

For NFSv4 clients, also add:

nfsv4_client_enable="YES"

To mount an NFS share manually, use the mount command:

mount -t nfs 192.168.1.100:/tank/home /mnt/home

To mount an NFSv4 share explicitly:

mount -t nfs -o nfsv4 192.168.1.100:/tank/home /mnt/home

To mount NFS shares automatically at boot, add entries to /etc/fstab:

192.168.1.100:/tank/home /mnt/home nfs rw,noatime 0 0

A read-only fstab entry:

192.168.1.100:/usr/local/share/data /mnt/data nfs ro,noatime 0 0

Common mount options for NFS clients include:

To start NFS client services without rebooting:

service rpcbind start
service nfsclient start

ZFS NFS Properties

ZFS datasets can be shared over NFS directly using ZFS properties, without manually editing /etc/exports. This approach manages NFS exports alongside the datasets themselves.

First, enable the ZFS sharing service in /etc/rc.conf. The standard NFS server daemons must also be enabled as described in the server setup section above:

zfs_enable="YES"
nfs_server_enable="YES"
mountd_enable="YES"
rpcbind_enable="YES"

To share a ZFS dataset over NFS, set the sharenfs property. A value of on exports the dataset using default options:

zfs set sharenfs=on tank/home

You can pass NFS export options directly as the property value:

zfs set sharenfs="-alldirs -ro -network 192.168.1.0 -mask 255.255.255.0" tank/media

Allow read-write access from a specific host:

zfs set sharenfs="-alldirs 192.168.1.10" tank/projects

To disable NFS sharing on a dataset:

zfs set sharenfs=off tank/home

To view the current sharenfs setting on a dataset:

zfs get sharenfs tank/home

To see all datasets with NFS sharing enabled:

zfs get sharenfs

After setting ZFS NFS properties, share all configured datasets and update the running export table:

zfs share -a

Note that ZFS-managed NFS exports and entries in /etc/exports can coexist. The mountd daemon reads both sources when loading exports.

Troubleshooting

If clients cannot connect, verify that the server's firewall allows traffic on the NFS-related ports. For NFSv4, TCP port 2049 is required. NFSv3 additionally uses rpcbind (port 111) and dynamic ports for mountd and statd.

To fix mountd dynamic port assignments for firewall rules, pin them in /etc/rc.conf:

mountd_flags="-r -p 1234"
rpc_statd_flags="-p 1235"
rpc_lockd_flags="-p 1236"

To see what file systems are currently exported by the server:

showmount -e localhost

To check which clients have active mounts:

showmount -a localhost

On the client side, to list available exports from a server:

showmount -e 192.168.1.100

If an NFS mount hangs, check that rpcbind is running on both the server and client, and that the server's nfsd and mountd processes are active:

service rpcbind status
service nfsd status
service mountd status