Linux – Configure gateway for two NICs through static routeing

linuxlinux-networking

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 the net-tools package. It's long deprecated anyway. Use ip from the iproute2 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:

# Assumes that previous configuration properly set address and route scopes
ip route flush all proto static scope global

Then add the tables for each interface.

ip route add 10.0.0.3/24 dev eth0 table 5000
ip route add default via 10.0.0.254 dev eth0 table 5000

ip route add 10.0.1.1/24 dev eth1 table 5001
ip route add default via 10.0.1.26 dev eth1 table 5001

One then sets up routing rules:

ip rule add from 10.0.0.3 table 5000
ip rule add from 10.0.1.1 table 5001

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:

ip route add default via 10.0.0.254 dev eth0

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.

ip route add default nexthop via 10.0.0.254 dev eth0 weight 1 nexthop via 10.0.1.26 dev eth1 weight 1

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.