Sub topic 8
NFS versions and how they work
This chapter covers how NFS is built, how versions 3 and 4 differ, and which ports and daemons each version uses. It assumes you are comfortable with the Linux command line, users, groups, and permissions, TCP/IP basics, and managing services with systemctl. Later chapters cover exporting and mounting. Here you learn the working model so those steps make sense.
What NFS is
NFS (Network File System) lets a client mount a directory that lives on a server and use it as if it were local. Programs on the client call open(), read(), and write() as usual. The kernel NFS client turns those calls into network requests, and the server applies them to its real filesystem.
NFS is the native sharing protocol for Unix-like systems. It uses Unix user IDs, group IDs, and mode bits directly. Samba, by contrast, translates Windows-style accounts and access rules onto Unix files.
NFS is built on RPC (Remote Procedure Call). The client calls named procedures on the server, such as LOOKUP, READ, WRITE, and GETATTR. The server runs them and returns the result.
The version history
Four versions matter in practice. Servers and clients negotiate the version at mount time.
| Version | Year | Transport | State | Main traits |
|---|---|---|---|---|
| NFSv2 | 1980s | UDP | Stateless | 32-bit file sizes, obsolete. Disabled in modern kernels. |
| NFSv3 | 1995 | TCP or UDP | Stateless | Large files, async writes, several helper daemons and ports. |
| NFSv4.0 | 2003 | TCP | Stateful | One port, built-in locking, ACLs, optional Kerberos. |
| NFSv4.1 and 4.2 | 2010 and 2016 | TCP | Stateful | Sessions, parallel NFS (pNFS), server-side copy, sparse files. |
Most current distributions default to NFSv4.2 when both sides support it. They fall back to lower versions when the client asks for one or the server lacks support.
Stateless and stateful designs
NFSv3 is stateless. The server does not remember which clients have a file open. Each request carries everything the server needs, in the form of a file handle. A file handle is an opaque identifier for a file or directory. A server can reboot and clients continue, because no per-client memory is lost.
The cost is that features needing memory must live elsewhere. File locking in NFSv3 is handled by a separate protocol, the Network Lock Manager (NLM), and a status monitor (rpc.statd) that tracks which hosts hold locks.
NFSv4 is stateful. The server tracks open files, locks, and delegations. A delegation lets the server hand a client temporary authority over a file, so the client can cache it aggressively until another client needs it. Because the server holds state, it also runs a grace period after restart. During that period, clients reclaim their locks and no new locks are granted. You may see applications pause briefly after a server reboot.
How NFSv3 is wired together
An NFSv3 server needs several cooperating services:
rpcbind(also called portmapper) listens on port 111. Clients ask it which port each RPC service uses.rpc.mountdhandles mount requests and checks them against/etc/exports.nfsdruns in the kernel and serves the actual file operations, on port 2049.rpc.statdand the lock managerlockdhandle locking and crash recovery.
Only rpcbind and nfsd use fixed ports. The others choose ports dynamically from a range unless you pin them in the NFS configuration. This is why NFSv3 is awkward behind firewalls. You must either open a wide range of ports or fix the helper ports in configuration and open exactly those.
How NFSv4 simplifies this
NFSv4 folds mounting, locking, and state tracking into a single protocol on TCP port 2049. A client needs only that port. Neither rpcbind nor rpc.mountd is involved for a pure NFSv4 mount, although rpc.mountd still runs on the server when v3 is enabled. NFSv4 also drops UDP.
Other NFSv4 differences:
- Compound operations. The client can bundle several operations into one request, which cuts round trips on high-latency links.
- Pseudo-filesystem. The server presents exports under a single root. Older setups used
fsid=0to mark that root. Current servers let you mount an export by its real path, as in themount -t nfs4 fileserver:/srv/nfs/projectsexample in this tutorial. - ID names. NFSv4 sends user and group identities as
user@domainstrings by default instead of raw numbers. A helper calledidmapdconverts them. The next chapters on identity mapping cover the consequences. - ACLs. NFSv4 has its own access control list model, which differs from POSIX ACLs.
- Security. Kerberos (
sec=krb5,krb5i,krb5p) is part of the design and offers authentication, integrity, and encryption. Without Kerberos, the server trusts the user IDs that the client claims.
What a request looks like
The following diagram shows the sequence for an NFSv4 mount followed by a read.
Checking which version you are using
On the client, nfsstat -m shows each NFS mount and its negotiated options:
nfsstat -mLook for vers=4.2 and proto=tcp in the output. You can also run mount | grep nfs.
To request a version explicitly, pass the vers option:
sudo mount -t nfs -o vers=3 fileserver:/srv/nfs/projects /mnt/projects
sudo mount -t nfs -o vers=4.1 fileserver:/srv/nfs/projects /mnt/projectsUsing -t nfs4 forces version 4, and the minor version is negotiated downward as needed.
On the server, the kernel exposes which versions are enabled:
cat /proc/fs/nfsd/versionsA result such as -2 +3 +4 +4.1 +4.2 means v2 is off and the rest are on. On Red Hat-family systems you control this in /etc/nfs.conf under the [nfsd] section, for example:
[nfsd]
vers3=n
vers4=y
vers4.1=y
vers4.2=yRestart nfs-server after changing it. Disabling v3 shrinks the attack surface and removes the dynamic ports, but older clients that only speak v3 will then fail to mount.
Hard mounts, soft mounts, and failure behavior
Because the network can fail, NFS clients choose how to react when the server stops answering.
- A hard mount (the default) retries forever. Processes touching the mount block, often in uninterruptible sleep, until the server returns. No data is silently lost.
- A soft mount gives up after
retransretries and returns an I/O error to the application. This can corrupt data if an application does not check for write errors.
Use hard for anything that writes data. Reserve soft for read-only, non-critical access.
The intr option is obsolete. Modern kernels let you interrupt a blocked process with a fatal signal such as SIGKILL regardless.
Caching and consistency
NFS clients cache file data and attributes to avoid network trips. NFS therefore offers close-to-open consistency. When a client closes a file, its changes are flushed to the server. When another client opens that file, it checks with the server and sees the latest data. Between open and close, two clients can see different views of the same file.
The attribute cache lifetime is controlled by options such as actimeo. Short values give fresher views at the cost of more traffic. Setting noac disables attribute caching and is slow. Applications that need strict coordination between hosts should use file locks, not rely on timing.
Server-side, the export option sync (the default) makes the server commit data to stable storage before replying to a write. async replies earlier and risks losing acknowledged data if the server crashes. The later chapter on exports covers these options.
Common mistakes
- Assuming a firewall opening for port 2049 is enough for NFSv3. It is not, because
rpcbindandmountdneed reachable ports too. - Forgetting that a server with v3 disabled rejects clients that request
vers=3. - Seeing
nobodyas the owner of files on an NFSv4 mount. This usually means the ID domain differs between client and server, which a later chapter explains. - Using
softmounts for writable data.
Summary of the working model
- NFS runs file operations over RPC between the kernel client and the kernel server
nfsd. - NFSv3 is stateless, uses helper daemons and several ports, and needs
rpcbind. - NFSv4 is stateful, uses only TCP 2049, and supports compound requests, delegations, ACLs, and Kerberos.
- The client and server negotiate the version. You can inspect and pin it with
nfsstat -m,vers=, and/etc/nfs.conf. - Hard mounts protect data. Close-to-open consistency governs what clients see.
Exercise
A client mounted with -t nfs -o vers=3 works, but after you set vers3=n in /etc/nfs.conf on the server and restart nfs-server, new mounts with vers=3 fail while mount -t nfs4 still works. Why, and which port must be reachable for the working mounts?
Show answer
The server no longer offers NFSv3, so it rejects any client that requests that version. NFSv4 is still enabled, and it needs only TCP port 2049, because it does not use rpcbind or mountd for the mount itself.
Exercise
An application writes backup files to an NFS mount using soft and sometimes ends up with truncated files after a short network outage. What is the likely cause, and what should you change?
Show answer
A soft mount returns an I/O error after the retry limit, and an application that ignores write errors continues as though the write succeeded. Switch to a hard mount so operations block and retry until the server returns.