This question already has an answer here:

I am making a program that scrapes information from some web services. I want to test how my program reacts when it can't reach the web service, like if the web service is down or if the local computer has no internet access. Disconnecting my whole computer from the network every time I want to run a test is far too much work.

Is there a simple way to run a program under Linux with no network access?

A command like this would be awesome:

$ no_networking "ruby my_program.rb"

marked as duplicate by Archemar, chaos, cuonglm, Stephen Kitt, John WH Smith Mar 24 '15 at 17:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • what about spinning up a VM and running it in there? – rob Mar 24 '15 at 11:16
  • @rob: Doable, but far, far more work than a simple command – Hubro Mar 24 '15 at 11:21
  • 2
    Why don't you just use a bogus URL for testing? – terdon Mar 24 '15 at 11:27
  • @terdon: That's... Brilliant! That hadn't occurred to me at all – Hubro Mar 24 '15 at 11:39
  • run it in screen with a sleep delay like sleep 90 && ./myprogram. then take down the network. if the machine is reached by network then before taking the network down for it, set up a sleep delayed command to restore the network or to reboot, in another screen. – Skaperen Mar 24 '15 at 12:41

You have several options like adding the host you want to connect to into the /etc/hosts file:

vi /etc/hosts, add: host.where.i.want.to.connect.to

make sure that is an IP address which does not exist in your network. Using this method you are overriding the resolving of the host you have configured in your my_program.rb application. If you want to 'allow' normal access again, you can simply comment the line in the /etc/hosts file by prefixing the line with a # character.

Another option is to add a firewall rule which blocks or rejects traffic to the host you are connecting to.

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