NFS shares on Ubuntu

As part of my NAS migration from TrueNAS to Ubuntu, I needed to set up some NFS shares on Ubuntu Server 20.04.

Pretty painless, but I need the option to connect from my iMac, there were some additional hoops to jump through, to get everything to work correctly.

Steps

  1. Install the necessary packages:

    sudo apt install nfs-kernel-server -y
    
  2. Create directory to share, unless the directory alread exists:

    sudo mkdir /mnt/nfsdir2share
    
  3. NFS will translate any root operations on the client to the nobody:nogroup credentials as a security measure, so change the directory ownership to match those credentials.

    sudo chown nobody:nogroup /mnt/nfsdir2share
    
  4. The /etc/exports file follows this configuration structure:

    directory_to_share client(share_option1,...,share_optionN)

    Edit the /etc/exports file, adding this line, to allow users from the 192.168.1.x subnet access:

    /mnt/nfsdir2share	192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash,all_squash)
    

    NFS options explained in detail:

    rw: This option gives the client computer both read and write access to the volume.

    sync This option forces NFS to write changes to disk before replying. This results in a more stable and consistent environment since the reply reflects the actual state of the remote volume. However, there is a performance hit, but worth the data integrity.

    no_subtree_check: This option prevents subtree checking, which is a process where the host must check whether the file is actually still available in the exported tree for every request. This can cause many problems when a file is renamed while the client has it opened. In almost all cases, it is better to disable subtree checking.

    no_root_squash: By default, any file request made by user root on the client machine is treated as if it is made by user nobody on the server. This was intended as security feature to prevent a root account on the client from using the file system of the host as root. You should not enable this option without a good reason.

    auto_squash: This option will map all User IDs (UIDs) and group IDs (GIDs) to the anonymous user. If you plan on mounting the NFS share(s) on OSX, you'll want to use this option, otherwise you would need to sudo to create/modify any files on the share.

  5. Restart the NFS server:

    sudo systemctl restart nfs-kernel-server
    
  6. Check NFS server status:

    sudo systemctl status nfs-kernel-server
    ● nfs-server.service - NFS server and services
         Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
         Active: active (exited) since Mon 2021-08-30 11:17:06 MDT; 2s ago
    
  7. Verify the NFS shares:

    sudo showmount -e 192.168.1.204
    Export list for 192.168.1.204:
    /mnt/nfsdir2share 192.168.1.0/24
    

OSX mounting issue

Mounting the NFS share in Linux worked without a problem, but when I tried to mount the NFS share on my iMac, I ran into an error:

sudo mount -t nfs 192.168.1.204:/mnt/nfsdir2share /Users/tom/test123
mount_nfs: can't mount with remote locks when server (192.168.1.204) is not running rpc.statd: RPC prog. not avail
mount: /Users/tom/test123 failed with 74

I found out that the rpc-statd service was dead on the server, so it needed to be configured to start on bootup and started now:

sudo systemctl status rpc-statd
● rpc-statd.service - NFS status monitor for NFSv2/3 locking.
     Loaded: loaded (/lib/systemd/system/rpc-statd.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
sudo systemctl enable --now rpc-statd
sudo systemctl status rpc-statd
● rpc-statd.service - NFS status monitor for NFSv2/3 locking.
     Loaded: loaded (/lib/systemd/system/rpc-statd.service; disabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-08-30 11:30:25 MDT; 2s ago

Once the rpc-statd service was running, I was able to mount the NFS share:

sudo mount -t nfs -o resvport,rw 192.168.1.204:/mnt/nfsdir2share /Users/tom/test123

Verify the NFS share is mounted:

mount | grep nfsdir2share
192.168.1.204:/mnt/nfsdir2share on /Users/tom/test123 (nfs)

References

How To Set Up an NFS Mount on Ubuntu 20.04 https://www.digitalocean.com/community/tutorials/how-to-set-up-an-nfs-mount-on-ubuntu-20-04

nfs(5) - Linux manual page https://man7.org/linux/man-pages/man5/nfs.5.html

Red Hat Enterprise Linux - Storage Administration Guide https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/storage_administration_guide/ch-nfs

Linux NFS - HowTo http://nfs.sourceforge.net/nfs-howto/ar01s03.html

NFS mount options https://www.golinuxcloud.com/unix-linux-nfs-mount-options-example/#Understanding_all_quash_vs_no_all_squash