Sub topic 3
Subordinate UID and GID Ranges
This chapter assumes you know the previous chapter's idea of a user namespace. It also assumes you can read /etc/passwd and run basic shell commands. Here you will learn what the files /etc/subuid and /etc/subgid contain, why rootless Docker needs them, and how to check them.
Why one UID is not enough
A user namespace maps user IDs (UIDs) inside it to UIDs outside it. An unprivileged process can map exactly one ID: its own. That is enough to make a process believe it is root, but not enough to run a container. Images contain files owned by many users, such as root, www-data and nobody. Those users need distinct UIDs inside the container.
Subordinate ID ranges fill this gap. The system administrator assigns each user a block of extra UIDs and GIDs that no real account uses. The user may map those IDs into their own namespaces.
The file format
Each line in /etc/subuid and /etc/subgid has three colon-separated fields: the user name (or numeric UID), the first ID in the range, and the number of IDs.
alice:100000:65536This line gives alice the host IDs 100000 through 165535. Rootless Docker needs at least 65536 IDs in both files, because that is the full range of a traditional 16-bit UID space.
How the mapping works
Suppose alice has UID 1000. The rootless daemon's namespace maps IDs as follows:
| ID inside the namespace | ID on the host |
|---|---|
| 0 (root) | 1000 (alice) |
| 1 to 65536 | 100000 to 165535 |
Container root is therefore alice on the host. A container process running as UID 33 appears on the host as UID 100032, which owns nothing and has no privileges.
Who writes the mapping
Only a privileged program can map more than one ID. Two small setuid helpers do this on behalf of the user: newuidmap and newgidmap. They read /etc/subuid and /etc/subgid, confirm the requested range belongs to the caller, and write the mapping. They usually come in the uidmap package. The next chapter shows how RootlessKit calls them.
Checking your ranges
Run these commands as the user who will run the daemon:
# Check that the current user has subordinate ID ranges
grep "^$(whoami):" /etc/subuid /etc/subgid
# Check that the helper binaries are installed
command -v newuidmap newgidmapEach file should print one line for your user with a count of at least 65536. Both helpers should print a path.
Once the daemon is running, you can see the live mapping from inside a container:
docker run --rm alpine cat /proc/self/uid_mapThe output has three columns: the first ID inside, the first ID outside, and the length. Expect a line like 0 1000 1 followed by 1 100000 65536.
Common errors
- No entry for your user. The installer fails with a message about missing subuid or subgid. Add a range with
sudo usermod --add-subuids 100000-165535 --add-subgids 100000-165535 alice. - Range too small. Fewer than 65536 IDs causes image pulls to fail with
lchownerrors when a layer contains a file owned by a high UID. - Overlapping ranges. If two users share IDs, one user's container files may be accessible to the other. Keep ranges disjoint.
- Users from a directory service. Accounts supplied by LDAP or SSSD may not get automatic entries. Add them to the files yourself.
Exercise
A user has the line bob:200000:65536 in /etc/subuid and UID 1500. On the host, which UID owns a file created by container UID 5 in bob's rootless daemon?
Show answer
Container UID 0 maps to host UID 1500, and container UIDs 1 and above map to 200000 onward. Container UID 5 is the fifth subordinate ID, so the host UID is 200000 + 4 = 200004.