Stop Creating Users for Every Damn Daemon: Let Systemd Handle It
By Saket Jain Published Linux/Unix
Stop Creating Users for Every Damn Daemon: Let Systemd Handle It
Technical Briefing | 11/26/2026
How many times have you set up a new service, only to find yourself reaching for useradd and groupadd? You create app_svc, give it a home directory you’ll never use, set a shell nobody can log into, and then you try to remember if you need to chown some directories. It’s tedious, error-prone, and honestly, overkill for most daemons. Every extra persistent user on a system is another entry in /etc/passwd, another potential attack surface, however small. I’ve seen teams generate dozens of these just for simple internal tools, and it’s a mess to keep track of.
Systemd’s Answer to User Management Fatigue
Here’s where DynamicUser=yes comes in, and frankly, it radically simplifies things if you haven’t used it. When you enable this in your unit file, systemd won’t just run your service as an existing user; it’ll dynamically allocate a new, unused UID and GID pair from a specified range (typically 60001-65534 on modern distributions) every time the service starts. And get this: when the service stops, that user and group simply vanish. No persistent entry, no lingering resources, just a clean slate. It’s like a temporary anonymous identity for your daemon, and it makes service isolation much, much easier.
[Unit]
Description=My Ephemeral Service
After=network.target
[Service]
ExecStart=/bin/sh -c 'echo "Hello from $(id -un) (UID $(id -u))" > %S/message.txt && cat %S/message.txt && sleep infinity'
DynamicUser=yes
StateDirectory=my-ephemeral-service
[Install]
WantedBy=multi-user.target
Beyond the Basics: Where DynamicUser Shines (and Stumbles)
So, where does DynamicUser really fit? It’s phenomenal for services that are largely self-contained, or where their persistent data doesn’t need to be owned by a *specific*, unchanging UID/GID. Think stateless web services, queue consumers, background processing jobs that write logs or temporary caches. If your application needs to write persistent data, systemd has you covered there too. Directives like StateDirectory=, CacheDirectory=, and LogsDirectory= are your friends. Systemd creates these directories under /var/lib, /var/cache, and /var/log respectively, and automatically chowns them to your ephemeral user at service start. This is huge; it means your service can write its state without ever needing a manually provisioned, persistent user.
- Automatic, ephemeral user and group creation for services.
- No more manual useradd and chown for most simple daemons.
- Reduced attack surface: no persistent user means fewer potential targets for privilege escalation.
- Cleaner system: no orphaned users or groups after service removal.
- Seamless integration with StateDirectory= and friends for managed persistent data.
Now, it’s not a panacea. If you’ve got a database that needs its data directory owned by a specific, unchanging postgres or mysql user, DynamicUser=yes isn’t for you. But for a vast number of auxiliary services, API endpoints, or job processors, this directive simplifies your life immensely and inherently improves security. Think of it as another arrow in your systemd quiver, another way to isolate your services without sweating the small stuff. It’s one of those features that, once you start using it, you’ll wonder how you ever managed without it.
