Linux Command Reference ยท Systemd and Logging (Read-Only)
systemd service sandboxing
Harden systemd services with ProtectSystem, ProtectHome, PrivateTmp, NoNewPrivileges, ReadWritePaths, CapabilityBoundingSet and systemd-analyze security.
systemd can confine a service with a handful of one-line settings in its unit file. They use kernel features (mount namespaces, capabilities, seccomp) without any extra software, so adding them is one of the cheapest ways to limit the damage if a daemon is compromised: a web server that cannot see /home or write to /usr is a much worse target.
The settings go in the [Service] section. They need a real systemd, so this page is read-only; the examples score and compare sample unit files to show how to read a hardening list.
[Service] ExecStart=/usr/local/bin/api DynamicUser=yes NoNewPrivileges=yes ProtectSystem=strict ProtectHome=yes PrivateTmp=yes ReadWritePaths=/var/lib/api
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
# directives in [Service] NoNewPrivileges=yes ProtectSystem=yes|full|strict ProtectHome=yes|read-only|tmpfs PrivateTmp=yes PrivateDevices=yes PrivateNetwork=yes ReadWritePaths=PATH... ReadOnlyPaths=PATH... InaccessiblePaths=PATH... CapabilityBoundingSet=CAP_NET_BIND_SERVICE AmbientCapabilities=CAP_NET_BIND_SERVICE SystemCallFilter=@system-service RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX ProtectKernelTunables=yes ProtectKernelModules=yes ProtectControlGroups=yes DynamicUser=yes StateDirectory=NAME RuntimeDirectory=NAME
| Name | Description |
|---|---|
NoNewPrivileges= |
The process and its children can never gain privileges (blocks setuid binaries and file capabilities). |
ProtectSystem= |
Mount parts of the file system read-only for the service: yes (/usr, /boot), full (adds /etc), strict (everything). |
ProtectHome= |
Make /home, /root and /run/user empty (yes), read-only, or hidden behind a tmpfs. |
PrivateTmp= |
Give the service its own /tmp and /var/tmp, invisible to other programs. |
PrivateDevices= |
Provide a minimal /dev without real hardware devices. |
PrivateNetwork= |
Only a loopback interface; no external networking. |
ReadWritePaths=, ReadOnlyPaths=, InaccessiblePaths= |
Punch holes in or add to the protections for specific paths. |
CapabilityBoundingSet= |
The only capabilities the service may ever hold; AmbientCapabilities= hands some to a non-root user. |
SystemCallFilter= |
Allow only listed system calls; @system-service is a ready-made group for ordinary daemons. |
RestrictAddressFamilies= |
Limit which socket families the service may create. |
DynamicUser= |
Allocate a throwaway user and group for each run; implies several of the protections above. |
StateDirectory= and friends |
Create /var/lib/NAME (and /run, /var/log, /var/cache equivalents) owned by the service user. |
Returns: systemd-analyze security UNIT prints each setting with an exposure score and an overall rating from 0.0 (safe) to 10.0 (UNSAFE).
Errors and edge cases: a service that now cannot write somewhere fails with "Read-only file system" or "Permission denied"; check the journal (journalctl filters) and add the path to ReadWritePaths=. Paths that do not exist make a unit fail to start unless prefixed with -.
Usage notes
- Start strict, then loosen. Add
ProtectSystem=strictandPrivateTmp=yes, run the service, and open only what the log complains about. DynamicUser=yesis a shortcut. It gives a private UID,ProtectSystem=strict,PrivateTmpandNoNewPrivilegesautomatically; useStateDirectory=for persistent data.- Binding port 80 as a normal user needs
AmbientCapabilities=CAP_NET_BIND_SERVICE(with a matchingCapabilityBoundingSet=), not root. - These are not containers. A sandboxed service still shares the kernel and PID namespace unless you add
PrivateUsersorProtectProc=; treat the settings as defense in depth. - Measure with
systemd-analyze security, ideally with the unit name, and aim to lower the score, not to reach zero: see systemd-analyze. - Combine with least privilege. Run as a dedicated
User=and keep secrets inLoadCredential=or files with mode 600. - Old systemd versions lack some options (
ProtectProc=,RestrictFileSystems=); unknown directives are ignored with a warning, so checksystemctl statusafter a change.