I am surprised no one has mentioned the simplest command to do this: ifmetric
. It can be installed using sudo apt-get install ifmetric
. This command can be used to change the metric of any interface. The interface with lower metric is preferred for Internet.
To use this, first see the metrics using route
command:
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.42.0.1 0.0.0.0 UG 100 0 0 eth0
0.0.0.0 10.42.0.2 0.0.0.0 UG 600 0 0 wlan0
Here, eth0
has lower metric, so it will be preferred over wlan0
. If you want to prefer wlan0
, then lower its metric:
sudo ifmetric wlan0 50
Now, the routing table would look like:
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.42.0.2 0.0.0.0 UG 50 0 0 wlan0
0.0.0.0 10.42.0.1 0.0.0.0 UG 100 0 0 eth0
Now Linux will be using wlan0
for Internet. The change will be reflected immediately.
1Great answer. Thx (also for the route -n). I had to reboot after installing ifmetric for the command to change the metric – Thorsten Niehues – 2018-02-15T20:40:21.180
How to make this change permanent (I want to not use accidentally my iPhones hotspot) – Thorsten Niehues – 2018-02-15T20:41:52.553
2@ThorstenNiehues: A quick solution coming to my mind to make it permanent is to add the ifmetric command to your crontab, by doing
crontab -e
and then add the line at the end:@reboot sudo ifmetric wlan0 50
. – shivams – 2018-02-15T20:56:18.777It works. But i can't access local machines after this (via browser e.g.). Any idea? Thanks. – tomasb – 2018-02-23T13:16:39.587
@tomasb: interesting doubt. That is expected, however, as changing the interface preference will bring you into the local network of the preferred interface. One straightforward way to access local machines would be to access them using a virtual machine. However, better solutions must exist. Let me think and reply. – shivams – 2018-02-23T15:17:06.423
I tried this, but I am looking for a solution which lists the networks by SSID, so it's human-readable, and such that the settings are permanent. I found a better answer here
– 6005 – 2019-10-04T17:46:26.130