OK I have two NICs eth0
and eth1
. Each over their own default gateway. I can only access the network on eth0
since that is the first NIC to have a default route added. How can I configure my static routes to allow both to access their separate subnets.
eth0: 10.0.0.3 netmask 255.255.255.0 gw 10.0.0.254
eth1: 10.0.1.1 netmask 255.255.255.0 gw 10.0.1.26
EDIT:
Question2
If no route exists in the routing table on Linux does it use eth0
by default?
Best Answer
You can do policy routing, i.e., have multiple routing tables, and select the routing table to use based on your source address (in general other fields, but not relevant here). Note that the table numbers shown below are arbitrarily chosen.
I do not know how to do policy routing using
route
from thenet-tools
package. It's long deprecated anyway. Useip
from theiproute2
package.You'll also need to have policy routing enabled in your kernel. The major distributions do this by default if I am not mistaken.
Start from a clean state, if appropriate:
Then add the tables for each interface.
One then sets up routing rules:
The last step is to configure the default gateway for when you are the initiator, so to speak.
If you want to only use
eth0
as the default gateway, then just do something like this:Note that there is no table specified, so it defaults to main. Using
eth1
is left as an exercise to the reader.If you want to load-balance between the two, can do multipath routes using
nexthop
.As for question two, no Linux does create a default route automatically. If there is no local route for your traffic, you'll get a
EHOSTUNREACH
error.EDIT: Note that none of the above should be taken to imply that IP is stateful, that it has anything to do with connections, or even an inherent notion of a flow. It merely means that we assume that a flow has always the same endpoints, and that we bind a flow to gateway depending on our source address. This is simply necessary because many gateways (esp. in the context of residential ISPs) do reverse path filtering.