2

This is my routing table when I connect my Android phone via USB to my Raspberry and enable USB-tethering.

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.42.129  0.0.0.0         UG    204    0        0 usb0
0.0.0.0         192.168.0.1     0.0.0.0         UG    303    0        0 wlan0
192.168.0.0     0.0.0.0         255.255.255.0   U     303    0        0 wlan0
192.168.42.0    0.0.0.0         255.255.255.0   U     204    0        0 usb0

I want wlan0 to be the preferred interface. So how can I change the metric of one the interfaces permanently?

Also how does Linux decide which metric value it will use for an interface?

6

One of the solutions is to configure settings in /etc/network/interfaces adding default route with a predefined metric:

auto lo
iface lo inet loopback

# Primary interface
auto wlan0
iface wlan0 inet static 
    address 192.168.0.100
    netmask 255.255.255.0 
    dns-nameservers 192.168.0.1 8.8.8.8
    post-up /sbin/ip route add default via 192.168.0.1 dev wlan0 metric 10
    post-down /sbin/ip route del default via 192.168.0.1 dev wlan0 metric 10
    wpa-ssid <your_SSID>
    wpa-psk <your_PSK>

Maybe, you need to add another Wi-Fi options specific to your settings.


Another solution is to change the metric in the /etc/dhcpcd.conf. According to the dhcpcd manual metric can be assigned to the interface:

metric
         Metrics are used to prefer an interface over another one, lowest
         wins.  dhcpcd will supply a default metric of 200 +
         if_nametoindex(3).  An extra 100 will be added for wireless
         interfaces. 

Add these lines to /etc/dhcpcd.conf:

interface wlan0
metric 200

interface usb0
metric 300

And restart dhcpcd and networking services.

  • my /etc/networking/interfaces looks like this, so can I still add them?: # interfaces(5) file used by ifup(8) and ifdown(8) # Please note that this file is written to be used with dhcpcd # For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf' # Include files from /etc/network/interfaces.d: source-directory /etc/network/interfaces.d – MatMis Dec 25 '17 at 23:35
  • Could you please show the content of the directory /etc/network/interfaces.d? If there are no interfaces, you can add that config. – Gnat Dec 25 '17 at 23:40
  • /etc/network/interfaces.d is empty. – MatMis Dec 25 '17 at 23:44
  • I don't want to have a fixed default gateway and I still want to use dhcp. Do you know any other solution? – MatMis Dec 25 '17 at 23:56
  • Can you provide the output of the file /etc/dhcpcd.conf ? – Gnat Dec 26 '17 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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