I have a Python script that access the network, and I want to deny it network access sometimes. How can I achieve this with SELinux or AppArmor? I want to be able to launch this specific script with restricted access.


AppArmor has the ability to block network connections from applications. I've never done it myself, but you should look at the AppArmor community page and the network rules page for details on how to do it.

From the looks of it, the deny network directive should allow you to block all network access for an application.

| improve this answer | |
  • thanks a lot, but suddenly I could not run apparmor - AppArmor not available as kernel LSM famous bug:( – scythargon Aug 23 '12 at 10:44

If you have SELinux enabled, I was under the impression that the default action was to deny access so what you want to do is to allow network access at times, right?

I suspect that you going to have to do something like this answer on serverfault, which contains SE Linux configuration for a single process httpd and then combines that configuration with iptables to control the network access.

To add in the time aspect, there's a time module in iptables. Here's a link showing some real usage on another forum.

AppArmor has already been well-answered :)

| improve this answer | |

AppArmor profile for this case is:

profile nonetwork /path/to/exec {
  # Allow all rules...
  # ...but no network
  deny network,
  deny capability net_admin,
  deny capability net_bind_service,
  deny capability net_broadcast,
  deny capability net_raw,

Nowadays not all linux distros support some features, for example Debian 10 not support network, unix, dbus rules. So this profile will not works in such distros.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.