I have a Ubuntu box that has two networking interfaces (eth0 and wlan0). They are both configured for DHCP in /etc/network/interfaces, but they both assign a default gateway:


auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto wlan0
iface wlan0 inet dhcp
wireless-essid test

Result of route -n

Kernel IP routing table
Destination Gateway      Genmask       Flags  Metric Ref Use  Iface        UG    100    0   0    wlan0        UG    100    0   0    eth0   U     0     0   0    eth0   U     0     0   0    wlan0

How can I set up /etc/network/interfaces to have only one default gateway, on the interface I want?

Worst case scenario, how can I at least control which one gets on top on the route -n command, each boot?


  • This box will travel a lot, and will be connected to different networks, so I don´t know in advance the IP addresses/ranges it will have.

  • Sometimes the default gw interface will be eth0. Sometimes it will be wlan0 ... So, this needs to be kind of automatic ...

  • Welcome to Super User! Check our formatting tools – you can indent code by 4 spaces so you don't need to add <br> all over the place :) – slhck Jun 16 '12 at 20:52

Usually network related stuff is manged by "network-manager" and it comes with a fairly good glass bulb that knows how to do the right thing(TM).

If you insist on using /etc/network/interfaces and have a computer that travels a lot you should put an executable script into /etc/network/if-up.d. All scripts in this directory are executed everytime an interface goes up. In your new script you may implement logic that modifies your routing table as you want it.

I suggest you start reading the "IFACE OPTIONS" section in the interfaces man-page. Notice however, there is no such thing as "the way" of determining "the correct" settings for your routing table.

You can change the ordering of your default routes by assigning different metrics. The metric is a simple number and the kernel will prefer the default route with the lowest (I think) metric. So you should come up with some test that your script runs and based on the outcomes of that test your script assigns metric to default routes.

  • I want that way after not finding any other info about this issue ... Thanks :) – user140600 Jun 17 '12 at 21:54

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.