Who this is for: anyone who has written a .service file to run their own program on a Linux server: a self-hosted app, an API, a worker, a bot, a game server. The typical unit is five lines long, with an ExecStart= and a Restart=always. It runs as root with full access to the filesystem, every device and every system call. If the program has a bug an attacker can reach, they reach the whole machine.
systemd can wall a service off with a few lines in a drop-in, and it costs nothing at run time. The catch is that each setting can break a service that needs what it takes away, so the work is adding settings one at a time and watching the journal.
The order to do it in
- Get secrets out of
Environment=. The systemd.exec manual says environment variables are not suitable for secrets: a unit's environment is visible to unprivileged users over D-Bus (systemctl show) and is inherited by every child process. UseLoadCredential=name:/etc/app/nameand read$CREDENTIALS_DIRECTORY/name, or anEnvironmentFile=owned by root with mode 0600. - Stop running as root.
DynamicUser=yesis the shortest route: systemd creates a user for the life of the service and, per the manual, also turns onNoNewPrivileges=andRestrictSUIDSGID=, impliesProtectSystem=strictandProtectHome=read-only, and gives the service a private/tmp. AddStateDirectory=appfor data it keeps. If files must belong to a fixed user, create one and setUser=. - Take the binary out of writable places. A unit that runs
/tmp/x, or a root unit that runs something under/home, runs whatever is at that path when it starts. Install it under/usr/local/binor/opt, owned by root. - Add the sandbox with
sudo systemctl edit app.service, which writes/etc/systemd/system/app.service.d/override.conf. The checker prints a starter one with only the lines you are missing. - Restart and read the journal (
journalctl -u app.service -e). Remove the line that broke it, or loosen it (a writable path, a device, a system call group).
Each setting the checker reads
| Setting | What it blocks | What it can break |
|---|---|---|
NoNewPrivileges=yes | A setuid program (sudo, su, pkexec) raising the service's privileges. | Services that really call sudo or a setuid helper. Rarely anything else. |
ProtectSystem=strict | Writes anywhere except /dev, /proc, /sys and the paths you list. yes and full only cover /usr, /boot and (full) /etc. | Anything that writes outside its listed paths. Add ReadWritePaths=, StateDirectory=, CacheDirectory= or LogsDirectory=. |
ReadWritePaths=/ | Nothing: it re-opens the whole filesystem and cancels the line above. The checker flags it as high. | — |
ProtectHome=yes | Reading /home, /root and /run/user: SSH keys, shell history, browser profiles. | Services that live in or read a home directory. Use read-only, or move the files. |
PrivateTmp=yes | Sharing /tmp with every other process, the usual place for symlink and planted-file attacks. | Services that hand files to other programs through /tmp. |
PrivateDevices=yes | Seeing raw disks, memory and input devices in /dev. | GPU, serial, USB or audio access. Use DeviceAllow= for just that device instead. |
ProtectKernelTunables=, ProtectKernelModules=, ProtectKernelLogs=, ProtectControlGroups= | Changing /proc/sys, loading modules, reading the kernel log, rewriting cgroups. | System tools that tune the kernel or manage containers. |
SystemCallFilter=@system-service with SystemCallArchitectures=native | System calls a normal service never makes (mount, kexec, reboot, module loading, raw I/O). | Unusual programs. Add SystemCallErrorNumber=EPERM so a blocked call fails instead of killing the service while you test. |
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 | Raw packet, netlink and Bluetooth sockets. | Network tools, VPNs and anything reading interface state over netlink (add AF_NETLINK). |
AmbientCapabilities=, CapabilityBoundingSet= | The checker flags CAP_SYS_ADMIN, CAP_SYS_MODULE, CAP_SYS_PTRACE, CAP_SYS_RAWIO, CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH, CAP_BPF and CAP_SETUID: each is a short step from full root. | Keep only what the program needs; a web server on port 443 needs only CAP_NET_BIND_SERVICE. |
+ or ! before an Exec*= command | Nothing: the prefix runs that command with full privileges outside the sandbox. | Keep it to a short fixed helper, never the main process. |
UMask= | The checker flags a mask that leaves new files writable by other users (0000, 0002 and similar). | Use 0027, or 0077 for private data. |
ExecStart=/usr/bin/docker run … | Little: dockerd starts the container outside the unit, so User= and the sandbox settings confine only the client (podman and nerdctl are treated the same way). The checker says so and skips the missing-sandbox findings. | Harden the container itself with the runtime's options (its user, read-only root filesystem, dropped capabilities). |
curl … | sh in an Exec*= line | Nothing: every start runs whatever that URL serves. | Download once, check a hash or signature, install, and run the installed file. |
A worked example
A typical self-written unit, and the drop-in the checker writes for it:
[Service]
ExecStart=/usr/local/bin/ledger-api --port 8080
Restart=always
# Starter hardening for ledger-api.service. Install with: sudo systemctl edit ledger-api.service
# Then: sudo systemctl restart ledger-api.service && journalctl -u ledger-api.service -e
# Loosen any line that breaks the service; each is independent.
[Service]
#DynamicUser=yes # run as a throwaway user; check it can still reach its files first
NoNewPrivileges=yes # setuid binaries cannot raise privileges
ProtectSystem=strict # the whole filesystem read-only; add ReadWritePaths= for what it writes
ProtectHome=yes # home directories hidden
PrivateTmp=yes # a private /tmp
PrivateDevices=yes # no hardware devices; remove if it needs a GPU or serial port
ProtectKernelTunables=yes # /proc/sys and /sys read-only
ProtectKernelModules=yes # no module loading
ProtectKernelLogs=yes # no kernel log access
ProtectControlGroups=yes # cgroup tree read-only
SystemCallArchitectures=native # no foreign-ABI system calls
SystemCallFilter=@system-service # the usual service system calls only
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 # only Unix and IP sockets
The user line is commented out on purpose: whether the service can run as a throwaway user depends on which files it needs, which only you know.
When to use something else
- On the machine itself,
systemd-analyze security app.servicescores the loaded unit, including vendor drop-ins this tool cannot see. Use both: this one in the repository before deploy, that one on the host. - For containers, the same questions are answered by the Docker Compose Security Check and the Kubernetes Pod Security Check.
- For a service that protects valuable data, have a qualified person review it. A clean scan is never proof a service is safe.