Hardening systemd Services with Sandboxing on Linux
This deep-dive covers systemd service security hardening with actionable analysis, practical implications, and expert insights.
Why Most Linux Daemons Run with Far Too Many Privileges
By default, a basic systemd unit file inherits full read access to /home, write access to most of the filesystem, raw kernel capabilities, and unrestricted system calls. If an attacker exploits a vulnerability in a web API or background worker, an unhardened unit hands them the entire server. Modern systemd ships built-in kernel sandboxing (mount namespaces, seccomp-bpf, capabilities, and cgroups) that takes five minutes to apply and requires zero Docker overhead.
Step 1: Audit Your Existing Services with systemd-analyze security
Run the built-in exposure analyzer to see every active service ranked by risk score:
systemd-analyze security
systemd-analyze security myapp.service
A stock unit running as root typically scores 9.4 UNSAFE to 9.6 UNSAFE. The detailed breakdown lists every missing protection along with its weight.
Step 2: Apply a Production Sandboxed Drop-In Override
Create a clean override for your service with sudo systemctl edit myapp.service (or write a new unit in /etc/systemd/system/myapp.service):
[Unit]
Description=Hardened Production API Service
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/myapp --config /etc/myapp/config.toml
Restart=on-failure
RestartSec=3
# Identity and ephemeral writable state
DynamicUser=yes
StateDirectory=myapp
ConfigurationDirectory=myapp
RuntimeDirectory=myapp
# Filesystem isolation
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectKernelLogs=yes
ProtectControlGroups=yes
ProtectClock=yes
ProtectHostname=yes
# Privilege and capability restrictions
NoNewPrivileges=yes
CapabilityBoundingSet=
AmbientCapabilities=
RestrictSUIDSGID=yes
LockPersonality=yes
MemoryDenyWriteExecute=yes
# Network and syscall filtering
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
RestrictNamespaces=yes
SystemCallArchitectures=native
SystemCallFilter=@system-service
SystemCallFilter=~@privileged @resources @obsolete @debug
# File permission umask
UMask=0077
[Install]
WantedBy=multi-user.target
Step 3: Validate, Reload, and Re-Score
Check unit syntax, reload systemd, restart the daemon, and inspect the new security score:
sudo systemd-analyze verify /etc/systemd/system/myapp.service
sudo systemctl daemon-reload
sudo systemctl restart myapp.service
sudo systemctl status myapp.service
systemd-analyze security myapp.service
With the directives above enabled, most compiled Go, Rust, or Python daemons drop from 9.6 UNSAFE down to 1.2 OK to 1.8 OK.
Handling Common Application Requirements Safely
| App Requirement | Safe Directive Adjustment |
|---|---|
| Needs to bind port 80 or 443 | Set CapabilityBoundingSet=CAP_NET_BIND_SERVICE and AmbientCapabilities=CAP_NET_BIND_SERVICE |
| Uses JIT compiler (Node.js, PHP OPcache, Java) | Remove MemoryDenyWriteExecute=yes (JIT requires W+X pages) |
| Needs to write logs or uploads to a fixed folder | Add ReadWritePaths=/var/lib/myapp/uploads |
| Needs a static system user for file ownership | Replace DynamicUser=yes with User=myapp and Group=myapp |
Summary
Hardening Linux daemons with native systemd sandboxing gives you container-grade isolation directly at the init layer with zero runtime overhead. Make systemd-analyze security part of every deployment checklist so no internet-facing service ever runs unrestricted.
Was this technical analysis helpful?
Your feedback informs our benchmark updates and ongoing article audits.
FAQ
Frequently Asked Questions
Structured for search engines and AI answer systems (AEO/GEO).
Yes. Add AmbientCapabilities=CAP_NET_BIND_SERVICE and CapabilityBoundingSet=CAP_NET_BIND_SERVICE so the service can bind privileged ports without running as root.
ProtectSystem=full makes /usr, /boot, and /etc read-only. ProtectSystem=strict makes the entire root filesystem read-only, requiring explicit ReadWritePaths= or StateDirectory= grants for any writable folder.
SIGSYS means the binary called a Linux syscall blocked by your seccomp filter. Check journalctl or dmesg for the blocked syscall number, or keep @system-service and remove only specific risky groups like ~@privileged ~@resources.
No. Always use sudo systemctl edit <service> to create a drop-in override under /etc/systemd/system/<service>.d/override.conf so OS package upgrades never overwrite your hardening.
More answers in our FAQ hub.
Newsletter
Practical Linux & Applied AI Newsletter
Get new tested tutorials and benchmark comparisons delivered directly to your inbox. Zero spam.
Join Weekly guides readers getting our weekly digest.
- Practical Linux administration & terminal workflows
- Tested open-source AI setups & benchmarks
- Zero spam - unsubscribe anytime