2

If I have PiA and PiB, its fairly simple to make PiA a gateway for PiB by setting the 'static routers=PiA' on PiB in /etc/dhcpcd.conf

But what if I want/need to set up two IP addresses on a Pi and route eth0.0 to eth0 ?

I've done that, but because the route table shows 2 default routes, the one with the lowest Metric is being used by both interfaces (eth0 & eth0.0)

Any way to force Default Route Metric 204 to be used by eth0.0 and Default Route Metric 202 to be used by eth0 ?

/etc/network/interfaces

iface eth0 inet manual

auto eth0.0
iface eth0.0 inet manual
    vlan-raw-device eth0

/etc/dhcpcd.conf

interface eth0
  static ip_address=192.168.3.50/24
  static routers=192.168.3.3
  static domain_name_servers=8.8.8.8 8.8.4.4

interface eth0.0
  static ip_address=192.168.3.51/24
  static routers=192.168.3.50
  static domain_name_servers=8.8.8.8 8.8.4.4

route

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.3.3     0.0.0.0         UG    202    0        0 eth0
default         192.168.3.50    0.0.0.0         UG    204    0        0 eth0.0

1 Answer 1

1

[Is there a]ny way to force Default Route Metric 204 to be used by eth0.0 and Default Route Metric 202 to be used by eth0 ?

No, because routes are not "used" by interfaces.

Simplified, Linux outbound routing works like this:

  1. A program requests the kernel to send a packet to a specific IP address.
  2. The kernel searches through the routing table for the most specific route that matches that address (see below).
  3. If the route is an interface route for a destination on the network to which that interface is attached, the packet is sent directly to the destination host. 4. If the destination address is via a gateway, that gateway must be on the network to which that interface is attached and the packet is sent to the gateway which is expected to forward it further.

A "more specific" route has a longer prefix. The number of prefix bits (expressed as a "Genmask" in your route command output above, but as /n in the output of ip route). So, given the following routes:

192.168.66.0/24 (prefix mask 255.255.255.0)
192.168.0.0/16  (prefix mask 255.255.0.0)
0.0.0.0/0       (prefix mask 0.0.0.0)

192.168.66.17 would match the first one, because the first 24 bits (192.168.66) are the same. 10.1.2.3 would match the last one because the first 24 bits of it (10.1.2) are not the same as the first one, nor are the first 16 bits (10.1) the same as the second one. The first 0 bits of any two addresses match which is why that last route is called a "default route."

So in your case, anybody trying to send a packet to an otherwise unmatched address will match your lower-metric default route and it will always be sent to that gateway through that interface.

Given that understanding; can you explain what you're trying to do? If the first default route can always get the packets there, why would you ever bother to send to the second? If the first route can't get the packets to some destinations, you can add specific routes for those destinations to be sent via the second gateway and interface.

2
  • The short answer to what I am trying to do is: put an OpenVPN server on the same Pi as a Tor Router and have the output of the Server (that would normally just go to the Internet destination to be sent through Tor instead). The only way I can think of doing this is to have OpenVPN on one interface and Tor on another and then route the outbound traffic from OpenVPN to Tor. BTW: Thank you very much for your response! I've been banging my head against this for 4 days now and getting nowhere.
    – Da Hai Zhu
    Commented Apr 6, 2017 at 4:03
  • Also, I found this web site: thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System It seems it might do what I want, but still not getting anywhere with it.
    – Da Hai Zhu
    Commented Apr 6, 2017 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.