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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
dan3
  • 660
  • 6
  • 16

2 Answers2

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Ulrich Dangel
  • 25,079
  • 3
  • 80
  • 80
  • 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 01 '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.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • 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 01 '13 at 20:18