Hardening Systemd Units: Capabilities, Sandboxing, and Minimal Attack Surface
Many self-hosted network services still run as unrestricted root simply because they need to bind port 80 or 443 or read TLS private keys. Modern systemd ships with declarative sandboxing directives that dramatically shrink the blast radius of any bug in minutes.
Baseline Hardening Snippet
[Service]
DynamicUser=yes
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
NoNewPrivileges=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectSystem=strict
ProtectHome=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
SystemCallArchitectures=native
MemoryDenyWriteExecute=yes
Auditing Your Score
Run systemd-analyze security your-service.service after reloading systemd. Getting a network-facing daemon from the default 9.6 UNSAFE down below 2.0 OK usually takes less than ten lines of unit configuration and zero code changes.