2

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?

5
  • 2
    what does your full routing table look like? $ip route
    – Zypher
    Aug 15, 2012 at 17:35
  • @Zypher Nothing, its blank atm. Aug 15, 2012 at 17:37
  • You ran ip route and the result was empty? Then you shouldn't be getting any packets sent anywhere. There is no hidden default.
    – Alan Curry
    Aug 15, 2012 at 17:42
  • @AlanCurry Yes I realize when the routing table is empty I will get no connections because the system does not know where to send the data. I am looking for the best way to configure my static routes to include those connections. Aug 15, 2012 at 17:46
  • The necessary routes should be created automatically based on the netmasks when you configure the interfaces. If you must add them manually, route add -net 10.0.0.0 netmask 255.255.255.0 dev eth0 would do it. But it shouldn't be necessary.
    – Alan Curry
    Aug 15, 2012 at 17:49

3 Answers 3

5

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.

1
  • Table names can be defined in /etc/iproute2/rt_tables so as to use such names instead of numbers.
    – Ale
    Apr 4, 2023 at 14:39
2

Under normal circumstances, you can only have one default gateway. It is the default after all. All traffic that doesn't match a more specific static route will go out the default gateway on eth0.

If you want all of the traffic for just the 10.0.1.0/24 subnet to go out of eth1 and the rest of the traffic to use eth0, do what @AlanCurry said and do something like route add -net 10.0.1.0 netmask 255.255.255.0 dev eth1

4
  • I tried that particular command and the one @AlanCurry suggested however neither in combination or alone did they work. As for multiple routes I was under the impression the linux kernel began support for that several years ago. link Aug 15, 2012 at 18:16
  • Of course you can add multiple default routes, the question is why you would want to. Normally you want the default to be the interface that points toward the Internet. We have very little information to go on, so it looks like you're doing something weird for no reason. Can you back up and give a bigger picture of your network design? And also, tell what distribution you're using so we can point you to the correct config files (like /etc/network/interfaces for the Debian family)
    – Alan Curry
    Aug 15, 2012 at 19:56
  • @Blackninja543 I think this is a prime example of you doing something unusual and expecting a different behavior than you're getting. Instead of having such a narrow scope to your question, why not explain the whole problem and explain what you've tried. Perhaps there's an answer that you're not considering. Instead of pigeonholing your possible answers, why not just outline the hole problem.
    – MDMarra
    Aug 15, 2012 at 19:59
  • Also, multiple default gateways are supported, just not in the way that you expect them to work.
    – MDMarra
    Aug 15, 2012 at 20:00
0

The simplest way:

ip route flush all
ip route add 10.0.0.0/24 dev eth0
ip route add 10.0.3.0/24 dev eth1
ip route add default via 10.0.0.254

That will give you access to both subnets on the proper devices and route all internet traffic through eth0 on gateway 10.0.0.254.

You can set these up to persist through reboots / restarts with rules in Red Hat based flavors in : /etc/sysconfig/network-scripts/route-ethX.


/etc/sysconfig/network-scripts/route-eth0:

 10.0.0.0/24 dev eth0
 default via 10.0.0.254

/etc/sysconfig/network-scripts/route-eth1:

10.0.3.0/24 dev eth1

You must log in to answer this question.

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