Sub topic 11
UID/GID Mapping and Root Squash
This chapter assumes you understand Linux users, groups, file permissions, NFS exports, and client mounts. You should also know how to use id, ls -l, and systemctl.
NFS does not normally authenticate a user with a username and password. The client sends the user and group identity associated with each request. The server then checks that identity against the ownership and permissions of the exported files.
How NFS identifies users
With traditional NFS authentication, the client sends numeric user and group IDs:
- A UID identifies a user.
- A GID identifies a group.
- File ownership is stored as numeric UID and GID values.
- The username shown by
lscomes from the local system's account database.
For example, if both systems have a user with UID 1001, files owned by UID 1001 appear to belong to that user on both systems. The usernames do not need to match, but matching IDs make the result understandable and predictable.
# Run on both the client and the server
id alice
getent passwd alice
getent group staff
# Inspect numeric ownership
ls -ln /srv/nfs/projectsNFSv4 can use name-based identity mapping through rpc.idmapd, especially for displayed ownership. The client and server must use a compatible NFSv4 domain. Even then, you should manage accounts consistently. Identity mapping does not replace access control.
Root squashing
root_squash protects the server from root access arriving through an NFS client. When a client sends requests as UID 0, the server maps those requests to an anonymous account instead of treating them as server root.
Linux commonly maps the request to nobody or nfsnobody. The exact account depends on the distribution.
| Export option | Result |
|---|---|
| `root_squash` | Client root becomes the anonymous identity |
| `no_root_squash` | Client root remains root on the server |
| `all_squash` | Every client user becomes the anonymous identity |
| `anonuid=UID,anongid=GID` | Selects the anonymous UID and GID |
root_squash is normally the safe default. Avoid no_root_squash unless you have a specific, controlled requirement such as a trusted diskless-client environment. A root user on a client may still read or modify files that the anonymous identity can access. Root squashing limits privilege; it does not make an export private.
A practical export
This export allows clients from one subnet to write files while retaining root squashing:
/srv/nfs/projects 192.168.1.0/24(rw,sync,root_squash,no_subtree_check)Apply the change and inspect the effective configuration:
sudo exportfs -ra
sudo exportfs -vIf several servers need shared ownership, create the same group with the same GID on each system, or use centralized identity management. Then set the directory's group and the setgid bit so new files inherit that group:
sudo chgrp staff /srv/nfs/projects
sudo chmod 2775 /srv/nfs/projectsThe setgid bit on a directory is represented by the 2 in 2775.
Exercise
A client administrator runs touch as root, but the file cannot be created in an NFS export. What should you check first?
Show answer
Check the export's root_squash behavior, the anonymous account's permissions, the directory ownership and mode, and the server logs. Root on the client is not automatically root on the NFS server.