Linux Debian Networking – How to Set a Routing Table that Prefers WLAN DHCP Interface as Default Route

debianlinuxnetworking

eth0 has a static IP set in /etc/interfaces

wlan0 gets it IP dynamically

How can I change my routing table and where would I put the command to always make wlan0 the default route so internet access will work? Right now I can ssh into the box using either eth0 or wlan0 but internet access gets routed through eth0 always which does not work.

Also, are auto and allow-hotplug contradictory options?

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.20.1    0.0.0.0         UG    0      0        0 eth0
192.168.10.0    *               255.255.255.0   U     0      0        0 wlan0
192.168.20.0    *               255.255.255.0   U     0      0        0 eth0

source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
auto eth0
allow-hotplug eth0
iface eth0 inet static
        address 192.168.20.2
        netmask 255.255.255.0
        gateway 192.168.20.1
allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
allow-hotplug wlan1
iface wlan1 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Best Answer

To defined a preferred interface, use the metric directive in the interfaces. The higher the value, the lower the priority.

allow-hotplug eth0
iface eth0 inet static
    address 192.168.20.2
    netmask 255.255.255.0
    gateway 192.168.20.1
    metric 30
allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    metric 10

Then restart the networking service with:

service networking restart

From Debian Reference - Chapter 5. Network setup

The ifmetric package enables us to manipulate metrics of routes a posteriori even for DHCP.

The following sets the eth0 interface to be preferred over the wlan0 interface.

Install the ifmetric package.

Add an option line with "metric 0" just below the "iface eth0 inet dhcp" line in "/etc/network/interfaces".

Add an option line with "metric 1" just below the "iface wlan0 inet dhcp" line in "/etc/network/interfaces".

The metric 0 means the highest priority route and is the default one. The larger metric value means lower priority routes. The IP address of the active interface with the lowest metric value becomes the originating one. See ifmetric(8).

Related Question