Home
Home Blogs k4tz's blog
    • Blog
    • Clinic
    • Contact
    • Download
    • Video
    • Login

Poll

Favourite console text editor in Ubuntu:

LXer -- Linux and Open Source News

  • How to install FreeNX server and client in Ubuntu 9.10 (Karmic)
  • Microsoft Office 2007 on OpenSolaris
  • Fedora 13 Alpha Benchmarks
  • OSFA Refutes IIPA's Attack on Open Source Software
  • Will Simon Phipps Energize OSI?
more

Linux Today

  • Deferrable functions, kernel tasklets, and work queues
  • Simon Phipps elected as OSI director
  • Beginner’s Guide to Git
  • Day 3 of the Trial, Through the Eyes of Groklaw and the SL Tribune
  • AMD to Introduce Netbook Chip in 2011
more

Linux Insider

  • Android Has Enough Class for Opera
  • Ubuntu Dumps the Brown
  • Atol Delivers Flawless File Management With No Frills
more

Run a particular program but prevent it from accessing the Internet

Submitted by k4tz on Thu, 06/25/2009 - 11:50
  • Blog
  • Linux
  • Ubuntu

Source: http://ubuntuforums.org/showthread.php?t=1188099

Rational:
Some programs like to access the Internet on their own. For example, many Windows programs I run in wine "call home", and Rhythmbox accesses Amazon each time I play a new MP3 in order to try and download cover art. For privacy purposes, you may want to run a program but ensure it *cannot* access the Internet.

Howto: (6 steps)

Step 1. Create a group called "no-internet" and add your user as a member of this new group.
(System->Administration->Users and Groups)

Step 2. Create a script (somewhere in your PATH) called "ni" (stands for No Internet) as follows:

Code:
sudo nano /usr/bin/ni

with this contents:

Code:
#!/bin/bash 
sg no-internet "$1"

And make it executable:

Code:
sudo chmod +x /usr/bin/ni

Step 3. Create a script called iptables_no-internet_rule as follows:

Code:
sudo nano /etc/network/if-pre-up.d/iptables_no-internet_rule

with this contents:

Code:
#!/bin/bash
iptables -A OUTPUT -m owner --gid-owner no-internet -j DROP

And make it executable:

Code:
sudo chmod +x /etc/network/if-pre-up.d/iptables_no-internet_rule

Step 4. Logout and then log back in again to make the group permissions take effect.

Step 5. Enable the new firewall settings you made above in step 3 by running the following command:

Code:
sudo /etc/network/if-pre-up.d/iptables_no-internet_rule

Step 6. Finished. You can now run any program without allowing that program to access the network by using this command:

Code:
ni "program_name"

Examples:

- Note: the quotes around the program name are only there to allow you to enter commands needing parameters.

Code:
ni "ping www.google.com"
ni "wine install.exe"
ni firefox

will all run but fail to access the Internet because ping, wine, and firefox are run using the ni script as the group no-internet, which has been bared from outputting anything to other networks. Note: if you are just running a single word command like firefox you don't need the quotes. Also note, for testing, make sure firefox isn't already running because then it will already have Internet access. Close it first and then run it preceeded by ni.

Options

The above will actually prevent all outgoing network access by the programs run with ni; however, sometimes this may not be what you want. For example, certain local network access for games in wine might be acceptable. If you want to allow only local network access but still keep the Internet in general blocked, you can change the iptables config line in the file mentioned in Step 3 to the following:

Code:
iptables -A OUTPUT -m owner --gid-owner no-internet -d ! 192.168.0.0/24 -j DROP

change the 192.168.0.0 to match your local network as required.

Revert all changes

The above changes will persist even after system reboot so you can always run any program with the "ni" script to prevent it from getting out on the network. However, if you no longer want to have this feature enabled, you can uninstall the above by simply removing the two files created like this:

Code:
sudo rm /usr/bin/ni
sudo rm /etc/network/if-pre-up.d/iptables_no-internet_rule

and then remove the group "no-internet" from (System->Administration->Users and Groups).

I hope this helps others.

  • k4tz's blog
  • Add new comment

Recent blog posts

  • Configure ThinkPad laptop trackpoint on Ubuntu
  • How to make WPA connection in Ubuntu on demand
  • Review: Sabily 9.10 - Linux Ubuntu for Muslims
  • How to Install 64bit flash on Ubuntu
  • How to get Wireless LAN (Broadcom) on Acer Aspire 4720Z working with Ubuntu 9.10
  • Quick loot at Ubuntu 10.04 Lucid Lynx Alpha 3
  • System testing and benchmarking under Ubuntu 9.10
  • How to PXE booting Ubuntu Installer
  • How to Install Debian onto your Nexus One using Ubuntu
  • (Re) Install a Linux Kernel
more

Linux World

  • Microsoft's Internet Driving Licence: stupid, unworkable and unenforceable
  • Making a videoloop with Kino and Audacity
  • So is ChromeOS a desktop winner? I think not
  • Firefogg: Transcoding videos to open web standards with Mozilla Firefox
  • The Morevna Project: Anime with Synfig and Blender
Archive Syndicate content

Recent comments

  • Re
    9 weeks 4 days ago
  • Re
    9 weeks 4 days ago
  • Re
    10 weeks 4 days ago
  • Re
    11 weeks 19 hours ago
  • Re
    11 weeks 5 days ago
  • Re
    11 weeks 5 days ago
  • Re
    11 weeks 6 days ago
  • iwl3945
    13 weeks 4 days ago
  • HomeBank
    25 weeks 5 days ago
  • KMyMoney and direct connect to banks
    29 weeks 6 days ago
All contents copyright © 2008, Dhuha Net. All rights reserved
Ubuntudoctor® is a member of the Dhuha Network. Privacy Policy
RoopleTheme