Sub topic 11
UID/GID mapping and root squash
NFS does not send user names to identify who owns a file. It sends numbers. This chapter explains how those numbers are interpreted, why mismatches cause confusing permission errors, and what root_squash does to protect the server.
This chapter assumes you can export and mount an NFS directory (covered in the previous two chapters), and that you know how Linux users, groups, and chmod/chown permissions work.
How NFS identifies users
Every file on a Linux filesystem is owned by a numeric user ID (UID) and group ID (GID). Names like alice are only a lookup in /etc/passwd and /etc/group.
With the default sec=sys authentication, an NFS client sends the UID and GID of the calling process with each request. The server does not check a password. It trusts the client and applies normal permission checks to those numbers.
This has one important consequence. Permissions follow the number, not the name. If alice is UID 1000 on the client and UID 1001 on the server, the server treats her requests as UID 1000, whoever that is on the server.
Seeing the mismatch
Suppose the server has these accounts:
| Account | Server UID |
|---|---|
| alice | 1001 |
| bob | 1000 |
On the client, alice is UID 1000. Alice creates a file in the mounted share. On the server, the file belongs to UID 1000, which is bob.
# On the client, as alice (UID 1000)
touch /mnt/projects/notes.txt
ls -ln /mnt/projects/notes.txt
# On the server
ls -l /srv/nfs/projects/notes.txtThe client shows 1000 1000. The server shows bob bob. Nothing is broken. Both machines are reporting the same number through different name lookups.
Use ls -n and id when debugging. They show the numbers that actually matter.
id alice
getent passwd 1000Run these on both machines and compare.
Fixing mismatches
There are three common approaches.
- Keep UIDs and GIDs consistent. Create users with the same numeric IDs on every machine. This works for a few hosts. Use
useradd -u 2001 aliceandgroupadd -g 2000 staffexplicitly. - Use a central directory. LDAP or SSSD with a directory service gives every machine the same IDs. This is the standard approach for larger sites.
- Force the mapping on the server. The
all_squashoption withanonuidandanongidmaps every client user to a single server account.
The third option suits a shared drop directory where individual ownership does not matter:
/srv/nfs/dropbox 192.168.1.0/24(rw,sync,no_subtree_check,all_squash,anonuid=2500,anongid=2500)Every request is handled as UID 2500 and GID 2500, so make sure that account owns the directory.
sudo groupadd -g 2500 nfsdrop
sudo useradd -M -u 2500 -g 2500 -s /usr/sbin/nologin nfsdrop
sudo chown nfsdrop:nfsdrop /srv/nfs/dropbox
sudo exportfs -raWhat root squash does
The root user (UID 0) can read and write any file. If the server trusted a client's claim of UID 0, anyone with root on any client machine could modify any file on the export, including files owned by other users or root.
root_squash prevents this. The server rewrites requests from UID 0 to the anonymous user. It does the same for GID 0. This is the default, so you get it even without writing the option.
| Option | Effect |
|---|---|
| `root_squash` | Client root becomes the anonymous user. This is the default. |
| `no_root_squash` | Client root stays UID 0 on the server. |
| `all_squash` | Every client user becomes the anonymous user. |
| `anonuid=N`, `anongid=N` | Set the IDs used for the anonymous user. |
The anonymous user defaults to nobody (often UID 65534). On some systems it appears as nfsnobody.
Observing it
Check what the server is using:
sudo exportfs -vIn the output, root_squash appears among the options even if you did not write it. The same output also shows wdelay, hide, and other defaults.
Now test from a client:
sudo touch /mnt/projects/rootfile
ls -ln /mnt/projects/rootfileThe file shows UID 65534, not 0. If the directory is not writable by nobody, touch fails with Permission denied, even for root. This surprises many administrators. Root on the client is not root on the share.
Squashing and file ownership
Squashing changes how requests are interpreted. It does not change files that already exist. A file owned by root on the server stays owned by root. A squashed client user cannot modify it unless the mode bits allow access to others.
A common cause of "Permission denied" for root on a client is a directory with mode 0755 owned by a regular user. Root is squashed to nobody, which falls under "others" and has no write permission.
Group handling and the 16-group limit
With sec=sys, the protocol carries a UID, a primary GID, and a list of supplementary GIDs. The RPC format limits that list to 16 groups. If a user belongs to more than 16 groups, extra groups are dropped and permissions based on them silently fail.
Modern servers avoid this by looking up group membership locally. The rpc.mountd daemon can do this with the manage-gids option, which you set in /etc/nfs.conf:
[mountd]
manage-gids = yWith this set, the server ignores the group list from the client and resolves groups itself from the server's own user database. Group membership then must be defined on the server.
NFSv4 and ID mapping by name
NFSv4 changes how owners are represented in metadata. It sends owners as strings in the form user@domain. The client and server translate between names and numbers using an ID mapper (nfsidmap on the client, with the domain set by Domain in /etc/idmapd.conf or /etc/nfs.conf).
There are two cases to know.
- With
sec=syson current kernels, the actual permission checks still use numeric UIDs and GIDs in the RPC credentials. The name mapping only affects whatls -ldisplays and whatchownsends. - With Kerberos (
sec=krb5), the server authenticates a principal and maps it to a local account, so numbers on the client no longer decide access.
If the domain strings differ between client and server, owners often appear as nobody or 4294967294 in listings. Set the same domain on both sides:
[general]
Domain = example.comThen clear the client's cached mappings:
sudo nfsidmap -cRemount if the display does not change.
On modern kernels, if the client and server both use numeric IDs only for sec=sys, you can also see nfs4_disable_idmapping behave as enabled. In that case numeric strings are exchanged directly, and mismatched domains cause no problem. This is a common reason that one pair of machines works and another does not.
Cheat sheet for diagnosing ownership problems
- Run
ls -lnon the client andls -lnon the server. Compare the numbers. - Run
id useron both machines. - Check
exportfs -vforroot_squash,all_squash,anonuid, andanongid. - For NFSv4 names showing as
nobody, compareDomainon both sides. - If a user in many groups loses access, check for the 16-group limit and consider
manage-gids.
Exercise
A client's root user runs touch /mnt/projects/test on an export with default options. The directory on the server is owned by bob:staff with mode 0755. The command fails with "Permission denied". Explain why, and name two ways to let the file be created.
Show answer
The default root_squash maps client UID 0 to the anonymous user (nobody). That user is "others" for the directory, and mode 0755 gives others no write permission, so the write is denied. One fix is to run the command as a user who can write, such as bob (with matching UIDs on both machines) or a member of staff if the directory is group-writable (mode 0775 or 2775). Another is to change the directory's ownership or mode on the server so the intended user can write. Using no_root_squash would also work but is unsafe and should be avoided.
Exercise
On the server, alice has UID 1001. On a client, alice has UID 1000, and a different user carol has UID 1001. Alice creates a file in the share from the client. Whose file does the server consider it to be, and how would you confirm this?
Show answer
The server sees UID 1000 in the request, so the file is owned by whichever server account has UID 1000, not by alice. To confirm, run ls -ln on the new file on both machines to see the numeric owner, then run getent passwd 1000 on the server. The fix is to give alice the same UID on both machines, or to use a central directory service such as LDAP or SSSD.