21.2. Providing NFS Exports
The NFS server in Debian 13 is the kernel's nfsd plus a small set of
user-space helpers from nfs-kernel-server. NFSv4 - the version a
modern client negotiates by default - needs one TCP port and no
separate lock daemon; the auxiliary services that NFSv3 required still
run for compatibility, and pinning their ports keeps future firewall
tightening possible. This section builds the export tree, defines the
exports, and closes the network path down to exactly the clients that
should have it.
21.2.1. Installing the Server and Defining NFSv4 Exports
On filesrv, as john, install the NFS server package and the
filesystem tools this chapter uses (acl and attr provide
setfacl/getfacl and setfattr/getfattr; quota provides the quota
tools):
$ sudo apt install nfs-kernel-server acl attr quota
apt prints the package plan and asks for confirmation; answer Y (or
just press Enter, since the default is to proceed). The package
registers nfs-server.service, enabled by Debian's preset.
The rest of the server preparation is sustained administrative work, so open a root shell and stay in it for this section. On filesrv, as john:
$ sudo -i
The prompt becomes #. Note that HOME is now /root, so ~ no
longer means john's home directory - the workspace stays at
/home/john/debian-handbook/network-files and this section does not
use it.
The practice data lives on the second disk, /dev/vdb (2 GiB in the
lab). Creating a filesystem on /dev/vdb destroys whatever is on
it - confirm the device name with lsblk first, as
Chapter 11 showed. Create it with ext4's
quota feature enabled:
# mkfs.ext4 -F -O quota -L filedata /dev/vdb
mke2fs 1.47.2 (1-Jan-2025)
Discarding device blocks: 0/524288 done
Creating filesystem with 524288 4k blocks and 131072 inodes
Filesystem UUID: 2f14e70e-b5a2-4b9d-8d87-6d05a0bd8809
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: 0/16 done
Writing inode tables: 0/16 done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: 0/16 done
The progress report is mke2fs's own narration - the same report
Chapter 9 walked through when it first
appeared, and -q would suppress it, but the visible confirmation
that the filesystem was actually written is worth its dozen lines
here. -F is required because the target is a whole bare disk, not a
partition - mke2fs would otherwise stop and ask for confirmation that
you really mean to put a filesystem on the entire device - and -O
quota is what writes the quota inodes now rather than in a later
quotacheck pass. Find the new filesystem's UUID, add a persistent
mount, and mount it:
# blkid -s UUID -o value /dev/vdb
2f14e70e-b5a2-4b9d-8d87-6d05a0bd8809
One line, the UUID of the freshly created filesystem - the same value
the progress report printed, and the lab's own; yours will differ. Add
the mount to /etc/fstab on filesrv with the editor from
Chapter 3, substituting the value just printed
for uuid-from-blkid:
UUID=uuid-from-blkid /srv/nfs ext4 defaults,usrquota,grpquota,acl 0 2
The usrquota,grpquota options let the kernel account for per-user and
per-group usage; acl is ext4's default but stated explicitly here so
the intent survives later edits. mkfs.ext4 -O quota wrote the quota
inodes at creation time, so no quotacheck pass is needed. Mount and
create the share tree:
# mount -a
mount -a processes every entry in /etc/fstab and mounts the ones
that are not mounted yet - here the new /srv/nfs entry - and prints
nothing when it succeeds. Create the directories with the owners and
modes from Section 21.1.3:
# mkdir -p /srv/nfs/team /srv/nfs/private /srv/nfs/public /srv/nfs/admin /srv/nfs/scratch
# chown john:team /srv/nfs/team && chmod 2770 /srv/nfs/team
The && between the two commands runs the second only if the first
succeeds - set the owner, then the mode, for each directory in turn:
# chown john:john /srv/nfs/private && chmod 0700 /srv/nfs/private
# chown root:team /srv/nfs/public && chmod 0755 /srv/nfs/public
# chown root:root /srv/nfs/admin && chmod 0700 /srv/nfs/admin
# chown john:team /srv/nfs/scratch && chmod 0777 /srv/nfs/scratch
Each chown and chmod is silent. Give john a quota on the volume -
soft limit 61440 KiB (60 MiB), hard limit 81920 KiB (80 MiB), no inode
limits - and set the default ACL that makes every new file in team
readable by alice:
# setquota -u john 61440 81920 0 0 /srv/nfs
# setfacl -m d:u::rwx,d:u:alice:rwx,d:g::rwx,d:o::---,d:m::rwx /srv/nfs/team
Both commands are silent. The default ACL entries (the d: prefix)
apply to future creations in the directory rather than to the directory
itself; Section 21.4.3 shows the effect on a file created through SMB.
Leave the root shell with exit and verify the mounted volume from
john's shell:
$ sudo df -h /srv/nfs
Filesystem Size Used Avail Use% Mounted on
/dev/vdb 2.0G 616K 1.8G 1% /srv/nfs
The 2 GiB volume is mounted, nearly empty, and ready to hold the chapter's practice data. Real servers grow this volume with LVM as Chapter 13 described; the workflow on top does not change.
The remaining server-side file edits are administrative again - on
filesrv, open a root shell once more with $ sudo -i from john's shell
and keep it for the export and daemon configuration below.
Now define the exports. The complete contents of /etc/exports on
filesrv:
# NFSv4 exports for filesrv.lab.example.
# The first line is the NFSv4 pseudo-root (fsid=0). Children are exported
# separately so each can carry its own access policy.
/srv/nfs 192.168.230.0/24(rw,sync,no_subtree_check,fsid=0,root_squash,sec=sys) 10.21.0.0/24(rw,sync,no_subtree_check,fsid=0,root_squash,sec=sys)
/srv/nfs/team 192.168.230.0/24(rw,sync,no_subtree_check,root_squash,sec=sys) 10.21.0.0/24(rw,sync,no_subtree_check,root_squash,sec=sys)
/srv/nfs/private 192.168.230.20(rw,sync,no_subtree_check,root_squash,sec=sys) 10.21.0.0/24(rw,sync,no_subtree_check,root_squash,sec=sys)
/srv/nfs/public 192.168.230.0/24(ro,sync,no_subtree_check,root_squash,sec=sys) 10.21.0.0/24(ro,sync,no_subtree_check,root_squash,sec=sys)
/srv/nfs/admin 192.168.230.20(rw,sync,no_subtree_check,no_root_squash,sec=sys)
/srv/nfs/scratch 192.168.230.0/24(rw,sync,no_subtree_check,root_squash,sec=sys) 10.21.0.0/24(rw,sync,no_subtree_check,root_squash,sec=sys)
Figure 21.3. /srv/nfs is the NFSv4 pseudo-root; clients mount
children by name below it. The dashed admin export is deliberately
separate: it exists to show an NFSv3 no_root_squash comparison, and
the lab mounts it by its full server path with vers=3.
The first line is the piece that makes the rest look tidy: fsid=0
marks /srv/nfs as the NFSv4 pseudo-root. An NFSv4 client mounts
files.lab.example:/team and the server resolves that name inside the
pseudo-root, so clients never need to know the server's actual path
/srv/nfs/team - the exported namespace is decoupled from the disk
layout, and moving the tree later changes only the export file. Each
child carries its own client list and options, which is why they are
exported individually instead of relying on the root's policy.
Two more server-side pieces keep the service predictable. First, pin
the ports of the NFSv3-era helper daemons so a future firewall
tightening knows exactly what to admit. Append these sections to
/etc/nfs.conf on filesrv (the file's existing [general] and other
sections stay as they are):
[mountd]
port=20048
[statd]
port=32765
outgoing-port=32766
[lockd]
port=32768
udp-port=32768
In the lab these lines were appended from a prepared snippet file; with the editor, add them at the end of the file. Confirm they landed:
# tail -n 12 /etc/nfs.conf
# principal=
[mountd]
port=20048
[statd]
port=32765
outgoing-port=32766
[lockd]
port=32768
udp-port=32768
Second, both server and client must agree on the NFSv4 identity
mapping domain - the string used to translate between numeric IDs and
user@domain names when the protocol carries names (NFSv4 ACLs, and
Kerberos flavors). On Debian it defaults to the host's DNS domain.
Leave the root shell with exit and check the value on filesrv, as
john:
$ sudo /usr/sbin/nfsidmap -d
lab.example
The lab's DNS domain is lab.example, so both ends of every mount
agree; if your hosts' DNS domains differ, set the same Domain value
in /etc/idmapd.conf on every participant.
Activate the exports. Still on filesrv, as john:
$ sudo systemctl enable --now nfs-server
The command produced no output here because the package preset had
already enabled the unit; it prints a Created symlink line when it
enables a unit for the first time. Reload the export list from
/etc/exports and inspect what the kernel is now serving (the reload
itself prints nothing):
$ sudo exportfs -ra
$ sudo exportfs -v
/srv/nfs/private
192.168.230.20(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
/srv/nfs/admin 192.168.230.20(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
/srv/nfs 192.168.230.0/24(sync,wdelay,hide,no_subtree_check,fsid=0,sec=sys,rw,secure,root_squash,no_all_squash)
/srv/nfs 10.21.0.0/24(sync,wdelay,hide,no_subtree_check,fsid=0,sec=sys,rw,secure,root_squash,no_all_squash)
/srv/nfs/team 192.168.230.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
/srv/nfs/team 10.21.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
/srv/nfs/private
10.21.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
/srv/nfs/public
192.168.230.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)
/srv/nfs/public
10.21.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)
/srv/nfs/scratch
192.168.230.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
/srv/nfs/scratch
10.21.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
exportfs -v prints the effective options, including defaults the
export file did not spell out. Reading it line by line is worth the
habit: the pseudo-root shows fsid=0, private is limited to
192.168.230.20 (client1's address) plus the tunnel network, public
shows ro for both networks, and only admin shows no_root_squash.
The exportfs options used here:
| Option | Description |
|---|---|
-r |
Re-export every entry in /etc/exports (and drop entries removed from it) |
-a |
Together with -r, apply to all exports rather than one path |
-v |
List the current exports with their effective options |
Confirm the service state and the listener:
$ sudo systemctl status nfs-server --no-pager --lines=4
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; preset: enabled)
Drop-In: /run/systemd/generator/nfs-server.service.d
└─order-with-mounts.conf
Active: active (exited) since Sun 2026-11-22 03:55:18 UTC; 6s ago
Invocation: dac28f04055747b9a3f45d4633b9cf94
Docs: man:rpc.nfsd(8)
man:exportfs(8)
Process: 28764 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Process: 28766 ExecStart=/bin/sh -c /usr/sbin/nfsdctl autostart || /usr/sbin/rpc.nfsd (code=exited, status=0/SUCCESS)
Main PID: 28766 (code=exited, status=0/SUCCESS)
Mem peak: 1.8M
CPU: 19ms
Nov 22 03:55:18 filesrv systemd[1]: Starting nfs-server.service - NFS server and services...
Nov 22 03:55:18 filesrv sh[28767]: nfsdctl: lockd configuration failure
Nov 22 03:55:18 filesrv systemd[1]: Finished nfs-server.service - NFS server and services.
Two details in that output regularly confuse administrators and are
worth naming. Active: active (exited) is correct for this unit: it is
a oneshot that starts the kernel nfsd threads and exits, so "running"
would be wrong - the threads, not a daemon PID, are the server. And the
nfsdctl: lockd configuration failure line comes from the unit's
attempt to pre-configure the NFSv3 lock daemon through the new
nfsdctl interface; the service still completed successfully, and the
mount tests in Section 21.3 are the real check. Verify that the kernel
is listening on the NFSv4 port:
$ sudo ss -tlnp | grep 2049
LISTEN 0 4096 0.0.0.0:2049 0.0.0.0:*
LISTEN 0 4096 [::]:2049 [::]:*
The pipeline runs two commands: ss -tlnp lists listening TCP sockets
with process names, and grep 2049 keeps the NFS port's lines. NFSv4
lives entirely on TCP port 2049 - no portmapper negotiation, no
scattered helper ports - which is what makes its firewall story so
much simpler than NFSv3's.
21.2.2. Client Restrictions, root_squash, and Export Options
Each export line is directory client-list(options...). The client
list is a trust statement, and the options are the policy applied to
that trust. The options used in this chapter's export file:
| Option | Description |
|---|---|
rw / ro |
Read-write or read-only access for the matching clients |
sync |
Reply to writes only after the data is on stable storage - the safe default to state explicitly |
root_squash |
Map client UID 0 to the anonymous identity (65534) before permission checks |
no_root_squash |
Let client root act as server root; only for tightly controlled administrative exports |
sec=sys |
Accept AUTH_SYS numeric-UID authentication (the default, stated for clarity) |
fsid=0 |
Mark this path as the NFSv4 pseudo-root |
no_subtree_check |
Skip cross-export overlap checks; the modern default for reliability |
insecure |
Allow client source ports above 1024 (needed by some NFSv3 clients; not used here) |
Client selectors can be single addresses (192.168.230.20), CIDR
networks (192.168.230.0/24), hostnames resolved at export time, or
wildcards (*.lab.example). Prefer addresses and networks: names can
fail to resolve during boot and silently skip an export. One export
line may carry several client groups with different options - the
pseudo-root line above serves the LAN and the tunnel network with
identical policy - and a directory exported to two clients with
conflicting options gets each client its own policy.
root_squash deserves its reputation as the default worth keeping.
Under sec=sys the client asserts UIDs; without squashing, a client
root claims server root, which is exactly as dangerous as it sounds.
With squashing, client root becomes nobody (65534) and runs into the
same permission walls as any other anonymous user - in this lab, a
root-created file in the world-writable scratch tree lands owned
65534:65534, and the same root cannot write into team at all
(Section 21.3.3 shows both). no_root_squash has one legitimate
shape: an administrative export used deliberately, by known hosts, for
tasks like backup or image replication. The lab isolates that case in
/srv/nfs/admin, exported only to client1's address.
A subtlety of the NFSv4 pseudo-root is worth knowing before you are
surprised by it: the pseudo-root's own squash policy colors what
clients see through it. In this chapter's configuration the root and
all its children use root_squash consistently, and the
no_root_squash comparison lives on a separate NFSv3 export where
the option's meaning is unambiguous. If you ever need per-child
squash differences under one pseudo-root, test the exact combination
on your kernel version rather than assuming the child's option wins.
Finally, the export list is visible to clients - showmount -e prints
it - so do not treat it as secret; treat it as the first policy layer,
backed by the firewall and the directory permissions.
21.2.3. Activating Exports and Restricting Firewall Access
The exports are live the moment exportfs -ra runs, but the network
path needs two more pieces: name resolution, so clients can use
files.lab.example instead of addresses, and a packet filter that
admits exactly the trusted sources.
Give the server a lightweight DNS answer for the lab names, and give client1 the probe and lookup tools this section and Section 21.7 use. On filesrv, as john:
$ sudo apt install dnsmasq nftables
On client1, as john:
$ sudo apt install netcat-openbsd bind9-dnsutils
netcat-openbsd provides nc; bind9-dnsutils provides dig.
Then place the lab records in a dnsmasq include. The complete contents
of /etc/dnsmasq.d/ch21-lab.conf on filesrv:
interface=eth1
interface=wg0
bind-dynamic
domain-needed
bogus-priv
local=/lab.example/
address=/files.lab.example/192.168.230.10
address=/files-vpn.lab.example/10.21.0.1
log-queries
The wg0 line and the files-vpn record matter only from
Section 21.7 onward, when the WireGuard interface exists;
bind-dynamic is what lets dnsmasq bind interfaces that appear later
and survive restarts of them. Check the syntax:
$ sudo dnsmasq --test
dnsmasq: syntax check OK.
dnsmasq writes its check result to standard error; in a terminal it appears exactly as above. Restart the service:
$ sudo systemctl restart dnsmasq
The restart is silent. Confirm where the service is listening:
$ sudo ss -lntup | grep ":53 "
udp UNCONN 0 0 127.0.0.1:53 0.0.0.0:* users:(("dnsmasq",pid=28972,fd=6))
udp UNCONN 0 0 192.168.230.10:53 0.0.0.0:* users:(("dnsmasq",pid=28972,fd=4))
udp UNCONN 0 0 [::1]:53 [::]:* users:(("dnsmasq",pid=28972,fd=8))
tcp LISTEN 0 32 127.0.0.1:53 0.0.0.0:* users:(("dnsmasq",pid=28972,fd=7))
tcp LISTEN 0 32 192.168.230.10:53 0.0.0.0:* users:(("dnsmasq",pid=28972,fd=5))
tcp LISTEN 0 32 [::1]:53 [::]:* users:(("dnsmasq",pid=28972,fd=9))
dnsmasq answers on the loopback addresses and on eth1. Point each
client's resolver at the server. The same command serves both clients -
on client1 and on remoteclient, open a root shell ($ sudo -i from
john's shell on each) and write the resolver file:
# printf 'nameserver 192.168.230.10\n' > /etc/resolv.conf
The printf output is redirected into the file with >, so nothing
appears on the terminal. Read the file back - on client1, still in the
root shell:
# cat /etc/resolv.conf
nameserver 192.168.230.10
One line, the pointer to the server; remoteclient's copy is identical.
A DHCP-managed client would normally configure its resolver through the
same manager - a supersede domain-name-servers statement in
dhclient.conf, or the equivalent in NetworkManager or
systemd-networkd as Chapter 17
covered - so that lease renewals do not overwrite the pointer. Leave
the root shell (exit) on each host and resolve the name from
client1, as john:
$ dig +short @files.lab.example files.lab.example
192.168.230.10
The dig options used here and in the rest of the chapter:
| Option | Description |
|---|---|
@server |
Query this nameserver directly instead of the system resolver |
+short |
Print only the answer values, one per line |
-t TYPE |
Request a specific record type (A, MX, TXT, ...) |
-x |
Reverse lookup: turn an address into a name |
The client asked the server by name and got the server's LAN address back - name resolution and service reachability now share one destination. remoteclient's resolver gets the same file and its own verification in Section 21.7.3.
Now the firewall. The complete contents of /etc/nftables.conf on
filesrv:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
ct state established,related accept
iifname "lo" accept
iifname "eth0" accept
ip protocol icmp accept
ip6 nexthdr ipv6-icmp accept
iifname "eth1" ip saddr 192.168.230.0/24 accept
iifname "wg0" accept
iifname "eth1" udp dport 51820 accept
iifname "eth1" tcp dport 53 accept
iifname "eth1" udp dport 53 accept
}
chain forward {
type filter hook forward priority filter; policy drop;
}
}
Chapter 18 built this vocabulary; the design points
specific to a file server are the source rules. The input chain accepts
everything from the trusted LAN by source address - not merely by
interface - because remote traffic reaches this single-homed server on
the same eth1, arriving with a 192.168.231.0/24 source after the
router forwards it. An iifname "eth1" accept rule would have let the
remote LAN in; the ip saddr 192.168.230.0/24 match is what keeps
Section 21.7's denied paths honest. The wg0 accept admits tunnel
traffic once the interface exists, UDP 51820 lets peers establish the
tunnel, and the DNS rules let remote clients resolve the server's
name even though file service itself is refused. The management
interface eth0 stays open so SSH survives any mistake. Load the
ruleset and confirm it:
$ sudo nft -f /etc/nftables.conf
The load produces no output. List the active ruleset:
$ sudo nft list ruleset
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
ct state established,related accept
iifname "lo" accept
iifname "eth0" accept
ip protocol icmp accept
ip6 nexthdr ipv6-icmp accept
iifname "eth1" ip saddr 192.168.230.0/24 accept
iifname "wg0" accept
iifname "eth1" udp dport 51820 accept
iifname "eth1" tcp dport 53 accept
iifname "eth1" udp dport 53 accept
}
chain forward {
type filter hook forward priority filter; policy drop;
}
}
From client1 (as john, with nc installed above), verify that the NFS
port is reachable from the trusted LAN:
$ nc -zv -w4 files.lab.example 2049
Connection to files.lab.example (192.168.230.10) 2049 port [tcp/nfs] succeeded!
nc made a TCP connection and closed it without sending data: -z
means zero-I/O scan mode, -v prints the result (to standard error,
which is why the message is part of the transcript), and -w4 bounds
the wait to four seconds. A timed-out probe is the firewall's signature
- an actively refused service answers immediately, a filtered one stays
silent. That contrast is the diagnostic used throughout Section 21.7.
21.2.4. NFS Authentication Choices and Untrusted Networks
Everything above runs under sec=sys, and it is important to be clear
about what that means: the server trusts the client to tell the truth
about UIDs, and the network path carries that assertion in the clear.
On a network you control - a server LAN behind your own firewall, a
VPN you operate - that trust is a reasonable design, and it is the
baseline this chapter uses for both the direct LAN path and the
WireGuard path.
NFS also has its own cryptographic answer. With a Kerberos
infrastructure in place, exports and mounts can require sec=krb5
(authenticated users), sec=krb5i (integrity-protected operations),
or sec=krb5p (operations encrypted on the wire). The server exports
with sec=krb5p in the options list, holds a service principal
(nfs/filesrv.lab.example) in a keytab, and clients mount with
-o sec=krb5p; user tickets then prove identity per user instead of
per host. The prerequisites are real work - a KDC, keytabs on every
participant, gssd running, and the NFSv4 idmap domain aligned on all
sides - and this chapter does not exercise that path. It remains the
documented answer for NFS on networks you do not fully control, and
Chapter 35 builds the identity
infrastructure it depends on.
Between the two poles sits the practical middle: keep sec=sys, but
never expose port 2049 outside a network you trust. That is exactly the
shape of this chapter's lab - the remote client's direct route to
2049 dies in the server's input chain, and its legitimate access rides
an encrypted WireGuard tunnel instead. The same design at site scale
reads: NFS for the machine room and the office LAN, VPN for everyone
else, and SMB (which authenticates users and encrypts in-protocol) for
client populations that cannot be given a trusted path at all.