Securing background daemons and network microservices on Ubuntu Server historically required system administrators to provision dedicated, unprivileged system accounts (such as www-data, nginx, or redis) using useradd or adduser. Over time, as servers host dozens of disparate services, local user databases (/etc/passwd and /etc/shadow) become cluttered with orphaned system user IDs (UIDs) and shared group memberships that persist even after applications are removed.
Modern versions of systemd on Ubuntu include a security mechanism named Dynamic Users. By adding a single configuration directive—DynamicUser=yes—to a service unit file, systemd automatically allocates a transient, completely isolated user and group ID when the service starts, locks the process inside a hardened sandbox, and automatically destroys the allocated user identity the moment the service stops.
How systemd Dynamic Users Function
When a systemd service configured with dynamic users executes, the operating system applies rigorous security guarantees:
- Transient UID/GID Allocation: systemd assigns an unused user ID from the reserved dynamic UID range (
61184to65519) without modifying/etc/passwdor/etc/group. - Automatic Isolation: Enabling
DynamicUser=yesimplicitly activates multiple systemd sandboxing directives, includingProtectSystem=strict,ProtectHome=yes,NoNewPrivileges=yes, andPrivateTmp=yes. - Zero File Ownership Persistence: Because the dynamic UID is temporary, the service cannot own persistent files across regular filesystems. If persistent storage is required, systemd provisions managed runtime and state directories.
Configuring a Service Unit with Dynamic Users
To demonstrate dynamic user configuration, create a systemd unit file for a custom Node.js, Go, or Python microservice:
- Create a new service definition file inside
/etc/systemd/system/:
sudo nano /etc/systemd/system/api-service.service
- Add the following service configuration:
[Unit]
Description=High Security API Microservice
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/python3 /opt/api/server.py
Restart=always
# Enable Transient Dynamic User Isolation
DynamicUser=yes
# Provision persistent application storage safely
StateDirectory=api-service
RuntimeDirectory=api-service
[Install]
WantedBy=multi-user.target
- Save the file and reload the systemd manager configuration:
sudo systemctl daemon-reload
Starting and Verifying Transient Process Credentials
Start the newly created service and inspect its operating credentials:
sudo systemctl start api-service.service
sudo systemctl status api-service.service
To verify the dynamic UID assigned to the running process:
# Inspect process credentials using ps
ps -eo pid,user,group,comm | grep python3
You will see that the service runs under a dynamically generated user label (such as 61245), confirming that it operates without root or permanent system user privileges.
Managing Persistent Data with StateDirectory
Because dynamic users are destroyed on service shutdown, standard file ownership can leave orphaned files that the next dynamically allocated UID cannot access. The StateDirectory=api-service directive solves this problem:
- systemd creates a dedicated directory at
/var/lib/api-service/. - Every time the service starts, systemd recursively re-chowns this folder to match the newly assigned dynamic UID.
- Upon shutdown, the directory remains preserved on disk, ready for the next allocated UID during the next launch cycle.
Checking Sandboxing Security with systemd-analyze
You can evaluate the security exposure of your service using the built-in systemd security audit tool:
systemd-analyze security api-service.service
Services leveraging DynamicUser=yes routinely score in the safest green exposure zone (below 2.0 OK), drastically minimizing the attack surface against remote privilege escalation exploits on Ubuntu Server.