brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: AmbientCapabilities lets a non-root systemd service bind port 80

Systemd
~/tips/tip-systemd-ambient-capabilities-low-port

[Service]

Lets a service running as a normal user listen on ports below 1024 without root.

Running a web service as root just for ports below 1024 is a poor trade, and a capability fixes it.

[Service]
User=web
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE

AmbientCapabilities= hands the named capability to the process even though it runs as a normal user, so it can bind ports 80 and 443. CapabilityBoundingSet= then removes every other capability from the process. The program itself needs no setcap on the binary. Other capabilities are listed in capabilities(7). Pair it with NoNewPrivileges=yes for a tighter result. Verify the result with ss -ltnp after a restart.

Further reading: systemd.exec manual page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link