21.3. Mounting NFS Filesystems
The client side is where file service meets real workstations, and where most NFS confusion actually happens: an NFS mount looks local, but its semantics - ownership numbers, failure behavior, boot dependencies - are network semantics. This section mounts the exports by hand, makes the mounts survive reboots without hanging them, examines what identity really crossed the wire, and then breaks the server on purpose to see what a client does.
21.3.1. Client Packages and Initial Mount Tests
On client1, as john, install the client set for the whole chapter
(nfs-common provides the NFS client and idmap helper, cifs-utils
and smbclient arrive for the SMB sections; netcat-openbsd and
bind9-dnsutils were already installed in Section 21.2.3 and are
listed again so the set is complete - apt simply reports them as
already installed):
$ sudo apt install nfs-common cifs-utils smbclient acl attr netcat-openbsd bind9-dnsutils
Answer apt's confirmation prompt with Y. The client needs no service
of its own beyond rpc-gssd/idmap plumbing that nfs-common arranges.
Create the mount points that the section uses - open a root shell on
client1 for the filesystem work ($ sudo -i from john's shell):
# mkdir -p /mnt/team /mnt/public /mnt/private /mnt/admin /mnt/scratch /mnt/soft
The command is silent. Leave the root shell (exit) and mount the
team export from john's shell:
$ sudo mount -t nfs4 files.lab.example:/team /mnt/team
A successful NFSv4 mount prints nothing. The source is the server's
name plus the path below the pseudo-root - /team, not
/srv/nfs/team. Inspect what the kernel actually negotiated:
$ findmnt -t nfs4 -o TARGET,SOURCE,FSTYPE,OPTIONS
TARGET SOURCE FSTYPE OPTIONS
/mnt/team files.lab.example:/team nfs4 rw,relatime,vers=4.2,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.230.20,local_lock=none,addr=192.168.230.10
findmnt filtered its output to NFSv4 mounts (-t nfs4) and selected
four columns (-o TARGET,SOURCE,FSTYPE,OPTIONS). The options line is
the mount's contract: protocol version 4.2, sec=sys authentication,
TCP, and - critically - hard, meaning the mount retries forever
during outages instead of failing (Section 21.3.4 makes that
tangible). The 128 KiB read/write sizes are the negotiated transfer
windows.
Write a file as john and look at what the server recorded. First the
write - the shell redirects printf's output into the file, so
nothing appears on the terminal:
$ printf "hello from client1\n" > /mnt/team/john-nfs.txt
$ ls -ln /mnt/team/john-nfs.txt
-rw-rw----+ 1 2100 2105 19 Nov 22 03:57 /mnt/team/john-nfs.txt
ls -ln shows numeric owner and group. Then the same file as the
server's own stat reports it - on filesrv, in a root shell
($ sudo -i from john's shell on filesrv; keep one available for the
server-side checks that recur through the chapter):
# stat -c '%u %g %a %n' /srv/nfs/team/john-nfs.txt
2100 2105 660 /srv/nfs/team/john-nfs.txt
Owner 2100, group 2105, mode 660 - identical on both sides, because
sec=sys carried the numbers unchanged and the server enforced its
directory policy (mode 2770 plus the default ACL) against them. The
+ in ls marks the POSIX ACL the default ACL attached to the new
file; its contents become visible in Section 21.4.3.
The read-only export demonstrates the next policy layer. On client1, as john:
$ sudo mount -t nfs4 -o ro files.lab.example:/public /mnt/public
The mount is silent. Try to write:
$ touch /mnt/public/denied.txt
touch: cannot touch '/mnt/public/denied.txt': Read-only file system
The write fails with the same error a read-only local filesystem would
give - the ro export option crossed into the mount's superblock, so
the refusal happens in the client kernel without a round trip.
showmount asks the server's mount daemon what it is willing to
serve. On client1, as john:
$ sudo showmount -e files.lab.example
Export list for files.lab.example:
/srv/nfs/scratch 10.21.0.0/24,192.168.230.0/24
/srv/nfs/public 10.21.0.0/24,192.168.230.0/24
/srv/nfs/team 10.21.0.0/24,192.168.230.0/24
/srv/nfs 10.21.0.0/24,192.168.230.0/24
/srv/nfs/admin 192.168.230.20
/srv/nfs/private 10.21.0.0/24,192.168.230.20
| Option | Description |
|---|---|
-e |
List the server's exports and the clients each is willing to serve |
-d |
List directories clients currently have mounted |
-a |
List mounted exports per client address and mount point |
The listing shows server paths (it speaks the mount daemon's dialect, not the pseudo-root's), and it shows every client group each export admits. It is a discovery tool, not an authorization display - anyone permitted to reach the daemon sees the same list, which is why the export list was never a secret and the firewall carries the real boundary.
21.3.2. Persistent Mounts, Network Dependencies, and Automounting
A mount typed at the shell disappears at reboot, but a file service
mount has a problem a local disk mount does not: it can only work
after the network is up. /etc/fstab entries are processed at boot,
and an NFS entry that tries to mount before the interface has an
address can stall boot for minutes or fail outright.
systemd answers with mount options that declare the dependency and defer the work. Unmount the temporary team mount first - on client1, as john:
$ sudo umount /mnt/team
The unmount prints nothing. Then add this line to /etc/fstab on client1 (with the editor from
Chapter 3):
files.lab.example:/team /mnt/team nfs4 _netdev,nofail,x-systemd.automount,x-systemd.idle-timeout=60,sec=sys 0 0
The options that make this safe:
| Option | Description |
|---|---|
_netdev |
Marks the filesystem as network-dependent; systemd orders it after the network is up |
nofail |
Boot continues even if this mount cannot complete |
x-systemd.automount |
Create an autofs mount point now; perform the real NFS mount on first access |
x-systemd.idle-timeout=60 |
Unmount after 60 seconds without access; the next access remounts automatically |
sec=sys |
Passed through to the NFS mount (the lab's explicit baseline) |
x-systemd.automount is the piece that changes boot behavior
qualitatively: systemd creates a lightweight autofs entry at
/mnt/team, nothing crosses the network at boot, and the first
process that touches the directory triggers the real mount. A server
outage no longer stalls boot, and an idle laptop unmounts after a
minute rather than holding a connection open. Make systemd read the
changed file and start the automount unit - in a root shell on client1
($ sudo -i from john's shell):
# systemctl daemon-reload
# systemctl start mnt-team.automount
Both commands are silent. The unit name derives from the mount path
(/mnt/team becomes mnt-team.automount). Back in john's shell
(exit), trigger the mount by using the directory, then show both
layers:
$ ls /mnt/team >/dev/null; findmnt -no TARGET,SOURCE,FSTYPE /mnt/team
/mnt/team systemd-1 autofs
/mnt/team files.lab.example:/team nfs4
The ls accessed the directory, which satisfied the autofs trigger;
its own listing was redirected to /dev/null so it does not clutter
the output - the point of the pipeline is what findmnt reports: two
entries for one path, the systemd-1 autofs shim and the real NFSv4
mount it summoned. The mount now appears and disappears with use, and
it returns after a reboot without any boot-time network wait. The same
pattern - _netdev,nofail,x-systemd.automount - is the right default
for every network filesystem entry in /etc/fstab, including the CIFS
entry in Section 21.5.2.
21.3.3. UID and GID Alignment and NFSv4 Identity Mapping
Section 21.1.2 made the claim that sec=sys carries numbers, not
names; this subsection watches it happen, in both its good and bad
cases. First confirm the lab's identity baseline on a client - on
client1, as john:
$ id; getent passwd john alice; getent group team
uid=2100(john) gid=2100(john) groups=2100(john),27(sudo),2105(team)
john:x:2100:2100::/home/john:/bin/bash
alice:x:2101:2101::/home/alice:/bin/bash
team:x:2105:john,alice
Three commands ran in sequence: id shows the current user, getent
passwd shows the two accounts, and getent group team shows the
collaboration group's membership. Every host in the lab carries these
same numbers, which is why the file written in Section 21.3.1 simply
worked.
Now break the alignment deliberately. Run a process under a UID that exists on no host - on client1, as john:
$ sudo setpriv --reuid 3100 --regid 3100 --clear-groups touch /mnt/scratch/uid3100.txt
Like any touch, it prints nothing on success. The options used:
| Option | Description |
|---|---|
--reuid=, --regid= |
Run the command with the given effective UID and GID |
--clear-groups |
Drop all supplementary groups |
--reset-env |
Not used here, but often wanted: reset environment variables instead of inheriting them |
setpriv runs touch with effective UID and GID 3100 and no
supplementary groups, which is exactly what a process on a
differently-numbered host would look like to the server. On filesrv,
in the root shell:
# stat -c '%u %g %n' /srv/nfs/scratch/uid3100.txt; getent passwd 3100 || true
3100 3100 /srv/nfs/scratch/uid3100.txt
The server stored the foreign numbers verbatim; getent passwd 3100
printed nothing (the || true keeps the shell's exit status clean
after the expected empty miss), so no account owns that file. Nothing
warned, nothing failed - the write succeeded because scratch is
world-writable - and the result is a file whose owner is a number with
no name, unreadable by any legitimate account if a stricter directory
had been involved. That silent success is the failure mode of
unmanaged UID drift.
Root identity behaves through the same mechanism, shaped by
root_squash. On client1, as john:
$ sudo touch /mnt/team/root-denied.txt
touch: cannot touch '/mnt/team/root-denied.txt': Permission denied
Client root claimed UID 0; the server rewrote it to 65534; nobody
cannot write in a 2770 john:team directory, so the refusal is a plain
permission error. In the world-writable scratch export the same claim
succeeds - and shows the rewrite - on client1:
$ sudo mount -t nfs4 files.lab.example:/scratch /mnt/scratch
$ sudo touch /mnt/scratch/root-scratch.txt
Both commands are silent. On filesrv, in the root shell:
# stat -c '%u %g %n' /srv/nfs/scratch/root-scratch.txt
65534 65534 /srv/nfs/scratch/root-scratch.txt
The file exists - the directory is 0777 - but its owner is
nobody:nobody (65534), proving the client's UID 0 never reached the
filesystem. For contrast, the isolated NFSv3 export in
/etc/exports says no_root_squash. Mount it with the old protocol
version - on client1, as john:
$ sudo mount -t nfs -o vers=3 files.lab.example:/srv/nfs/admin /mnt/admin
The mount is silent. Note the difference from the NFSv4 mounts: NFSv3 has no pseudo-root, so the source is the server's real path. Write as client root:
$ sudo touch /mnt/admin/root-v3.txt
Also silent. Check the server's record - on filesrv, in the root shell:
# stat -c '%u %g %n' /srv/nfs/admin/root-v3.txt
0 0 /srv/nfs/admin/root-v3.txt
Owner 0:0. With no_root_squash, the client's root assertion was
honored as server root. That is the whole argument for keeping
root_squash as the default in one captured pair of commands: the
difference between the two stat outputs is precisely the trust a
server grants or withholds.
Where do names enter at all? NFSv4 does carry user@domain strings -
in ACLs, and whenever the protocol represents a principal - and the
translation between those names and local numbers is the idmap
daemon's job, governed by the domain both ends must share. The
server's domain was checked in Section 21.2.1; the client reports the
same value - on client1, as john:
$ sudo /usr/sbin/nfsidmap -d
lab.example
With matching domains and aligned accounts, names and numbers agree,
and ls on an NFS mount shows usernames rather than bare numbers.
Mismatched domains produce nobody-owned files in name-sensitive
operations even when the numbers would have worked - the classic
"files show as nobody on NFSv4" complaint - and the fix is the same
Domain value in /etc/idmapd.conf everywhere. For sites where
keeping UIDs aligned by hand stops scaling, the structural answers are
central identity (Chapter 35) or
Kerberos NFS, where user - not host - identity authenticates.
21.3.4. Stale Handles, Server Outages, and Hung I/O
An NFS mount holds file handles - opaque references the server
issued for directories and files. If the server restarts, or an export
is redefined, handles already in client caches can stop resolving, and
the error that surfaces is Stale file handle. The other failure
shape is the inverse: the server is unreachable and the client must
decide between waiting forever and giving up. NFS mount options
control that decision:
| Option | Description |
|---|---|
hard (default) |
Retry indefinitely when the server is unreachable; file operations block until it returns |
soft |
Give up after retrans failed retries of timeo deciseconds and return an I/O error to the application |
timeo=600 |
Retry timeout in deciseconds (default 600 = 60 seconds) |
retrans=2 |
Retries before a soft mount reports failure |
ro, rw |
Read-only or read-write mount |
vers= |
Request a specific protocol version (the default negotiates the newest both sides support) |
hard is the default because it is correct for programs that expect
filesystem semantics: a database or editor that suddenly receives "I/O
error" on a transient network blip can corrupt or abandon work, while
blocking for a minute is usually harmless. soft trades that safety
for responsiveness, and belongs on mounts where an error is
acceptable - read-only browsing, scratch space, anything a user can
simply retry. Never put a writable database or a build tree on a
soft mount.
Watch both behaviors. Mount a deliberately soft copy of the team export - on client1, as john:
$ sudo mount -t nfs4 -o soft,timeo=50,retrans=1 files.lab.example:/team /mnt/soft
The retry math is bounded but not instant: timeo=50 sets the retry
timeout to five seconds (the unit is deciseconds) and retrans=1
bounds the attempts before a soft mount reports failure. Now stop the
server. Stopping a production file server interrupts every active
client; on the lab it is the point of the exercise - on filesrv, as
john:
$ sudo systemctl stop nfs-server
The stop is silent. On client1, as john, use the soft mount while the server is down:
$ ls /mnt/soft
ls: cannot open directory '/mnt/soft': Stale file handle
After roughly half a minute of retries - several timeout windows, not
the single five-second one - the soft mount gave up and the
application saw the failure, quickly compared with an indefinite wait
and without a hang. The
hard team mount from Section 21.3.2 would simply block through the
same outage (interrupting the blocked process with Ctrl+C works on
modern kernels; there is no intr option anymore because signals are
always deliverable). A production server outage therefore splits
clients by their mount type: soft mounts report errors and move on,
hard mounts wait in silence until the service returns.
Bring the server back - on filesrv, as john:
$ sudo systemctl start nfs-server
Also silent. Give the mount a moment - the NFSv4 client must notice the server is back and re-establish its state - then verify that the soft mount came back on its own, on client1, as john:
$ ls /mnt/soft >/dev/null; findmnt -no TARGET,SOURCE /mnt/soft
/mnt/soft files.lab.example:/team
The ls produced no visible output (its listing went to /dev/null;
the directory's contents are unchanged), and findmnt confirms the
mount is serving again. The Stale file handle error earlier was the
soft mount's bounded answer to a dead server - an error delivered to
that one ls - not lasting damage: the mount needed no unmount or
remount, because the server's export table and filesystem came back
unchanged, so the handles the client already held still resolved. The
recovery recipe is simply to restore the server and verify access. For
a mount that will not unmount because a
process is blocked inside it, umount -l (lazy detach) removes it
from the namespace so new accesses fail fast instead of hanging, while
the blocked process finishes or is killed separately.
The stale-handle scenario that genuinely requires remounting is an
export redefinition: if you remove and re-add an export with a
different identity, or replace the backing filesystem, hard-mounted
clients hold handles the server can no longer resolve at all. The
client symptom is the same Stale file handle, but this time it
persists after the server is healthy, and the fix is the umount,
mount pair - once the server is stable. Announcing NFS server
restarts to users is still worth the trouble: every client blocks or
errors for the duration, and the recovery above only works because
the export layout came back unchanged.