You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/building/users-and-groups.md
+69-35Lines changed: 69 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,31 +55,45 @@ credentials from a [CRD](https://kubernetes.io/docs/tasks/extend-kubernetes/cust
55
55
hosted in the API server. (To do things like this
56
56
it's suggested to reuse the kubelet credentials)
57
57
58
-
### Adding users and credentials statically in the container build
58
+
### System users and groups (added via packages, etc)
59
59
60
-
Relative to package-oriented systems, a new ability is to inject
61
-
users and credentials as part of a derived build:
60
+
It is common for packages (deb/rpm/etc) to allocate system users
61
+
or groups as part of e.g `apt|dnf install <server package>` such as Apache or MySQL,
62
+
and this is often done by directly invoking `useradd` or `groupadd` as part
63
+
of package pre/post installation scripts.
62
64
63
-
```dockerfile
64
-
RUN useradd someuser
65
-
```
65
+
With the`shadow-utils` implementation of `useradd` and the default glibc `files` this will
66
+
result in changes to the traditional `/etc/passwd` and `/etc/shadow` files
67
+
as part of the container build.
66
68
67
-
However, it is important to understand some two very important issues
68
-
with this as it exists today (the `shadow-utils` implementation of `useradd`)
69
-
and the default glibc `files` backend for the traditional `/etc/passwd`
70
-
and `/etc/shadow` files.
69
+
#### System drift from local /etc/passwd modifications
71
70
72
-
It is common for user/group IDs are allocated dynamically, and this can result in "drift" (see below).
71
+
When the system is initially installed, the `/etc/passwd` in the container image will be
72
+
applied and contain desired users.
73
73
74
-
Further, if `/etc/passwd` is modified locally (because there is a machine-local user),
75
-
then any added users injected via `useradd`*will not appear* on subsequent updates by default (they will be
74
+
By default (without `etc = transient`, see below), the `/etc` directory is machine-local
75
+
persistent state. If subsequently `/etc/passwd` is modified local to the machine
76
+
(as is common for e.g. setting a root password) then any new changes in the container
77
+
image (such as users from new packages) *will not appear* on subsequent updates by default (they will be
76
78
in `/usr/etc/passwd` instead - the default image version).
77
79
78
-
These "system users" that may be created by packaging tools invoking `useradd` (e.g. `apt|dnf install httpd`) that do
79
-
not also install a `sysusers.d` file. Currently for example, this is the case with
80
-
the CentOS Stream 9 `httpd` package. Per below, the general solution to this
81
-
is to avoid invoking `useradd` in container builds, and prefer one of the below
82
-
solutions.
80
+
The general best fix for this is to use `systemd-sysusers` instead of allocating
81
+
a user/group at build time at all.
82
+
83
+
##### Using systemd-sysusers
84
+
85
+
See [systemd-sysusers](https://www.freedesktop.org/software/systemd/man/latest/systemd-sysusers.html).
86
+
For example in your derived build:
87
+
88
+
```
89
+
COPY mycustom-user.conf /usr/lib/sysusers.d
90
+
```
91
+
92
+
A key aspect of how this works is that `sysusers` will make changes
93
+
to the traditional `/etc/passwd` file as necessary on boot instead
94
+
of at build time. If `/etc` is persistent, this can avoid uid/gid drift (but
95
+
in the general case it does mean that uid/gid allocation can
96
+
depend on how a specific machine was upgraded over time).
83
97
84
98
#### User and group home directories and `/var`
85
99
@@ -96,20 +110,6 @@ This is significantly better than the pattern of allocating users/groups
96
110
at "package install time" (e.g. [Fedora package user/group guidelines](https://docs.fedoraproject.org/en-US/packaging-guidelines/UsersAndGroups/)) because
97
111
it avoids potential UID/GID drift (see below).
98
112
99
-
#### Using systemd-sysusers
100
-
101
-
See [systemd-sysusers](https://www.freedesktop.org/software/systemd/man/latest/systemd-sysusers.html). For example in your derived build:
102
-
103
-
```
104
-
COPY mycustom-user.conf /usr/lib/sysusers.d
105
-
```
106
-
107
-
A key aspect of how this works is that `sysusers` will make changes
108
-
to the traditional `/etc/passwd` file as necessary on boot. If
109
-
`/etc` is persistent, this can avoid uid/gid drift (but
110
-
in the general case it does mean that uid/gid allocation can
111
-
depend on how a specific machine was upgraded over time).
112
-
113
113
#### Using systemd JSON user records
114
114
115
115
See [JSON user records](https://systemd.io/USER_RECORD/). Unlike `sysusers`,
@@ -193,6 +193,23 @@ them; this is the pattern used by cloud-init and [afterburn](https://github.com/
193
193
194
194
### UID/GID drift
195
195
196
+
Any invocation of `useradd` or `groupadd` that does not allocate a *fixed* UID/GID may
197
+
be subject to "drift" in subsequent rebuilds by default.
198
+
199
+
One possibility is to explicitly force these user/group allocations into a static
200
+
state, via `systemd-sysusers` (per above) or explicitly adding the users with
201
+
static IDs *before* a dpkg/RPM installation script operates on it:
0 commit comments