0

I have two network interfaces eth1 and wlan0. I would prefer all outbound traffic to go through eth1 before wlan0, but if eth1 is not up, and then wlan0 should handle the traffic.

Both interfaces get allocated IP's via dhclient.

I have experimented with supersede routers for interface "wlan0" in dhclient.conf, but it isn't having the desired effect (if wlan0 is up first, and get set as default gateway, then eth1 comes up, it doesn't overwrite the gateway).

Is there a better way to do this?

Jono
  • 113
  • 1
  • 1
  • 5

2 Answers2

4

The keyword you are looking for is "metric". It basically specifies how "preferred" a particular route is. The comment at the end of this article explains how to set it up on Debian based systems:

https://singpolyma.net/2012/08/how-to-force-the-default-route-to-always-use-a-specific-interface-on-ubuntu/

The article has some info on how to customise dhclient, but the comment has a simpler alternative:

another way is to edit /etc/networking/interfaces and under the section for your NIC that you do NOT want to force traffic over, add the line ‘metric 150′ (without quotes).

This will give the other NIC a lower metric, giving it priority.

Rogan Dawes
  • 161
  • 2
0

There is no built in way to do this, but with a little shell scripting it is quite easy to do. Since both interfaces are brought up by dhclient, then they have a hook that allows you to execute a script as per the manual:

Immediately after dhclient brings an interface UP with a new IP address, subnet mask, and routes, in the REBOOT/BOUND states, it will check for the existence of an executable /etc/dhcp/dhclient-up-hooks script, and source it if found. This script can handle DHCP options in the environment that are not handled by default. A per-interface. /etc/dhcp/dhclient-${IF}-up-hooks script will override the generic script and be sourced when interface $IF has been brought up.

So in your case, you want the default route from the WAN interface dropped if there is already a default route present. So you could create file /etc/dhcp/dhclient-wlan0-up-hooks with some shell script like this:

RESULT=$(netstat -rn | grep ^0.0.0.0 | grep eth0\$ | wc -l)

if [[ $RESULT == "1" ]]; then
   printf "eth0 already has a default route\n"
   printf "removing wlan0 default route since eth0 default route found\n"
   ...enter ip route del command to delete this route ...
else
   printf "do nothing since eth0 default route NOT found"
fi
Ricardo
  • 739
  • 5
  • 6