6

I'd like to start a program disconnected from the network (because immediately upon starting it tries to download huge amounts of data, which can be prevented by changing settings as soon as it settles down). However, I really don't want to actually bring the network down for this one program.

Is there some LD_PRELOAD or similar to give the program the impression that the network is down? I'd rather not create a virtual machine.

  • 4
    Under which Unix variant? Under Linux, see Block network access of a process? (which focuses on methods that work even for a non-cooperating process). – Gilles Sep 1 '13 at 19:50
  • Good point. Linux. – dan3 Sep 1 '13 at 20:13
7

Under Linux, try to use a network namespace, e.g:

sudo ip netns add namespace-name
sudo ip netns exec namespace-name executable

This should prevent the program from accessing the network.

  • WFM. The answer pointed to by @Gilles also contains pointers to unshare and nsenter (though I can't seem to find the latter on Ubuntu) – dan3 Sep 1 '13 at 20:25
  • What's the relationship between unshare and ip netns commands? – CMCDragonkai Jul 18 '15 at 11:40
3

@Ulrich's network namespace solution is a perfect solution if you're on a recent Linux and have superuser access. If not, an alternative could be to use a SOCKS wrapper like dante's socksify or tsocks (note that they work with LD_PRELOAD).

Like with tsocks, create a tsocks.conf with for instance:

path {
    reaches = 0/0
    server = 127.0.0.1
    server_port = -1
    server_type = 5
}
fallback = no

And call your application with:

TSOCKS_CONF_FILE=/path/to/that/tsocks.conf tsocks your-application

That only works for TCP connections and for IPv4. On the other hand, that means you can selectively specify which IP addresses you want to allow a connection to.

  • I will keep this one in mind, despite its limitations (I thought myself about "messing up" a proxy configuration, but decided to ask for something more general). I like the ip netns way though for my case. – dan3 Sep 1 '13 at 20:18

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.