11

Goal

Let ethernet take precedence over wireless when ethernet cable is plugged in

Method

After having done a fair amount of Googling and reading I've come to a point where I believe what I should be doing is something along the lines of

nmcli connection modify [id-of-ethernet-interface] ipv4.route-metric 200
nmcli connection modify [id-of-ethernet-interface] ipv6.route-metric 200

where 200 is a lower value than the wireless metric, to have the ethernet take precedence over the wireless.

Results

What perplexes me is the reports I get from route -n after I have executed the above commands and rebooted (for good measure), and the fact that this doesn't seem to amount to reaching my goal

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         123.456.89.1    0.0.0.0         UG    600    0        0 wlp1s0
0.0.0.0         123.456.89.1    0.0.0.0         UG    20200  0        0 enp0s31f6
123.456.89.0    0.0.0.0         255.255.255.192 U     200    0        0 enp0s31f6
123.456.89.0    0.0.0.0         255.255.255.192 U     600    0        0 wlp1s0
654.321.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enp0s31f6

The numbers add up with regards to my command execution, but for the lines that say

0.0.0.0         123.456.89.1    0.0.0.0         UG    20200  0        0 enp0s31f6
654.321.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enp0s31f6

The first line has 20 prefixed before the 200 value I set. This keeps being consistently applied based on what I run; If I change the value for the metric through nmcli to say 500, route -n will report 20500. Why does this happen? It sure doesn't look right, since I stated I wanted either 200 or 500, not 20200 and 20500.

The second line has a metric value that I have no idea where it comes from, and I can't seem to affect it at all. If anyone can shed light on this, I'm grateful.

It doesn't look like these commands end up in anything tangible, other than affecting the metrics; I cannot tell that that ethernet is taking precedence, so I assume it is not.

Other findings

What I have found curious, and seem to be working to some extent, is the use of $ sudo ifmetric enp0s31f6 200. This does two to three things;

  • It affects the metric of the interface (route -n reports all lines with the Ifaceenp0s31f6 to have the value 200)
  • It affects the UI in Ubuntu (In the upper right corner I will see a visual switch between ethernet and wireless icons switching, depending on the metric values I provide in the ifmetric command)
  • It sometimes throws a NETLINK: Error: File exists error at me. Subsequent executions of the same command may or may not result in this error

Some system info

  • EliteBook 850 G5
  • Ubuntu 18.04
  • Ubuntu install made through letting the installer use the entire disc, enabled encryption, enabled 3rd party downloads for drivers, etc.

Update #1

$ nmcli c show
NAME                UUID  TYPE      DEVICE    
Wired connection 2  [n/a] ethernet  enp0s31f6 
WiFi1               [n/a] wifi      wlp1s0

$ route -n
Destination     Gateway  Genmask         Flags Metric Ref    Use Iface
0.0.0.0         [n/a]    0.0.0.0         UG    600    0        0 wlp1s0
0.0.0.0         [n/a]    0.0.0.0         UG    20200  0        0 enp0s31f6
[n/a]           0.0.0.0  255.255.255.192 U     200    0        0 enp0s31f6
[n/a]           0.0.0.0  255.255.255.192 U     600    0        0 wlp1s0
[n/a]           0.0.0.0  255.255.0.0     U     1000   0        0 enp0s31f6

migrated from serverfault.com Jan 16 at 15:55

This question came from our site for system and network administrators.

  • Ethernet should be preferred by default. Strange. Is the output of nmcli c show the same as route -n's output? – Tommiie Jan 7 at 14:11
  • See my updated question. – shellström Jan 16 at 13:32
  • Please update your question with those result instead of dumping them in a comment. – Tommiie Jan 16 at 13:34
  • Yeah, I realised pretty fast, the dump into the comments weren't gonna work out. I'm making amendments to the edit. Give me 1 more minute, and you'll have the complete output. It's done. – shellström Jan 16 at 13:37
1

You have stacked problems here:

  • Your cable LAN and wireless LAN are a bridge to the same subnet 123.456.89.0/24
  • You will have two default gateways if you connect the same time at those networks(this can be solved with some advanced routing an ip rules)
  • Those gateways have THE SAME ADDRESS, since you have a bridge between wifi and cabled connection.

Maybe you should rely on external scripts to auto deactivate wifi when ethernet is plugged in like this one:

Create the script /etc/NetworkManager/dispatcher.d/70-wifi-wired-exclusive.sh. Contents:

#!/usr/bin/env bash

name_tag="wifi-wired-exclusive"
syslog_tag="$name_tag"
skip_filename="/etc/NetworkManager/.$name_tag"

if [ -f "$skip_filename" ]; then
  exit 0
fi

interface="$1"
iface_mode="$2"
iface_type=$(nmcli dev | grep "$interface" | tr -s ' ' | cut -d' ' -f2)
iface_state=$(nmcli dev | grep "$interface" | tr -s ' ' | cut -d' ' -f3)

logger -i -t "$syslog_tag" "Interface: $interface = $iface_state ($iface_type) is $iface_mode"

enable_wifi() {
   logger -i -t "$syslog_tag" "Interface $interface ($iface_type) is down, enabling wifi ..."
   nmcli radio wifi on
}

disable_wifi() {
   logger -i -t "$syslog_tag" "Disabling wifi, ethernet connection detected."
   nmcli radio wifi off
}

if [ "$iface_type" = "ethernet" ] && [ "$iface_mode" = "down" ]; then
  enable_wifi
elif [ "$iface_type" = "ethernet" ] && [ "$iface_mode" = "up"  ] && [ "$iface_state" = "connected" ]; then
  disable_wifi
fi

To disable the script, just execute touch /etc/NetworkManager/.wifi-wired-exclusive

Your Answer

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