Service Accounts & Groups
Every service should run as its own user. If the inventory API is compromised, the attacker gets the rights of inventory, not of root and not of the www-data account that also serves three other sites. Users & Groups covered /etc/passwd, id and sudo from a user's point of view; this lesson is about the accounts nobody logs in to.
What makes an account a service account
| Property | Human user | Service account |
|---|---|---|
| UID range | from UID_MIN (1000) |
below it: SYS_UID_MIN..SYS_UID_MAX (usually 100–999) |
| Login shell | /bin/bash |
/usr/sbin/nologin (or /bin/false) |
| Password | set, or locked with keys only | locked (! or * in /etc/shadow) |
| Home directory | /home/ada |
the service's data directory (/var/lib/inventory) or /nonexistent |
| Created by | an admin, an identity system (LDAP, SSO) | the package, the deploy tool, or systemd-sysusers |
Here is what a fresh Ubuntu system looks like. Note which system accounts still have a real shell:
getent passwd www-data nobody learner
grep -E '^(SYS_)?UID_(MIN|MAX)' /etc/login.defs
echo "--- system accounts with a usable shell:"
awk -F: '$3 < 1000 && $7 !~ /(nologin|false)$/ {print $1, $3, $7}' /etc/passwdwww-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
learner:x:1000:1000:learner:/home/learner:/bin/bash
UID_MIN 1000
UID_MAX 60000
--- system accounts with a usable shell:
root 0 /bin/bash
sync 4 /bin/sync
root needs its shell; sync is a historical oddity whose "shell" is /bin/sync. Anything else in that list on a production server deserves a question.
Creating service accounts
On a server you have three ways (sample sessions; useradd needs root):
$ sudo useradd --system --home-dir /var/lib/inventory --create-home \
--shell /usr/sbin/nologin --user-group inventory
$ getent passwd inventory
inventory:x:998:998::/var/lib/inventory:/usr/sbin/nologin
$ sudo passwd -S inventory
inventory L 2026-01-15 0 99999 7 -1--system picks a UID from the system range, --user-group creates a group of the same name, and the L in passwd -S means the password is locked. On Debian/Ubuntu, adduser --system --group inventory does the same with friendlier defaults.
The declarative way is systemd-sysusers: a small config file in /usr/lib/sysusers.d/ or /etc/sysusers.d/ describes the accounts, and the tool creates whatever is missing, at boot or when a package installs. It is idempotent, so it suits images and configuration management. It can also work on an alternative root directory (as image builders do), which lets us run it for real here, on a miniature /etc:
mkdir -p root/etc/sysusers.d
printf 'root:x:0:0:root:/root:/bin/bash\n' > root/etc/passwd
printf 'root:x:0:\nadm:x:4:\n' > root/etc/group
cat > root/etc/sysusers.d/inventory.conf <<'EOF'
#Type Name ID GECOS Home directory
u inventory - "Inventory API" /var/lib/inventory
m inventory adm
EOF
systemd-sysusers --root="$PWD/root"
echo "--- passwd"; cat root/etc/passwd
echo "--- group"; cat root/etc/group
ls -l root/etc/shadow | cut -c1-10Creating group 'inventory' with GID 999.
Creating user 'inventory' (Inventory API) with UID 999 and GID 999.
--- passwd
root:x:0:0:root:/root:/bin/bash
inventory:x:999:999:Inventory API:/var/lib/inventory:/usr/sbin/nologin
--- group
root:x:0:
adm:x:4:inventory
inventory:x:999:
----------
u creates a user (and a group of the same name, with - meaning "pick a free system ID"), m adds it to the adm group, which on Debian/Ubuntu may read most of /var/log. The shell defaults to nologin for system users, and the new shadow file is created with no permissions at all (only root, which ignores them, can read it). Running the tool again changes nothing, because the accounts already exist; that idempotence is why packages call it on every install.
The third way avoids a permanent account altogether: DynamicUser=yes in the unit (Sandboxing a Service) allocates a UID when the service starts and releases it when it stops. Persistent data then goes into StateDirectory=, which systemd keeps owned by whatever UID the service gets next time.
Groups as permissions
Groups are how you grant one account access to another's files without making them the owner:
| Group (Debian/Ubuntu) | Grants |
|---|---|
adm |
read most logs in /var/log |
systemd-journal |
read the whole journal |
sudo (wheel on RHEL) |
use sudo (through a rule in /etc/sudoers) |
docker |
talk to the Docker daemon, which is root-equivalent |
www-data |
the web server's files |
ssl-cert |
read private keys in /etc/ssl/private |
Two rules: add a service account to the fewest groups it needs, and never add a human to docker "to save typing sudo" without understanding that it gives them root. Group membership changes only take effect in new sessions; for a service that means a restart.
$ sudo usermod -aG ssl-cert inventory # -a: append; without it the user leaves every other group!
$ sudo systemctl restart inventory
$ id inventory
uid=998(inventory) gid=998(inventory) groups=998(inventory),110(ssl-cert)Test yourself
Why should each service have its own account?
- It makes logs prettier
- A compromise of one service only gets that account's rights, not root's or another service's
- systemd requires it
- To save disk space
What does usermod -G ssl-cert inventory (without -a) do?
- Adds ssl-cert to the user's groups
- Replaces all of the user's supplementary groups with just ssl-cert
- Creates the group
- Nothing without -a
Which statement about systemd-sysusers is true?
- It deletes unused accounts
- It creates the accounts described in sysusers.d files if they don't exist, and does nothing on a second run
- It only works for human users
- It needs a running systemd