Is that possible to add two different default gateways into the routing table for two different NICs in android?

joyce7216 picture joyce7216 · Aug 23, 2011 · Viewed 16.1k times · Source

By default, the packets of default connection will use eth0 to go out/in. The default gateway in the routing table is eth0's gw ip.

When another interface said eth1 is up, I want to add another default gateway for eth1. Is that possible in android? Since I cannot add another default gateway for eth1 because there already exists eth0's.

Thanks for your help.

Answer

dresende picture dresende · Oct 20, 2011

A default gateway is only one, it's the default. You can have several interfaces each one with a different gateway, but only one default (your system cannot choose).

Imagine this:

eth0 - IP: 192.168.0.10/24 Gateway: 192.168.0.1
eth1 - IP: 192.168.1.10/24

You should have at least 3 routes (usually automatic):

To go to some IP on 192.168.0.0, go thru eth0.
To go to some IP on 192.168.1.0, to thru eth1.
To go to anywhere else, go thru 192.168.0.1.

So you see, you don't need more than one default gateway. What you might want is a gateway for a specific route. Imagine that you have the network 192.168.2.0 that can only be reached by using gateway at 192.168.1.1. With the above configuration, your machine would try to use the default gateway 192.168.0.1. You could use something like:

route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1

This way you'll have a 2 gateways, but only 1 is the default. This second one is only used for 192.168.2.0 network.