11

This is my interfaces file:

auto eth0
iface eth0 inet static
   address 192.168.1.10
   netmask 255.255.255.0
   gateway 192.168.1.1

auto eth1
iface eth1 inet static
   address 192.168.2.10
   netmask 255.255.255.0
   gateway 192.168.2.1

If I restart networking daemon I get this error: eth1 is not up. I want two default gateways to achieve what is mentioned in the accepted answer to the question here. This is how my routing table should be:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
default         192.168.2.1     0.0.0.0         UG    0      0        0 eth1
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0
192.168.2.0     *               255.255.255.0   U     0      0        0 eth1

I got the above table by using ifconfig and route add default gw commands. But I want to do it with the /etc/network/interfaces file. How can I do it?

Update1:

iface eth0 inet static
    address 192.168.1.10
    netmask 255.255.255.0
    up ip route del 192.168.1.0/24
    post-up ip route add 192.168.1.0/24 dev eth0 metric 1
    up route add default gw 192.168.1.1 metric 1
5
  • 1
    As long as you don't care how your traffic goes out. On Solaris and earlier Linux versions this was done in a Round-Robin fashion
    – Karlson
    Commented Apr 4, 2012 at 15:44
  • Your interfaces file looks correct. Usually, DHCP is much more convenient today and that is what I used in this circumstance. You can give specific MAC addresses a set IP address in the DHCP server's settings. Commented Apr 5, 2012 at 1:36
  • Just a side note, NetworkManager recently added more extensive support for route metrics. It handles them automatically or you can set them manually. It might be a more convenient way or you might want to file a feature request for your tool. Commented Jan 2, 2015 at 10:40
  • Also you might want to add details on why you want to have two default routes at all. I can only guess that two routes with different metrics will not work as you expect. Commented Jan 2, 2015 at 10:42
  • The interfaces file allows setting a metric. There should be no need to add routes manually using up and post-up Commented Nov 22, 2017 at 9:16

1 Answer 1

11

This /etc/network/interfaces works for me on 2.6.32-40 Ubuntu 10.04 LTS:

auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
    address 192.168.1.10
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.254
    post-up route add default gw 192.168.1.1 metric 1
    pre-down route del default gw 192.168.1.1

auto eth1
iface eth1 inet static
    address 192.168.2.10
    netmask 255.255.255.0
    network 192.168.2.0
    broadcast 192.168.2.254
    post-up route add default gw 192.168.2.1 metric 2
    pre-down route del default gw 192.168.2.1

I get two default routes:

root@gamla:/etc/network# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    1      0        0 eth0
0.0.0.0         192.168.2.1     0.0.0.0         UG    2      0        0 eth1

Note the metric. Did I understand your question?

8
  • The network and gateway associated with eth0 should apper first as it's the primary network and gateway. What is the output of netstat -anr or just route commands in your setup?
    – nixnotwin
    Commented Apr 5, 2012 at 17:38
  • I edited, adding metric parameters to get the desired route order. My system has a number of other interfaces that I edited out of the output, and my interface names are not ethX, so I am not 100% sure that my solution will work for you. Try it and let me know the results. Commented Apr 5, 2012 at 18:03
  • Adding metric for default gateways works. But how can I add metric for routes? My routing table has a class B subnet which should come before my another class A subnet. Specifying metric 3 from debian ifmetric package did not work for me.
    – nixnotwin
    Commented Apr 7, 2012 at 15:59
  • 1
    The kernel stores the metric but does not use it to route. To use the metric, you need to run a routing daemon such as quagga (was zebra) or bird. Commented Apr 7, 2012 at 18:14
  • 1
    @EliRosencruft can you cite a reference plz?
    – drAlberT
    Commented Oct 8, 2013 at 11:23

You must log in to answer this question.

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