0

I have two interfaces: eth0 and eth1. Both of them have static addresses. They are connected to different routers and both routers have Internet access. I used this tutorial to configure links: https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System.

The problem is that I have access to both subnets but I connect to the Internet only via eth0. When I try to use eht1 link f.ex. to ping I got Destination Host Unreachable

How should I configure the links to have Internet access via both of them.

route -n:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use  Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0  eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0  eth0
10.10.0.0       0.0.0.0         255.255.255.0   U     0      0        0  eth1

When I set 10.10.0.1 as a default gateway I connect to Internet only via eth1.

/etc/network/interfaces:

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

auto eth1
  iface eth1 inet static
    address 10.10.0.10
    netmask 255.255.255.0
    post-up ip route add 10.10.0.0/24 dev eth1 src 10.10.0.10 table rt2
    post-up ip route add default via 10.10.0.1 dev eth1 table rt2
    post-up ip rule add from 10.10.0.10/32 table rt2
    post-up ip rule add to 10.10.0.10/32 table rt2
1
  • Thanks for the answer. It helps me a lot with understanding this issue but is doesn't resolve my problem. I need to use both gateways to test one or another router. Now before each test I set a proper gateway and that's solved my problem.
    – fasolens
    May 9, 2017 at 9:50

1 Answer 1

0

System can have only one gateway at the moment. Only can you do is to force some traffic aka ssh, ftp, .. or to specific network to go on different interface

Route traffic to specific port via specific interface

Example:

Here we shall take port 4444 as the service for routing. Use mangle table of iptables for modifying the 4444 packets.

sudo iptables -t mangle -A OUTPUT -p tcp –dport 4444 -j MARK –set-mark 0×1

We are marking all packets with destination port 4444 as ’0×1′ .

To delete this entry from iptables we can use -D instead of -A.

Next, create a new IP route table in /etc/iproute2/rt_table by just giving an entry

100 4444table

Write rule for 4444 packets.

ip rule add fwmark 0×1 lookup 4444table

Add route at new table 4444table. Here we shall use ’192.168.1.1′ as the gateway for 4444.

We copy all entries except default gateway entry from main table.

sudo ip route show table main | grep -Ev ^default | while read ROUTE ; do ip route add table 4444table $ROUTE; done

Add default gateway entry for 4444 packets to table 4444table

sudo ip route add default via 192.168.1.1 table 4444table

Use ip route show table 4444table to show all routes at 4444table.

Or routing by destination

Destination ip 132.45.65.60

Again use mangle table of iptables for modifying the packets for 132.45.65.60

sudo iptables -t mangle -A OUTPUT -d 132.45.65.60 -j MARK -set-mark 0×1

We are marking all packets router to ip 132.45.65.60 as ’0×1′ .

Now save and restart iptables.

service iptables save
service iptables restart

Next, create a new IP route table in /etc/iproute2/rt_tables by just giving an entry

100 132table

Write rule for packets to destination ip 132.45.65.60

ip rule add fwmark 0×1 lookup 132table

Add route at new table 132table. Here we shall use 192.168.1.1. All other traffic will go through the default gateway.

Copy all entries except default gateway entry from main table.

sudo ip route show table main | grep -Ev ^default | while read ROUTE ; do ip route add table 132table $ROUTE; done

Add default gateway entry for 132.45.65.60 packets to table 132table

 sudo ip route add default via 192.168.1.1 table 132table

Edit 1

If you have to set metric aka with metric interface without metric have "priority"

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

auto eth1
  iface eth1 inet static
    address 10.10.0.10
    netmask 255.255.255.0
    gateway 10.10.0.1
    metric 800 

With this setup if available default gateway will be 192.168.1.1 and traffic will go trough eth0. When you do ifdown eth0 or disconnect cable from eth0, second best will be gateway 10.10.0.1 with metric 800 and traffic will go trough eth1. When you connect cable or ifup eth0 default gw will be again 192.168.1.1.

Like I say.

System can not have two default gateways in one moment. If have network will not work.

3
  • Actually, system does have 2 gateways. The 3rd last line of the question shows that. I wonder if they have brought the Interface down and up to execute the change though.
    – davidgo
    May 9, 2017 at 6:36
  • Any network host can have only one default gateway. You can not have two default gateways. Is not possible. Only way to have two default gateways is to have one default gateway and second with bigger metric. In that case second gateway will be used when first in not available. Only is one way to hell You can not go from room at two doors. You must pick one.
    – 2707974
    May 9, 2017 at 7:13
  • You have Edit 1 in my answer.
    – 2707974
    May 9, 2017 at 7:24

You must log in to answer this question.

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