Disable Wifi when I plug in ethernet cable

I’ve been trying to find some info for this issue with little success. My problem is that I have a raspberry pi 3 that’s usually connected to the network thru wifi, but from time to time i plug in an ethernet cable so it has a faster connection, but when I connect the ethernet cable the RSpi keeps using the wlan connection.

I would like that there was a way to prioritize the ethernet connection over the wlan, and whenever ethernet is available use it instead of wireless.

Is there an easy way to achieve this?

Sorry, figured that i posted this on the wrong forum… should be moved to help


If you run the route -n command, you’ll see a column called metric. That is the priority of the interface, which is zero for both eth0 and wlan0. Zero is the highest priority, so, you’d want to give wlan0 a value of, say, 10. (Higher number = lower priority)

Here’s an example:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface         UG    0      0        0 eth0 UGH   0      0        0 wlan0 UGH   0      0        0 wlan0   U     0      0        0 tun0   U     0      0        0 eth0   U     0      0        0 wlan0 UH    0      0        0 wlan0 UH    0      0        0 eth0

There’s a command ifmetric that you can install to change the metric. It’s very simple to run:

osmc@osmc:~$ sudo ifmetric wlan0 10
osmc@osmc:~$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface         UG    0      0        0 eth0 UGH   10     0        0 wlan0 UGH   10     0        0 wlan0   U     0      0        0 tun0   U     0      0        0 eth0   U     10     0        0 wlan0 UH    0      0        0 eth0 UH    10     0        0 wlan0

The changes are lost after a reboot, so you’ll need to find some way to run it at startup. Using /etc/rc.local will unfortunately run too early, so it should be something that runs once networking is up.

1 Like

Thank you!!! i’ll try that!

Out of interest, what runs after rc.local that affects this? I have some ip routing commands in rc.local which have been working OK for me, but am I just lucky?

You got me thinking! Perhaps rc.local isn’t enabled in systemd. But it is.

osmc@osmc:~$ systemctl status rc-local
● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; static)
   Active: active (exited) since Sun 2017-04-30 13:26:43 CEST; 2min 22s ago
  Process: 324 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)

I think a better question is: what is the state of networking when rc.local runs? I added two commands to rc.local to answer that question: ifconfig and route -n

osmc@osmc:~$ cat /tmp/rclocal
lo        Link encap:Local Loopback  
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

So just the loopback interface is up. And, yes, you should buy more lottery tickets. :wink:

Tried your neat trick and sometimes got eth0, sometimes not, but I’m doing some other things in rc.local before looking at the route table so maybe that delay made it work. Now added this to rc.local, which may help the OP:

while [ -z $(ip route) ]; do sleep 1; done

I think ip route is preferred these days to route and can be used to set the metric so maybe no need to install ifmetric.

The if* and route commands are on the way out, but you’ll still need to install iproute2, since it’s not in the default OSMC build.

Here’s a discussion about the merits of rc.local vs systemd on Raspbian: Jessie: rc.local vs. systemd - Raspberry Pi Forums

Yes, systemd must be the right answer - if only I could get my head round it. :wink: