Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
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 Apr 4 '12 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. –  casualunixer Apr 5 '12 at 1:36

1 Answer 1

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?

share|improve this answer
    
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 Apr 5 '12 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. –  Eli Rosencruft Apr 5 '12 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 Apr 7 '12 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. –  Eli Rosencruft Apr 7 '12 at 18:14
    
This adds the metric post-up ip route add 192.168.1.0/24 dev eth0 metric 1 But the networking daemon by using intefaces file adds the same route with 0 metric before that. So I have to add this before my above mentioned command: up ip route del 192.168.1.0/24. Can it be made simpler? I have updated my question with reference to this query. –  nixnotwin Apr 9 '12 at 5:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.