I have a custom systemd service which I want to restrict to the local interface. On ctrl.blog systemd application firewalls examples I found that it should be possible to achieve this using only the service file. To make sure its working, I first tried to deny any network access:




IPAddressDeny=any # should block networking entirely


However the service is still working, so the line IPAddressDeny=any seems to have no effect. I then tried to apply an iptables rule to the cgroup created by systemd. Again, I tested by applying a rule that should deny all networking:

iptables -A INPUT  -m cgroup --path "/system.slice/myservice.service" -j REJECT
iptables -A OUTPUT -m cgroup --path "/system.slice/myservice.service" -j REJECT

But the service is still running and now I am out of ideas how to enforce network filters to my systemd service.

  • 1
    Could it be that your kernel has no support for cgroups firewalling? See unix.stackexchange.com/questions/467362/… – Pro Backup Feb 28 at 22:18
  • When I created a custom cgroup using mkdir /sys/fs/cgroup/net_cls/jail and added a process by echo $PID > /sys/fs/cgroup/net_cls/jail/tasks, the iptables-method worked. Is there an easy way to check for kernel support, though? – staxyz Feb 28 at 22:24
  • You could try setting PrivateNetwork=true instead? – meuh Mar 1 at 9:09
  • This is preventing network access, but now I can not even connect to the service on localhost. – staxyz Mar 1 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.