Practical systemd Unit Sandboxing for Go Daemons
Static Go binaries are easy to deploy, which often tempts operators into running them directly as root. A few lines in a systemd unit file dramatically shrink the attack surface without requiring containers.
Minimal Privileges for Port 443 Services
If a web server only needs to bind privileged ports 80 and 443, grant solely CAP_NET_BIND_SERVICE and drop everything else:
[Service]
User=www-data
Group=www-data
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
ProtectHome=true
Restart=on-failure
RestartSec=3s
Checking Exposure Score
systemd ships with a built-in analyzer that grades every active unit on a 0-10 exposure scale:
systemd-analyze security your-service.service
Even enabling just NoNewPrivileges, ProtectSystem, and dropping root typically cuts the exposure score in half with zero runtime overhead.