]> git.ipfire.org Git - thirdparty/systemd.git/blame - docs/USER_NAMES.md
Merge pull request #16518 from yuwata/network-fix-failure-in-updating-mac-address
[thirdparty/systemd.git] / docs / USER_NAMES.md
CommitLineData
b27cb676 1---
cafed7b3 2title: User/Group Name Syntax
5fe63895 3category: Users, Groups and Home Directories
cafed7b3
LP
4layout: default
5---
6
7# User/Group Name Syntax
8
9The precise set of allowed user and group names on Linux systems is weakly
10defined. Depending on the distribution a different set of requirements and
11restrictions on the syntax of user/group names are enforced — on some
12distributions the accepted syntax is even configurable by the administrator. In
13the interest of interoperability systemd enforces different rules when
14processing users/group defined by other subsystems and when defining users/groups
15itself, following the principle of "Be conservative in what you send, be
16liberal in what you accept". Also in the interest of interoperability systemd
17will enforce the same rules everywhere and not make them configurable or
18distribution dependent. The precise rules are described below.
19
20Generally, the same rules apply for user as for group names.
21
22## Other Systems
23
24* On POSIX the set of [valid user
25 names](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_437)
26 is defined as [lower and upper case ASCII letters, digits, period,
27 underscore, and
28 hyphen](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_282),
10eed984 29 with the restriction that hyphen is not allowed as first character of the
cafed7b3
LP
30 user name. Interestingly no size limit is declared, i.e. in neither
31 direction, meaning that strictly speaking according to POSIX both the empty
32 string is a valid user name as well as a string of gigabytes in length.
33
34* Debian/Ubuntu based systems enforce the regular expression
35 `^[a-z][-a-z0-9]*$`, i.e. only lower case ASCII letters, digits and
36 hyphens. As first character only lowercase ASCII letters are allowed. This
37 regular expression is configurable by the administrator at runtime
38 though. This rule enforces a minimum length of one character but no maximum
39 length.
40
41* Upstream shadow-utils enforces the regular expression
42 `^[a-z_][a-z0-9_-]*[$]$`, i.e. is similar to the Debian/Ubuntu rule, but
43 allows underscores and hyphens, but the latter not as first character. Also,
44 an optional trailing dollar character is permitted.
45
46* Fedora/Red Hat based systems enforce the regular expression of
47 `^[a-zA-Z0-9_.][a-zA-Z0-9_.-]{0,30}[a-zA-Z0-9_.$-]?$`, i.e. a size limit of
48 32 characters, with upper and lower case letters, digits, underscores,
49 hyphens and periods. No hyphen as first character though, and the last
50 character may be a dollar character. On top of that, `.` and `..` are not
51 allowed as user/group names.
52
53* sssd is known to generate user names with embedded `@` and white-space
54 characters, as well as non-ASCII (i.e. UTF-8) user/group names.
55
56* winbindd is known to generate user/group names with embedded `\` and
57 white-space characters, as well as non-ASCII (i.e. UTF-8) user/group names.
58
59Other operating systems enforce different rules; in this documentation we'll
60focus on Linux systems only however, hence those are out of scope. That said,
61software like Samba is frequently deployed on Linux for providing compatibility
62with Windows systems; on such systems it might be wise to stick to user/group
63names also valid according to Windows rules.
64
65## Rules systemd enforces
66
67Distilled from the above, below are the rules systemd enforces on user/group
68names. An additional, common rule between both modes listed below is that empty
69strings are not valid user/group names.
70
6b000af4
LP
71Philosophically, the strict mode described below enforces an allow list of
72what's allowed and prohibits everything else, while the relaxed mode described
73below implements a deny list of what's not allowed and permits everything else.
cafed7b3
LP
74
75### Strict mode
76
77Strict user/group name syntax is enforced whenever a systemd component is used
78to register a user or group in the system, for example a system user/group
79using
80[`systemd-sysusers.service`](https://www.freedesktop.org/software/systemd/man/systemd-sysusers.html)
81or a regular user with
82[`systemd-homed.service`](https://www.freedesktop.org/software/systemd/man/systemd-homed.html).
83
84In strict mode, only uppercase and lowercase characters are allowed, as well as
85digits, underscores and hyphens. The first character may not be a digit or
86hyphen. A size limit is enforced: the minimum of `sysconf(_SC_LOGIN_NAME_MAX)`
87(typically 256 on Linux; rationale: this is how POSIX suggests to detect the
88limit), `UT_NAMESIZE-1` (typically 31 on Linux; rationale: names longer than
89this cannot correctly appear in `utmp`/`wtmp` and create ambiguity with login
90accounting) and `FILENAME_MAX` (4096 on Linux; rationale: user names typically
91appear in directory names, i.e. the home directory), thus MIN(256, 31, 4096) =
9231.
93
94Note that these rules are both more strict and more relaxed than all of the
95rules enforced by other systems listed above. A user/group name conforming to
96systemd's strict rules will not necessarily pass a test by the rules enforced
97by these other subsystems.
98
99Written as regular expression the above is: `^[a-zA-Z_][a-zA-Z0-9_-]{0,30}$`
100
101### Relaxed mode
102
103Relaxed user/group name syntax is enforced whenever a systemd component accepts
104and makes use of user/group names registered by other (non-systemd)
105components of the system, for example in
106[`systemd-logind.service`](https://www.freedesktop.org/software/systemd/man/systemd-logind.html).
107
108Relaxed syntax is also enforced by the `User=` setting in service unit files,
109i.e. for system services used for running services. Since these users may be
110registered by a variety of tools relaxed mode is used, but since the primary
111purpose of these users is to run a system service and thus a job for systemd a
112warning is shown if the specified user name does not qualify by the strict
113rules above.
114
115* No embedded NUL bytes (rationale: handling in C must be possible and
116 straight-forward)
117
118* No names consisting fully of digits (rationale: avoid confusion with numeric
119 UID/GID specifications)
120
121* Similar, no names consisting of an initial hyphen and otherwise entirely made
122 up of digits (rationale: avoid confusion with negative, numeric UID/GID
123 specifications, e.g. `-1`)
124
125* No strings that do not qualify as valid UTF-8 (rationale: we want to be able
126 to embed these strings in JSON, with permits only valid UTF-8 in its strings;
127 user names using other character sets, such as JIS/Shift-JIS will cause
128 validation errors)
129
130* No control characters (i.e. characters in ASCII range 1…31; rationale: they
131 tend to have special meaning when output on a terminal in other contexts,
132 moreover the newline character — as a specific control character — is used as
133 record separator in `/etc/passwd`, and hence it's crucial to avoid
134 ambiguities here)
135
136* No colon characters (rationale: it is used as field separator in `/etc/passwd`)
137
138* The two strings `.` and `..` are not permitted, as these have special meaning
139 in file system paths, and user names are frequently included in file system
140 paths, in particular for the purpose of home directories.
141
142* Similar, no slashes, as these have special meaning in file system paths
143
144* No leading or trailing white-space is permitted; and hence no user/group names
145 consisting of white-space only either (rationale: this typically indicates
146 parsing errors, and creates confusion since not visible on screen)
147
148Note that these relaxed rules are implied by the strict rules above, i.e. all
149user/group names accepted by the strict rules are also accepted by the relaxed
150rules, but not vice versa.
151
152Note that this relaxed mode does not refuse a couple of very questionable
153syntaxes. For example it permits a leading or embedded period. A leading period
154is problematic because the matching home directory would typically be hidden
155from the user's/administrator's view. An embedded period is problematic since
156it creates ambiguity in traditional `chown` syntax (which is still accepted
157today) that uses it to separate user and group names in the command's
158parameter: without consulting the user/group databases it is not possible to
159determine if a `chown` invocation would change just the owning user or both the
86b52a39 160owning user and group. It also allows embedding `@` (which is confusing to
cafed7b3
LP
161MTAs).
162
163## Common Core
164
165Combining all rules listed above, user/group names that shall be considered
166valid in all systemd contexts and on all Linux systems should match the
167following regular expression (at least according to our understanding):
168
169`^[a-z][a-z0-9-]{0,30}$`