3

I have a desktop PC running debian that has two network cards installed. One network card is connected to a secure company network and requires a static IP. The other network card (eth1) is connected to a dlink router and then to a cable modem with DHCP addressing.

I'd like to create a script to switch between the two networks. I want to make sure that the two networks aren't connected at the same time. Right now my script checks the up/down status of each interface and toggles them with ifup/ifdown. However, I'm having a number of issues trying to switch networks which I'll attempt to outline but I get the feeling that I'm just going about this the wrong way.

If I configure /etc/network/interfaces to reflect the static IP settings required for the company network connection and the DHCP required for the cable connection than I get the error "RTNETLINK answers: file exists" because I have more than one gateway defined in the file.

If I define the gateways via 'route add' and include metering ifup won't bring up eth0. If I leave eth0 and eth1 undefined in the interfaces file and use network manager to manage them, ifup won't bring up either interface. In both cases I get the error "Failed to bring up eth#" (replace # with either 0 or 1) with no explanation for further detail when I try to use ifup.

If I replace ifup/ifdown with "ifconfig eth# up/down" than the interfaces go up and down properly but the DNS settings in resolv.conf don't update so I can't do much. Adding "resolvconf -u" doesn't seem to do the trick either.

I can get everything working properly on either network if I manually edit resolv.conf after switching but I'd like to have this as automated and easy as possible. Any help you can provide is greatly appreciated.

---update---

/etc/network/interfaces

auto lo
  iface lo inet loopback

auto eth0
  iface eth0 inet static
  address 10.0.0.152
  netmask 255.255.255.0
  gateway 10.0.0.1
  network 10.0.0.0
  broadcast 10.0.0.255
  dns-search company.com
  dns-nameservers 10.0.0.2 10.0.0.3 10.0.0.4
  meter 100  #without this line I get RNETLINK answers: file does not exist

auto eth1
  iface eth1 inet dhcp  #assigns an address in the 192.168.0.0/24 space with 192.168.0.1 as DNS
  meter 200  #without this line I get RNETLINK answers: file does not exist

/etc/resolv.conf

nameserver 10.0.0.2
nameserver 10.0.0.3
nameserver 10.0.0.4
search company.com cable.provider.com

Unless I manually edit the file, my resolv.conf always has the DNS servers for the eth0 connection and both domains in the search path. In order to get the cable connection to work I have to delete the other nameserver lines and add 192.168.0.1 and in order for the company connection to work I just have to delete the cable provider search path.

1 Answer 1

1

The DHCP settings shouldn't contain a gateway address in /etc/network/interfaces. To configure DHCP on eth0, for example, you only need the following lines at a bare minimum:

auto eth0
    iface eth0 inet dhcp

Once that's configured properly ifup & ifdown should work correctly, then you can put any scripts you want ran when changing an interface's state in /etc/network/if-up.d & /etc/network/if-down.d. However, if your DHCP server is setup correctly it should provide a DNS configuration to it's clients, so configuring it by hand shouldn't be necessary

EDIT

To have your DNS settings change automatically you need to add the necessary configuration to each respective interface in /etc/network/interfaces, even if the interface uses DHCP.
DNS settings in /etc/network/interfaces are passed to resolvconf when ifup & ifdown are called by using hook scripts in the /etc/network/if-(up|down).d directory.

When you bring up an interface using ifup it's DNS settings are appended to /etc/resolv.conf, which is what you're experiencing. Conversly, the same settings are removed when the interface is torn down using ifdown. But even if you use ifdown, if there are no static DNS settings in /etc/network/interfaces then the settings for that interface not be removed from /etc/resolv.conf.

So your entry for eth1 should look like:

auto eth1    
    iface eth1 inet dhcp  #assigns an address in the 192.168.0.0/24 space with 192.168.0.1 as DNS
    metric 200            #without this line I get RNETLINK answers: file does not exist
    dns-search cable.provider.com

Additionally, you can use the -v option with ifup & ifdown to enable verbose output and see exactly what hook scripts are being executed.

Sources: man resolvconf man interfaces

3
  • That was my initial thought as well. I added my config files to my initial post. Unless I add the metering I get that RNETLINK error which my research has indicated means that there are multiple gateways defined in the interfaces file. I tried again this morning and I am able to use ifup/ifdown with the metering lines in my interfaces file but I'm still having the DNS/resolv.conf issue.
    – DarkShadow
    Jun 10, 2014 at 13:11
  • @DarkShadow see edit
    – Creek
    Jun 10, 2014 at 16:24
  • Adding the "dns-search" line under eth1 did the trick. Thank you very much!
    – DarkShadow
    Jun 11, 2014 at 12:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .