Sub topic 9
Exporting Directories over NFS
Assume you are comfortable with Linux permissions, IP addresses, systemctl, and editing configuration files. This chapter uses NFSv4 on a server and a Linux client. NFS, or Network File System, lets a client access a server directory as if it were part of the client’s local filesystem.
How NFS exports work
An NFS server does not share every directory automatically. It publishes selected directories as exports. Each export has:
- The directory to share.
- The clients allowed to connect.
- Access options such as read-only or read-write.
- Permission behavior for remote users, including root.
The /etc/exports file defines exports. A client network can be written with CIDR notation, such as 192.168.1.0/24.
# /etc/exports
/srv/nfs/projects 192.168.1.0/24(rw,sync,no_subtree_check)This configuration shares /srv/nfs/projects with clients on the 192.168.1.0/24 network. Clients can read and write files. sync makes the server commit changes before reporting success. This is safer during a failure, although it can be slower. no_subtree_check avoids checks that are unnecessary when exporting a complete filesystem subtree.
Create the directory and set its normal Linux ownership and permissions before exporting it:
sudo mkdir -p /srv/nfs/projects
sudo chown root:staff /srv/nfs/projects
sudo chmod 2775 /srv/nfs/projectsThe setgid bit, represented by the leading 2 in 2775, makes new files inherit the directory’s group. The NFS service still enforces the underlying Linux owner, group, and mode bits.
Applying and inspecting exports
After editing /etc/exports, reload the export table:
sudo exportfs -raThe -r option re-exports all directories. The -a option applies the operation to all exports. Inspect the active result:
sudo exportfs -vYou should see the path, allowed client network, and effective options. To see exports through the NFS discovery service from a client, run:
showmount -e fileservershowmount is useful for checking visibility, but NFSv4 clients can sometimes work even when this discovery output is unavailable.
Common export choices
Use ro for data that clients must not change. Use rw only when clients need to write. Restrict clients to specific IP addresses or networks instead of using a wildcard. Keep root_squash enabled unless you have a documented administrative reason to change it. With root squashing, remote root access is mapped to an unprivileged identity.
Exercise
What does sudo exportfs -ra do after you edit /etc/exports?
Show answer
It reloads all export definitions and applies the current rules to the running NFS server.