1. add this to .service
AmbientCapabilities=CAP_NET_BIND_SERVICE
2. or listen on :8080 and use NAT: iptables -t nat -I OUTPUT -p tcp -o lo --dport 80 -j REDIRECT --to-ports 8080
3. or make the port unprivileged sysctl -w net.ipv4.ip_unprivileged_port_start=80
It may work for httpd too, I haven't tested it.Not everything that wants to open up a port seems to support socket activation. I tried with 6tunnel and couldn't get it to work.
[1] https://www.freedesktop.org/software/systemd/man/systemd-soc...
https://www.freedesktop.org/software/systemd/man/systemd.exe...
[Service]
AmbientCapabilities=CAP_NET_BIND_SERVICE
SocketBindDeny=any
SocketBindAllow=tcp:80
SocketBindAllow=tcp:443
These ports should be denied by the kernel because they're already taken by httpd, and all other will be denied by bpf filters installed by systemd.It feels like plugging holes in a dam, but that's what you do with popular operating systems.
i added support for httpd to support systemd socket activation in 2013: https://svn.apache.org/viewvc?view=revision&revision=1511033
httpd can start as non-root, assuming other configurations like the access / error logs are writable by the non-root user.
This allows to let systemd to manage ports using socket units which will also stay up and buffer requests when restarting a service, allow service activation on demand/incoming requests or per connection service instances, e.g. for better isolation of sshd's per connection/user.
One approach that may work with systemd is to have two processes. One would be a broker, running as root. It would grab a port, for example. The other process would be spawned by the broker as a limited service and inherit that port from the parent, with no permissions of its own to open it, only to inherit.
IDK how to express that in systemd-land though. At that point you might be better off just writing the code to sandbox things yourself.