Sub topic 9
Exporting directories over NFS
This chapter covers how an NFS server decides what to share and who may use it. It assumes you know Linux users, groups, and permissions, can read a CIDR subnet such as 192.168.1.0/24, and can install packages and manage services with systemctl. Mounting from a client and UID/GID mapping have their own chapters. Here we cover the server side.
What an export is
An export is a directory that the NFS server makes available to remote clients. The server's kernel NFS daemon (nfsd) serves it. The list of exports lives in /etc/exports and in files under /etc/exports.d/. A helper command, exportfs, loads that list into the kernel.
Installing and starting the server differs by distribution family:
| Family | Package | Service unit |
|---|---|---|
| Debian, Ubuntu | `nfs-kernel-server` | `nfs-kernel-server` |
| RHEL, Fedora, Rocky | `nfs-utils` | `nfs-server` |
Enable the service so that it starts at boot, for example sudo systemctl enable --now nfs-server on a Red Hat-family system.
The format of /etc/exports
Each line has a directory path followed by one or more client entries. Each client entry has its own options in parentheses:
/srv/nfs/projects 192.168.1.0/24(rw,sync,no_subtree_check)Do not put a space between the client and the opening parenthesis. A space changes the meaning completely. These two lines are different:
/srv/nfs/projects 192.168.1.0/24(rw)
/srv/nfs/projects 192.168.1.0/24 (rw)The first line gives read-write access to the subnet. The second gives the subnet the default options (read-only) and gives every host read-write access. This is one of the most common and most dangerous mistakes in NFS administration.
Lines starting with # are comments.
Ways to name clients
- A single host name or IP address:
server2.example.comor192.168.1.20 - A subnet in CIDR notation:
192.168.1.0/24 - A wildcard in a host name:
*.example.com - A netgroup, written
@name, if you use NIS - A single
*for any host, which you should almost never use
You can list several clients on one line, each with its own options:
/srv/nfs/projects 192.168.1.0/24(rw,sync,no_subtree_check) 192.168.2.15(ro)Important export options
| Option | Effect |
|---|---|
| `rw` / `ro` | Allow read-write or read-only access. `ro` is the default. |
| `sync` | The server replies to a write only after the data is on stable storage. This is the default in current versions. |
| `async` | The server may reply before data is written to disk. It is faster, but a crash can lose data that the client believed was saved. |
| `no_subtree_check` | Skips a check that the requested file lies inside the exported subtree. This is the default in current nfs-utils and avoids problems when files are renamed. |
| `root_squash` | Maps requests from the client's root user to an unprivileged user. This is the default. |
| `no_root_squash` | Lets the client's root act as root on the exported files. Use it only for trusted hosts. |
| `all_squash` | Maps every client user to the anonymous user. |
| `anonuid=N`, `anongid=N` | Set the user and group ID used for squashed requests. |
| `fsid=N` | Gives the export a fixed identifier. Needed for some filesystems and for the NFSv4 root. |
| `crossmnt` | Lets clients see filesystems mounted below the exported directory. |
Write sync and no_subtree_check explicitly even though they are defaults. If you leave them out, exportfs prints a warning on some versions. Explicit options also make the file self-documenting.
Squashing is covered in more detail in the identity mapping chapter. For now, remember that root_squash is on unless you turn it off, and that it only changes the identity of root. Other users keep their numeric IDs.
Preparing the directory
An export only shares what the filesystem allows. Create the directory and set ownership and permissions first:
sudo mkdir -p /srv/nfs/projects
sudo chown root:staff /srv/nfs/projects
sudo chmod 2775 /srv/nfs/projectsThe 2 in 2775 is the setgid bit. New files inherit the directory's group, which helps when several users share a directory.
The server checks permissions against numeric user and group IDs sent by the client. With the default sys security, the server trusts the client to report them honestly. A user whose UID is 1000 on the client is treated as UID 1000 on the server, whatever name that number has on either side.
Applying and checking exports
Edit /etc/exports, then re-read it:
# /etc/exports on the server
/srv/nfs/projects 192.168.1.0/24(rw,sync,no_subtree_check)
# apply and inspect the exports
sudo exportfs -ra
sudo exportfs -vThe flags of exportfs do the following:
-rre-exports everything, synchronizing the kernel table with the file.-aacts on all entries.-vprints verbose output, including the full set of options in effect.-uunexports an entry.exportfs -u 192.168.1.0/24:/srv/nfs/projectsstops sharing it until the next reload.
The output of exportfs -v lists the real options, including defaults you did not write, such as root_squash and wdelay. Check this output after every change. It shows what the kernel is using, which is not always what you meant to write.
You do not need to restart the NFS service after a change. Restarting would interrupt clients that have active mounts. Use exportfs -ra instead.
From a client, or from the server itself, showmount -e lists what is exported:
showmount -e fileservershowmount talks to the older mount daemon. On a server that offers only NFSv4, it may fail or show nothing, even though NFSv4 mounts work. Treat a successful showmount as a good sign, and a failure as inconclusive.
The NFSv4 view of exports
NFSv3 clients mount the full server path, such as /srv/nfs/projects. NFSv4 historically presented a single pseudo-filesystem tree rooted at /, with exports placed within it. Current Linux servers let a client mount fileserver:/srv/nfs/projects using the real path, which is why the mount command in the next chapter works without extra configuration.
If you want to build a custom NFSv4 tree, use an export marked fsid=0 as the root and bind-mount other directories below it. Most small setups do not need this.
Per-directory overrides and nesting
You can export a directory and a subdirectory with different options, as long as they are separate lines:
/srv/nfs/data 192.168.1.0/24(ro,sync,no_subtree_check)
/srv/nfs/data/upload 192.168.1.0/24(rw,sync,no_subtree_check)If both are on the same filesystem, the client's view depends on how it mounts them. Keep layouts simple. Put writable areas in their own directories or on their own filesystems where possible.
You cannot export a directory and also export its parent on the same filesystem to the same client with different options and expect the child's options to apply when the client mounts the parent. The client gets the parent's options.
Common errors
- Space before the parenthesis. Described above. Run
exportfs -vand check that the client column shows what you intended. - Changes not applied. You edited the file but did not run
exportfs -ra. - Permission denied on the client even though the export is
rw. The export controls network access. The directory's own mode and ownership still apply, using numeric IDs. - Root cannot write. This is
root_squashworking as designed. Fix the directory permissions rather than enablingno_root_squash. - Export of a path that does not exist.
exportfsreports an error and skips that line. - Exporting a filesystem that lacks a stable identifier. Some filesystems, such as certain FUSE or tmpfs mounts, need an explicit
fsid=.
Exercises
Exercise
A colleague writes /srv/data 10.0.0.0/24 (rw,sync) in /etc/exports. Who can write to the export, and how do you fix the line?
Show answer
The space makes 10.0.0.0/24 an entry with default options (read-only), and (rw,sync) applies to every host. Everyone on the network can therefore write. Remove the space: /srv/data 10.0.0.0/24(rw,sync,no_subtree_check). Then run sudo exportfs -ra and confirm with sudo exportfs -v.
Exercise
You changed an option in /etc/exports and saved the file. Which command applies it without disconnecting current clients, and which command shows the options the kernel is actually using?
Show answer
Run sudo exportfs -ra to apply the change, and sudo exportfs -v to display the active options, including defaults such as root_squash.