Make NetworkManager prefer ethernet over wifi if both present
A similar question was asked for windows Automatically prefer Ethernet over WLAN but I'm on linux and am using NetworkManager.
Right now if the ethernet cable is plugged in and is available while wifi connections are also available NetworkManager chooses the wifi connection. How can I tell it to always choose the wired connection if available?
I'm on Fedora 17 using NetworkManager-0.9.4.0-9.git20120521
NetworkManager has a GUI frontend named networkmanager-connection-editor
or nm-connection-editor
, where priorities can be set for individual connections.
Just a couple things:
You could try these commands in your environment to see if they work and bring desired results:
- auto Ethernet
- auto ethX (Replace X with whatever number your kernel assigns (run ifconfig to check))
Alternatively, you can have a look at your IP routes found by issuing:
sudo route -n
By looking at the output, see if wlan0 is on all routes.
Several other questions have been asked with this and the only way to get Linux to favour an interface (by default it is eth0 but in your case, something went awry) is to manipulate the metrics.
You can configure the metric for an interface using ifmetric which manipulates the metric on an interface you specify. I haven't tested this on Fedora per se, but I can see it is in the repositories. So, issue:
yum install ifmetric
Then when it is installed, you will want to go to:
/etc/network/interfaces
Use vi, nano, emacs, whatever editor you use and then add underneath whichever interface you want to configure (in this case eth0):
up ifmetric eth0 X
X relates to a number determining the priority, 0 by default is the highest priority, but to avoid hitting static/default routes, use something above 2. Then you will want to add a higher number to wlan0. So:
up ifmetric eth0 10
and under wlan0:
up ifmetric wlan0 20
Your file should be looking like this:
iface eth0 inet dhcp
up ifmetric eth0 10
iface wlan0 inet dhcp
up ifmetric wlan0 20
Let us know if it helps!
EDIT: Just come across a few links:
How do I configure a linux machine to ignore wifi when connected via LAN?
How do I set the priority of network connections in Ubuntu?
EDIT 2:
There is also a metric field in Network Manager GUI!
Note: @Proxy user’s answer served me as the basis for my answer.
You can accomplish this using nmcli
(Command Line Interface of NetworkManager
), no need to install ifmetric
.
All you need to do is set the ipv4.route-metric
option of each required network to some positive integer (default is -1
); the lower the number, the higher the priority. After that you need to reconnect to all changed/required networks (simple up
is enough, no need to turn them down
first).
# List of connections
# You need to get the connection name (first column)
nmcli con
# Set the `ipv4.route-metric` of each required network
# Change the `$con_name_x` and integer as required
nmcli con mod $con_name_1 ipv4.route-metric 20 # This connection is more prefered
nmcli con mod $con_name_2 ipv4.route-metric 40 # This connection is less prefered
# Disconnect and reconnect the networks to make the changes effective
nmcli con down $con_name_1
nmcli con down $con_name_2
nmcli con up $con_name_1
nmcli con up $con_name_2
You can also set ipv6.route-metric
for each network.