Recently, I have encountered a problem of limiting Internet Access to specific programs. Could anybody recommend a good way of doing that, without using any particular software?

3 Answers 3


The solution for me happened to be straight forward.

  1. Create, validate new group; add required users to this group:
    • Create: groupadd no-internet
    • Validate: grep no-internet /etc/group
    • Add user: useradd -g no-internet username

      Note: If you're modifying already existing user you should run: usermod -a -G no-internet userName check with : sudo groups userName

  2. Create a script in your path and make it executable:
  • Create: nano /home/username/.local/bin/no-internet
  • Executable: chmod 755 /home/username/.local/bin/no-internet
  • Content:
sg no-internet "$@"
  1. Add iptables rule for dropping network activity for group no-internet:

    • iptables -I OUTPUT 1 -m owner --gid-owner no-internet -j DROP

      Note: Don't forget to make the changes permanent, so it would be applied automatically after reboot. Doing it, depends on your Linux distribution.
  2. Check it, for example on Firefox by running: no-internet "firefox"

In case you would want to make an exception and allow a program to access local network:

  • iptables -A OUTPUT -m owner --gid-owner no-internet -d -j ACCEPT
  • iptables -A OUTPUT -m owner --gid-owner no-internet -d -j ACCEPT
  • iptables -A OUTPUT -m owner --gid-owner no-internet -j DROP

NOTE: In case of spawning the rules will be maintained. For example, if you run a program with no-internet rule and that program will open browser window, still the rules will be applied.

  • 2
    A few notes Ilia: Ad 1: - to modify existing user: usermod -a -G groupName userName - check with : sudo groups userName Ad 3: - I already have a lot of rules in iptables, The position of the new rule is crucial. should be the first rule in chain OUTPUT. Therefore I use insert : iptables -I OUTPUT 1 -m owner --gid-owner no-internet -j DROP To allow access to LAN: make sure the ACCEPT rules are above the DENY rule. Works like a charm. Use it for example on Wifiguard. Prog checks wlan for unknown devices, but "phones home" on every start.
    – user205602
    Jan 15, 2014 at 14:01
  • The script will only pass the command. If you try launching a programm with parameters, you should use "$@" instead of "$1". For some reason, I had to temporarily store it in a variable for bash to process it correctly: cmd="$@"; sg no-internet "${cmd}"
    – onse
    Oct 20, 2015 at 12:49
  • use "nointernet" instead of "no-internet". For whatever reason Ubuntu 14.04 can't handle the dash when you try to use sg or chgrp (it prompts for a password, then fails).
    – user318195
    Oct 22, 2015 at 20:38
  • 1
    unshare -n YourAppToBlock > use "nointernet" instead of "no-internet". perhaps, 'no\\-internet'?
    – SarK0Y
    Apr 15, 2016 at 22:33
  • I tried it like described but for me, after adding me to "no-internet" and set the ip-tables I cannot connect to the internet anymore (with and without the bash script no-internet).
    – Viatorus
    Jul 26, 2016 at 13:23

A more straightforward possibility: use firejail. It runs the application inside sandbox. At the sandbox, you can control the access of the application to any network or folder in your computer.

To execute a certain application without network access do following:

firejail --net=none <application>

In that case, "The sandbox looks like a computer without any network interfaces." (See Network Section in documentation)

For example, firejail --net=none firefox will start firefox without any network connection.


See the Installation documentation. You should install from the package system in your distribution, or better get the latest version LTS. (For example, this latest LTS version, 9.56.2, works also in Ubuntu 16.04.)

  • Can I use firejail for slurm as well?
    – alper
    Nov 1, 2021 at 19:16

From answer for How to disable Internet connection for a single process and Block network access of a process

Then, starting a process without network access is as simple as:

unshare -n program ...

This creates an empty network namespace for the process. That is, it is run with no network interfaces, including no loopback. In below example we add -r to run the program only after the current effective user and group IDs have been mapped to the superuser ones (avoid sudo):

unshare -r -n ping google.com

  • 1
    I could not run libreoffice6.0 with firejail, but your method worked. :) Mar 5 at 17:56

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.