A guide to setup an NFS server
Setting up a NFS share shouldn’t take more than 2 minutes, 1 minute to setup the server (aka the machine who provide the service) and another minute to setup the client (aka the machine who consume the service). So let’s dig right in as we have about 2 minutes of work to do.
Setup the server
In debian/ubuntu, you’d need to install the right nfs server package:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
keyutils libevent-core-2.1-7 libnfsidmap1 libyaml-0-2 nfs-common python3-yaml rpcbind
Suggested packages:
open-iscsi watchdog
The following NEW packages will be installed:
keyutils libevent-core-2.1-7 libnfsidmap1 libyaml-0-2 nfs-common nfs-kernel-server python3-yaml
rpcbind
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 872 kB of archives.
After this operation, 3,342 kB of additional disk space will be used.
Do you want to continue? [Y/n]Y
Get:5 http://deb.debian.org/debian bookworm/main amd64 nfs-common amd64 1:2.6.2-4 [260 kB]
Get:6 http://deb.debian.org/debian bookworm/main amd64 libyaml-0-2 amd64 0.2.5-1 [53.6 kB]
Get:7 http://deb.debian.org/debian bookworm/main amd64 nfs-kernel-server amd64 1:2.6.2-4 [151 kB]
Get:8 http://deb.debian.org/debian bookworm/main amd64 python3-yaml amd64 6.0-3+b2 [119 kB]
Fetched 872 kB in 1s (888 kB/s)
Selecting previously unselected package libevent-core-2.1-7:amd64.
(Reading database ... 65893 files and directories currently installed.)
Preparing to unpack .../0-libevent-core-2.1-7_2.1.12-stable-8_amd64.deb ...
Unpacking libevent-core-2.1-7:amd64 (2.1.12-stable-8) ...
Selecting previously unselected package libnfsidmap1:amd64.
Preparing to unpack .../1-libnfsidmap1_1%3a2.6.2-4_amd64.deb ...
Unpacking libnfsidmap1:amd64 (1:2.6.2-4) ...
Selecting previously unselected package rpcbind.
Preparing to unpack .../2-rpcbind_1.2.6-6+b1_amd64.deb ...
Unpacking rpcbind (1.2.6-6+b1) ...
Selecting previously unselected package keyutils.
Preparing to unpack .../3-keyutils_1.6.3-2_amd64.deb ...
Unpacking keyutils (1.6.3-2) ...
Selecting previously unselected package nfs-common.
Preparing to unpack .../4-nfs-common_1%3a2.6.2-4_amd64.deb ...
Unpacking nfs-common (1:2.6.2-4) ...
Selecting previously unselected package libyaml-0-2:amd64.
Preparing to unpack .../5-libyaml-0-2_0.2.5-1_amd64.deb ...
Unpacking libyaml-0-2:amd64 (0.2.5-1) ...
Selecting previously unselected package nfs-kernel-server.
Preparing to unpack .../6-nfs-kernel-server_1%3a2.6.2-4_amd64.deb ...
Unpacking nfs-kernel-server (1:2.6.2-4) ...
Selecting previously unselected package python3-yaml.
Preparing to unpack .../7-python3-yaml_6.0-3+b2_amd64.deb ...
Unpacking python3-yaml (6.0-3+b2) ...
Setting up libnfsidmap1:amd64 (1:2.6.2-4) ...
Setting up libyaml-0-2:amd64 (0.2.5-1) ...
Setting up python3-yaml (6.0-3+b2) ...
Setting up rpcbind (1.2.6-6+b1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/rpcbind.service → /lib/systemd/system/rpcbind.service.
Created symlink /etc/systemd/system/sockets.target.wants/rpcbind.socket → /lib/systemd/system/rpcbind.socket.
Setting up libevent-core-2.1-7:amd64 (2.1.12-stable-8) ...
Setting up keyutils (1.6.3-2) ...
Setting up nfs-common (1:2.6.2-4) ...
Creating config file /etc/idmapd.conf with new version
Creating config file /etc/nfs.conf with new version
Adding system user `statd' (UID 103) ...
Adding new user `statd' (UID 103) with group `nogroup' ...
Not creating home directory `/var/lib/nfs'.
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.
Created symlink /etc/systemd/system/remote-fs.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.
auth-rpcgss-module.service is a disabled or a static unit, not starting it.
nfs-idmapd.service is a disabled or a static unit, not starting it.
nfs-utils.service is a disabled or a static unit, not starting it.
proc-fs-nfsd.mount is a disabled or a static unit, not starting it.
rpc-gssd.service is a disabled or a static unit, not starting it.
rpc-statd-notify.service is a disabled or a static unit, not starting it.
rpc-statd.service is a disabled or a static unit, not starting it.
rpc-svcgssd.service is a disabled or a static unit, not starting it.
rpc_pipefs.target is a disabled or a static unit, not starting it.
var-lib-nfs-rpc_pipefs.mount is a disabled or a static unit, not starting it.
Setting up nfs-kernel-server (1:2.6.2-4) ...
Created symlink /etc/systemd/system/nfs-client.target.wants/nfs-blkmap.service → /lib/systemd/system/nfs-blkmap.service.
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /lib/systemd/system/nfs-server.service.
nfs-mountd.service is a disabled or a static unit, not starting it.
nfsdcld.service is a disabled or a static unit, not starting it.
Creating config file /etc/exports with new version
Creating config file /etc/default/nfs-kernel-server with new version
Processing triggers for man-db (2.11.2-2) ...
Processing triggers for libc-bin (2.36-9+deb12u4) ...
~/$
Then let’s configure our sharesfrom /etc/exports
. For example:
/tmp/ *(rw,sync,no_subtree_check)
/home/ 192.168.68.101/24(rw,sync,no_subtree_check)
Each separate line is a separate share and each share has 3 pieces of configuration:
- the folder we want to serve which is the path of our share
- the ip range who is allowed access to the share.
- all sort of options, there’s tons of them and it’s all documented
Once we’ve configured things the way we want it, we need to restart everything:
~/$
Client Configuration
Before we can mount everything up, we first need to install the nfs driver.
My NFS server is available from 192.168.68.108. Let’s make a sanity check to make sure everything is actually running fine:
Trying 192.168.68.108...
Connected to 192.168.68.108.
Escape character is '^]'.
wazaaaa
Connection closed by foreign host.
~/$
Next stop is to install the nfs client:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package nfs-utils
root@tmp:~# apt install nfs-common
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
keyutils libnfsidmap1 rpcbind
Suggested packages:
watchdog
The following NEW packages will be installed:
keyutils libnfsidmap1 nfs-common rpcbind
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 397 kB of archives.
After this operation, 1426 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu lunar/main amd64 libnfsidmap1 amd64 1:2.6.2-4ubuntu1 [46.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu lunar/main amd64 rpcbind amd64 1.2.6-6 [45.8 kB]
Get:3 http://archive.ubuntu.com/ubuntu lunar/main amd64 keyutils amd64 1.6.3-2 [56.4 kB]
Get:4 http://archive.ubuntu.com/ubuntu lunar/main amd64 nfs-common amd64 1:2.6.2-4ubuntu1 [248 kB]
Fetched 397 kB in 2s (162 kB/s)
Selecting previously unselected package libnfsidmap1:amd64.
(Reading database ... 33959 files and directories currently installed.)
Preparing to unpack .../libnfsidmap1_1%3a2.6.2-4ubuntu1_amd64.deb ...
Unpacking libnfsidmap1:amd64 (1:2.6.2-4ubuntu1) ...
Selecting previously unselected package rpcbind.
Preparing to unpack .../rpcbind_1.2.6-6_amd64.deb ...
Unpacking rpcbind (1.2.6-6) ...
Selecting previously unselected package keyutils.
Preparing to unpack .../keyutils_1.6.3-2_amd64.deb ...
Unpacking keyutils (1.6.3-2) ...
Selecting previously unselected package nfs-common.
Preparing to unpack .../nfs-common_1%3a2.6.2-4ubuntu1_amd64.deb ...
Unpacking nfs-common (1:2.6.2-4ubuntu1) ...
Setting up libnfsidmap1:amd64 (1:2.6.2-4ubuntu1) ...
Setting up rpcbind (1.2.6-6) ...
Created symlink /etc/systemd/system/multi-user.target.wants/rpcbind.service → /lib/systemd/system/rpcbind.service.
Created symlink /etc/systemd/system/sockets.target.wants/rpcbind.socket → /lib/systemd/system/rpcbind.socket.
Setting up keyutils (1.6.3-2) ...
Setting up nfs-common (1:2.6.2-4ubuntu1) ...
Creating config file /etc/idmapd.conf with new version
Creating config file /etc/nfs.conf with new version
Adding system user `statd' (UID 110) ...
Adding new user `statd' (UID 110) with group `nogroup' ...
Not creating home directory `/var/lib/nfs'.
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.
Created symlink /etc/systemd/system/remote-fs.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.
auth-rpcgss-module.service is a disabled or a static unit, not starting it.
nfs-idmapd.service is a disabled or a static unit, not starting it.
nfs-utils.service is a disabled or a static unit, not starting it.
proc-fs-nfsd.mount is a disabled or a static unit, not starting it.
rpc-gssd.service is a disabled or a static unit, not starting it.
rpc-statd-notify.service is a disabled or a static unit, not starting it.
rpc-statd.service is a disabled or a static unit, not starting it.
rpc-svcgssd.service is a disabled or a static unit, not starting it.
Processing triggers for man-db (2.11.2-1) ...
Processing triggers for libc-bin (2.37-0ubuntu2.2) ...
Scanning processes...
Scanning candidates...
Restarting services...
systemctl restart snapd.seeded.service
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
and finally, let’s mount our share on the filesystem:
That’s it you’re done and unless your have a terrible internet, we should be way under the 2 minute mark!
If you want to go a step further, you can automatically mount your your nfs share in fstab. In my case it would look something like this:
...
192.168.68.108:/home/ /mnt/ nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
Patching common errors
Depending on your setup you might see a couple very common errors. Let’s fix those:
common issue 1: you don’t have the nfs mount driver installed:
mount: /mnt: bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program.
dmesg(1) may have more information after failed mount system call.
the fix is to install the missing nfs driver like this:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package nfs-utils
root@tmp:~# apt install nfs-common
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
keyutils libnfsidmap1 rpcbind
Suggested packages:
watchdog
The following NEW packages will be installed:
keyutils libnfsidmap1 nfs-common rpcbind
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 397 kB of archives.
After this operation, 1426 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu lunar/main amd64 libnfsidmap1 amd64 1:2.6.2-4ubuntu1 [46.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu lunar/main amd64 rpcbind amd64 1.2.6-6 [45.8 kB]
Get:3 http://archive.ubuntu.com/ubuntu lunar/main amd64 keyutils amd64 1.6.3-2 [56.4 kB]
Get:4 http://archive.ubuntu.com/ubuntu lunar/main amd64 nfs-common amd64 1:2.6.2-4ubuntu1 [248 kB]
Fetched 397 kB in 2s (162 kB/s)
Selecting previously unselected package libnfsidmap1:amd64.
(Reading database ... 33959 files and directories currently installed.)
Preparing to unpack .../libnfsidmap1_1%3a2.6.2-4ubuntu1_amd64.deb ...
Unpacking libnfsidmap1:amd64 (1:2.6.2-4ubuntu1) ...
Selecting previously unselected package rpcbind.
Preparing to unpack .../rpcbind_1.2.6-6_amd64.deb ...
Unpacking rpcbind (1.2.6-6) ...
Selecting previously unselected package keyutils.
Preparing to unpack .../keyutils_1.6.3-2_amd64.deb ...
Unpacking keyutils (1.6.3-2) ...
Selecting previously unselected package nfs-common.
Preparing to unpack .../nfs-common_1%3a2.6.2-4ubuntu1_amd64.deb ...
Unpacking nfs-common (1:2.6.2-4ubuntu1) ...
Setting up libnfsidmap1:amd64 (1:2.6.2-4ubuntu1) ...
Setting up rpcbind (1.2.6-6) ...
Created symlink /etc/systemd/system/multi-user.target.wants/rpcbind.service → /lib/systemd/system/rpcbind.service.
Created symlink /etc/systemd/system/sockets.target.wants/rpcbind.socket → /lib/systemd/system/rpcbind.socket.
Setting up keyutils (1.6.3-2) ...
Setting up nfs-common (1:2.6.2-4ubuntu1) ...
Creating config file /etc/idmapd.conf with new version
Creating config file /etc/nfs.conf with new version
Adding system user `statd' (UID 110) ...
Adding new user `statd' (UID 110) with group `nogroup' ...
Not creating home directory `/var/lib/nfs'.
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.
Created symlink /etc/systemd/system/remote-fs.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.
auth-rpcgss-module.service is a disabled or a static unit, not starting it.
nfs-idmapd.service is a disabled or a static unit, not starting it.
nfs-utils.service is a disabled or a static unit, not starting it.
proc-fs-nfsd.mount is a disabled or a static unit, not starting it.
rpc-gssd.service is a disabled or a static unit, not starting it.
rpc-statd-notify.service is a disabled or a static unit, not starting it.
rpc-statd.service is a disabled or a static unit, not starting it.
rpc-svcgssd.service is a disabled or a static unit, not starting it.
Processing triggers for man-db (2.11.2-1) ...
Processing triggers for libc-bin (2.37-0ubuntu2.2) ...
Scanning processes...
Scanning candidates...
Restarting services...
systemctl restart snapd.seeded.service
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
common issue 2: Operation not permitted in lxd:
Created symlink /run/systemd/system/remote-fs.target.wants/rpc-statd.service → /lib/systemd/system/rpc-statd.service.
mount.nfs: Operation not permitted
the fix can be 2 folds, first let’s try the simple one:
If that’s not enough, you might need to change the lxd config like this:
~/$ lxc config set tmp security.nesting true
~/$ # you might need to restart your container or vm
~/$ reboot