9

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?

MatMis
  • 523
  • 2
  • 7
  • 18

1 Answers1

14

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.

GAD3R
  • 63,407
  • 31
  • 131
  • 192
Gnat
  • 366
  • 2
  • 6
  • 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
  • 1
    /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 00:02
  • /etc/dhcpcd.conf: [link](https://pastebin.com/raw/g3KrY8zU) – MatMis Dec 26 '17 at 00:08
  • See another solution in the answer. – Gnat Dec 26 '17 at 00:13
  • sorry, **/etc/dhcpd.conf** does not exist. I had to create **/etc/dhcpd.conf** and it doesn't work after rebooting, the metric is not changed :( – MatMis Dec 26 '17 at 00:23
  • opps, not /etc/dhcpd.conf, but /etc/dhcpcd.conf – Gnat Dec 26 '17 at 00:25
  • I also tried adding to **/etc/dhcpcd.conf** with the same results. (metric is not changed after rebooting) – MatMis Dec 26 '17 at 00:28
  • Remove the ";". Then it works! – MatMis Dec 26 '17 at 00:40