Sandboxing services with systemd
Limit what a compromised service can touch with DynamicUser=, ProtectSystem=, PrivateTmp= and NoNewPrivileges=, then score it with systemd-analyze security.
Writing and overriding unit filesUsers, groups and the account files
Sandboxing services with systemd
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Sandboxing assumes the service will be broken into one day and limits what the attacker gets. It is a few lines in a unit file, enforced by the kernel.
- Run as a non-root user first. DynamicUser=yes allocates a throwaway UID per run and implies ProtectSystem=strict, ProtectHome=read-only, NoNewPrivileges=yes and a private /tmp.
- ProtectSystem=strict makes the whole filesystem read-only except /dev, /proc and /sys. Open holes with StateDirectory=, LogsDirectory= or ReadWritePaths=.
- PrivateTmp= gives the service its own /tmp and /var/tmp. ProtectHome= hides /home, /root and /run/user.
- NoNewPrivileges=yes stops the service and its children gaining privileges through setuid binaries or file capabilities.
- `systemd-analyze security` scores each service from 0 to 10 for exposure. It measures systemd settings only, not how safe the program is.
- Add settings one at a time and test. Most breakage is a path the service needs to write that you made read-only.
Assume the break-in
Your web service parses input from strangers. One day a bug in it, or in a library it uses, lets someone run code inside it. What happens next depends on what that process is allowed to do. If it runs as root with the whole filesystem writable, the attacker owns the machine. If it runs as a throwaway user that can see nothing but its own data directory, cannot write to /usr or /etc, cannot load kernel modules and cannot gain privileges, the attacker owns one small corner.
That is the idea behind sandboxing: reduce the blast radius before you know where the bug is. A hotel key card is the analogy. It opens your room and the gym. A thief who steals it does not get the other floors.
On Linux you could build this from namespaces, capabilities and seccomp by hand. systemd exposes them as unit settings, so hardening a service is a drop-in file of a dozen lines.
How much real units use it
I downloaded Debian 13’s systemd 257.13 package and unpacked it on this Mac, then counted how many of its 111 service files set each option:
$ for k in DynamicUser ProtectSystem ProtectHome PrivateTmp NoNewPrivileges SystemCallFilter User; do
printf "%s " $k; grep -l "^$k=" *.service | wc -l; done
DynamicUser 1
ProtectSystem 9
ProtectHome 9
PrivateTmp 8
NoNewPrivileges 13
SystemCallFilter 11
User 4
So even in systemd’s own package, most units are not sandboxed. Many of them are early-boot or one-shot units that must touch the whole system. The ones that are long-running daemons are. Here is systemd-timesyncd.service from the companion package, trimmed to its sandboxing:
[Service]
User=systemd-timesync
AmbientCapabilities=CAP_SYS_TIME
CapabilityBoundingSet=CAP_SYS_TIME
NoNewPrivileges=yes
PrivateDevices=yes
PrivateTmp=disconnected
ProtectHome=yes
ProtectSystem=strict
ProtectKernelModules=yes
ProtectKernelTunables=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
StateDirectory=systemd/timesync
SystemCallFilter=@system-service @clock
A time daemon needs one privilege, setting the clock, so it runs as its own user with only CAP_SYS_TIME, can write only its state directory, and can only open Unix and IP sockets.
The settings, from the outside in
All definitions here are from systemd.exec(5) for systemd 262.
Who it runs as. User= and Group= drop root. DynamicUser=yes goes further: a user and group are allocated when the unit starts, from UID range 61184 to 65519, and released when it stops. They never appear in /etc/passwd. It also implies ProtectSystem=strict, ProtectHome=read-only, NoNewPrivileges=yes and a private /tmp (disconnected). NoNewPrivileges= cannot be turned back off for such a unit. Because the UID is recycled, persistent data must live in StateDirectory=, which systemd manages for it.
What it can write. ProtectSystem=yes makes /usr, /boot and /efi read-only. full adds /etc. strict makes the entire filesystem read-only except /dev, /proc and /sys. The man page recommends it for all long-running services. Then open exactly what is needed:
[Service]
ProtectSystem=strict
StateDirectory=myapp # /var/lib/myapp, writable, owned by the service user
LogsDirectory=myapp # /var/log/myapp
ReadWritePaths=/srv/uploads
What it can see. ProtectHome=yes makes /home, /root and /run/user empty and inaccessible. PrivateTmp=yes gives the service its own /tmp and /var/tmp, removed when it stops, which ends the old class of attacks where one service plants files in another’s temporary directory. PrivateDevices=yes hides physical devices.
Whether it can climb. NoNewPrivileges=yes means no process in the service can gain privileges through execve(), whether by a setuid binary like sudo or by file capabilities. The man page calls it the simplest and most effective way to make sure a process can never elevate again. CapabilityBoundingSet= limits the root powers it could ever hold, covered in the capabilities lesson.
What it can ask the kernel. ProtectKernelModules=, ProtectKernelTunables= and ProtectControlGroups= block loading modules and writing /proc/sys and the cgroup tree. SystemCallFilter=@system-service allows a curated set of system calls and blocks the rest. RestrictAddressFamilies= limits socket types.
Most of these are built on mount namespaces, the topic of the namespaces lesson. A sandboxed service sees a filesystem arranged just for it.
Measuring it
systemd-analyze security with no argument lists every running service with an exposure score from 0.0 to 10.0. With a unit name it lists each check. The man page’s own example for systemd-logind.service:
NAME DESCRIPTION EXPOSURE
✗ PrivateNetwork= Service has access to the host's network 0.5
✗ User=/DynamicUser= Service runs as root user 0.4
✗ DeviceAllow= Service has no device ACL 0.2
✓ IPAddressDeny= Service blocks all IP address ranges
...
→ Overall exposure level for systemd-logind.service: 4.1 OK 🙂
That is quoted from the documentation, not run here. The man page is careful about what the number means: it only covers settings systemd itself applies. A high score does not mean the service is vulnerable, and a low one does not mean it is safe. It is a to-do list. --offline=true scores a unit file without loading it, and --threshold= makes it return an error above a score, which fits a CI check on unit files.
Doing it without breaking things
Hardening a working service is iterative:
systemctl edit myapp.serviceand add a few settings, starting withNoNewPrivileges=yes,PrivateTmp=yes,ProtectHome=yesandProtectSystem=full.- Restart it and exercise it. Read
journalctl -u myappfor permission errors. - Move to
ProtectSystem=strictand addStateDirectory=orReadWritePaths=for each path it writes. - Rerun
systemd-analyze security myapp.serviceand pick the next item.
The common failure is EROFS or “Read-only file system” from a path you forgot. The error names the path, so the fix is usually one line.
About the video
The embed is the whole of “Using systemd features to secure services”, a DevConf.CZ 2020 talk by Zbigniew Jędrzejewski-Szmek, a systemd maintainer, 34 minutes. Its description lists the ground: systemd doing privileged setup so the service needs no privileges, running as unprivileged users, using namespaces, capabilities and resource limits to constrain services, and BPF-based firewalls and device filters. It is the most authoritative source I found, but it is from 2020. Settings added since, such as PrivateTmp=disconnected, are not in it, and the talk has no chapters, so I judged it from the description. English was judged from the title, speaker and English auto-caption track. The SecureRandom video in deeper is a shorter hands-on walk-through.
What was run: downloading and unpacking Debian 13’s systemd and systemd-timesyncd packages and counting directives with grep on macOS. Nothing was started under systemd. Setting behaviour is from the systemd 262 man pages, checked 2026-11-28.
Where this fits
That ends services and logs. Next, the processes those services become, as the kernel shows them: processes through /proc.
Check yourself
You add ProtectSystem=strict to a service and it fails writing to /var/lib/myapp. What are two fixes?
Add StateDirectory=myapp, which creates /var/lib/myapp owned by the service user and keeps it writable, or ReadWritePaths=/var/lib/myapp.
What does DynamicUser=yes give you beyond User=someuser?
A UID that is allocated when the service starts and released when it stops, with no entry in /etc/passwd, plus implied hardening: ProtectSystem=strict, ProtectHome=read-only, NoNewPrivileges=yes and a private /tmp, among others.
A service runs as an unprivileged user but could exec a setuid-root helper. Which single setting closes that path?
NoNewPrivileges=yes. Processes in the service can no longer gain privileges through execve() of setuid, setgid or capability-carrying binaries.
systemd-analyze security rates your service 9.6 UNSAFE. Does that mean it is vulnerable?
No. It means systemd applies almost no sandboxing to it. The program may be safe, but if it is compromised nothing limits the damage.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.