Bosse_B
Posts: 1086
Joined: Thu Jan 30, 2014 9:53 am

RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 7:00 am

Can I set up my Rpi4 (with Raspbian Buster Lite) such that it on boot will connect eth0 and then only if eth0 does not connect fall back to WiFi?
Like this:
Ethernet is wired up and receives a DHCP connection on boot: don't look at WiFi at all.
Ethernet is not connecting (no cable) then and only then: use WiFi.

The Rpi4 box will travel and if it can connect by Ethernet I prefer that but if Ethernet is missing a number of WiFi networks are pre-configured in wpa_supplicant.conf such that it can use them in the order they are listed in the conf file.

I don't want both network interfaces active at the same time.

If this can be done then please advice how.
Bo Berglund
Sweden

User avatar
kerry_s
Posts: 1789
Joined: Thu Jan 30, 2020 7:14 pm

Re: RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 8:40 am

i'm not sure i get where your going, ethernet always works when connected.
if there's no ethernet then it just boots and waits, you can then use raspi-config to setup wifi.

another option is to have the wpa_supplicant.conf already made up and just put it in the /boot folder, it will auto load at boot, just like when you want to have ssh, you just create a file named ssh & it auto starts.

Bosse_B
Posts: 1086
Joined: Thu Jan 30, 2014 9:53 am

Re: RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 9:41 am

kerry_s wrote:
Sun Mar 15, 2020 8:40 am
i'm not sure i get where your going, ethernet always works when connected.
I do know this and it will be my preferred connection on this headless RPi4.
However, if it is not possible to connect a wire to the router but a known WiFi is available, then I want it to connect to the WiFi instead
if there's no ethernet then it just boots and waits, you can then use raspi-config to setup wifi.
No, I cannot do that since I will not have a connection to the RPi4. It is headless and I need to use PuTTY (SSH) to connect to it. Note: NO keyboard/monitor available!
another option is to have the wpa_supplicant.conf already made up and just put it in the /boot folder, it will auto load at boot, just like when you want to have ssh, you just create a file named ssh & it auto starts.
As I tried to explain I do have the /etc/wpa_supplicant/wpa_supplicant.conf file prepared for the 6-7 known WiFi networks I can encounter on location.
But I want WiFi to be connected ONLY if Ethernet is not connected!

So my question is if it is possible to configure the RPi4 such that WiFi is NOT used if there is an Ethernet cable connected even though /etc/wpa_supplicant/wpa_supplicant.conf contains info on a WiFi network present on location?
Only when eth0 does not connect should WiFi be tried.
Bo Berglund
Sweden

User avatar
kerry_s
Posts: 1789
Joined: Thu Jan 30, 2020 7:14 pm

Re: RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 10:30 am

1. no connection = no ssh
2. as i said, put the wpa file in boot.
3. Ethernet is always used first, the prefered, so it doesn't matter if there's wifi or not. you can still have the wpa file in there.

everything you want is what it does already as the default behavior. i do understand headless i use 1 everyday.

Bosse_B
Posts: 1086
Joined: Thu Jan 30, 2014 9:53 am

Re: RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 11:32 am

kerry_s wrote:
Sun Mar 15, 2020 10:30 am
1. no connection = no ssh
2. as i said, put the wpa file in boot.
Why in /boot? The WiFi configuration is already present in /etc/wpa_supplicant/
And any such file in /boot will be erased on first boot after it was put there.
3. Ethernet is always used first, the preferred, so it doesn't matter if there's wifi or not. you can still have the wpa file in there.
Are you saying that if eth0 connects on boot the RPi4 will not bring up WiFi even though there is a match in wpa_supplicant.conf with an on-air SSID? This is the behavior I am looking for...
everything you want is what it does already as the default behavior. i do understand headless i use 1 everyday.
OK, I will have to test it.
I was assuming that the RPi4 would become dual homed in this situation and that is what I want to avoid.
No WiFi if not nbeeded...
Bo Berglund
Sweden

MrEngman
Posts: 4115
Joined: Fri Feb 03, 2012 2:17 pm
Location: Southampton, UK

Re: RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 11:46 am

I have several Pi which have ethernet cable and wifi enable and both get connected when they boot.

What you could try is when your Pi boots run a script that checks command ifconfig eth0 to see if a network address is active and if it is turn off your wifi using a command like sudo ip link set wlan0 down.


MrEngman
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra

Please post ALL technical questions on the forum. Please Do Not send private messages.

dgordon42
Posts: 795
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 12:18 pm

@Bosse_B,

As far as I know, your Pi will use an Ethernet connection in preference to a WiFi connection for traffic when both interfaces are present.

If your Pi has both an Ethernet connection and a WiFi connection, run the "route-n" command to see where the Pi will send it's traffic. It should use the connection with the lowest metric.

You can test this by running the "ifconfig" command, downloading something from the internet, run the "ifconfig" command again, and then compare the results to see which interface carried the traffic.

If you need your Pi to disconnect from (as opposed to not use) WiFi when an Ethernet connection is present, I would go with @Mr.Engman's suggestion above and write a script to do that.

Hope this helps,
Dave.

Bosse_B
Posts: 1086
Joined: Thu Jan 30, 2014 9:53 am

Re: RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 2:18 pm

I just checked what happens when there is a network cable attached to the RPi4.
Result:
Both eth0 and wlan0 are now connected with different IP addresses on the same local network, i.e. the RPi4 has become dual-homed.
Not what I wanted.

So I have tried to add a check script to the boot sequence as suggested...

Added to /etc/rc.local:

Code: Select all

#Check if eth0 is connected, if so disable WiFi
/home/pi/bin/check_eth &
Content of /home/pi/bin/check_eth:

Code: Select all

#!/bin/bash
#This script checks if eth0 can connect, if so disable WiFi
#Let system get up first, default wait: 30s:
#Check for initial delay on command line

if [ -z "$1" ]; then
  DELAY="30"
else
  DELAY="$1"
fi
sleep $DELAY

EthAddr=$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
WiFiAddr=$(ip addr show wlan0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)

if [ -z $EthAddr ]; then
  echo "IP addr of eth0 not found, so use WiFi"
  echo "wlan0 IP=$WiFiAddr"
else
  echo "Using Ethernet! Switch off WiFi."
  echo "eth0  IP=$EthAddr"
  echo "wlan0 IP=$WiFiAddr"
  cmd="ifconfig wlan0 down"
  $cmd
fi
With this in place wlan0 is shut off if eth0 gets a DHCP address on startup.
Otherwise it will connect by WiFi if possible.
Bo Berglund
Sweden

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 6066
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 2:39 pm

Bosse_B wrote:
Sun Mar 15, 2020 2:18 pm
I just checked what happens when there is a network cable attached to the RPi4.
Result:
Both eth0 and wlan0 are now connected with different IP addresses on the same local network, i.e. the RPi4 has become dual-homed.
Not what I wanted.
...
Whilst that's not what you wanted, OOC why is it a "problem"?
Virtually all of my Pi's are set up to boot to command-line w/o auto login, have their IP addresses associated with their mac addresses and assigned by DHCP via the router. If I want to remotely login by SSH or VNC if they have a wired connection I use it's IP since it has priority etc. If not, that of the WiFi. I'm guessing your scenario(s) are different...
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's & P400. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Bosse_B
Posts: 1086
Joined: Thu Jan 30, 2014 9:53 am

Re: RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 5:49 pm

FTrevorGowen wrote:
Sun Mar 15, 2020 2:39 pm
Bosse_B wrote:
Sun Mar 15, 2020 2:18 pm
I just checked what happens when there is a network cable attached to the RPi4.
Result:
Both eth0 and wlan0 are now connected with different IP addresses on the same local network, i.e. the RPi4 has become dual-homed.
Not what I wanted.
...
Whilst that's not what you wanted, OOC why is it a "problem"?
Virtually all of my Pi's are set up to boot to command-line w/o auto login, have their IP addresses associated with their mac addresses and assigned by DHCP via the router. If I want to remotely login by SSH or VNC if they have a wired connection I use it's IP since it has priority etc. If not, that of the WiFi. I'm guessing your scenario(s) are different...
Trev.
Well, I am making a system template which will be used in different locations and on some the Pi should not connect to WiFi but on others WiFi is all there is.
I want to make sure I can switch off WiFi at boot if it is not needed (Ethernet present) so it does not connect a backdoor.
In one of the implementations it will be connected to a router, which is connected as client to another router which is hooked to a fiber box to the Internet.
It is an attempt at isolation when it turned out that the ISP would not issue another public IP when we plugged the RPi or the extra router directly to the fiber box....
If that had succeeded I would not have used the router at all, just installed ufw on the RPi and let it swim on the Internet proper. But the ISP said that they could not offer more than one public IP on the network...
So now it will be behind the second router and I don't want it to connect WiFi to the top router, because it would make that network accessible...
Bo Berglund
Sweden

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 6066
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: RPi4 using WiFi only if Ethernet connection missing?

Sun Mar 15, 2020 6:29 pm

Bosse_B wrote:
Sun Mar 15, 2020 5:49 pm
FTrevorGowen wrote:
Sun Mar 15, 2020 2:39 pm
Bosse_B wrote:
Sun Mar 15, 2020 2:18 pm
I just checked what happens when there is a network cable attached to the RPi4.
Result:
Both eth0 and wlan0 are now connected with different IP addresses on the same local network, i.e. the RPi4 has become dual-homed.
Not what I wanted.
...
Whilst that's not what you wanted, OOC why is it a "problem"?
Virtually all of my Pi's are set up to boot to command-line w/o auto login, have their IP addresses associated with their mac addresses and assigned by DHCP via the router. If I want to remotely login by SSH or VNC if they have a wired connection I use it's IP since it has priority etc. If not, that of the WiFi. I'm guessing your scenario(s) are different...
Trev.
Well, I am making a system template which will be used in different locations and on some the Pi should not connect to WiFi but on others WiFi is all there is.
I want to make sure I can switch off WiFi at boot if it is not needed (Ethernet present) so it does not connect a backdoor.
In one of the implementations it will be connected to a router, which is connected as client to another router which is hooked to a fiber box to the Internet.
It is an attempt at isolation when it turned out that the ISP would not issue another public IP when we plugged the RPi or the extra router directly to the fiber box....
If that had succeeded I would not have used the router at all, just installed ufw on the RPi and let it swim on the Internet proper. But the ISP said that they could not offer more than one public IP on the network...
So now it will be behind the second router and I don't want it to connect WiFi to the top router, because it would make that network accessible...
O.K. I understand the situation better now. Reminds me of the (before ISP provided WiFi/routers) situation I had in our old house quite a few years ago. In those days only one, known/identified, computer was allowed to be connected via the ISP's box to the internet. Routers & switches were not generally/cheaply available to individuals. The solution was to install two or more network cards in a "tower" P.C. running a Smoothwall (IIRC) Linux-based router/firewall. Having previously "had the floorboards up" to extend/install a second telephone socket network for the older Dial-Up connection I used that access to add a set of ethernet cabling and sockets which was still in use, albeit now via an ISP provided router and extra, piggy-backed WiFi routers (sub-nets) before we moved in 2018.
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's & P400. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

K8ILB
Posts: 1
Joined: Tue Nov 17, 2020 11:21 pm

Re: RPi4 using WiFi only if Ethernet connection missing?

Tue Nov 17, 2020 11:35 pm

I tried Bo Berglund's script in rc.local as well as several variations including rfkill block wlan0. None work with current Raspbian on my Pi 4s.

I want this to work because i often transfer data among several RPis and want them to use wired ethernet. Also, I do most programming and system maintenance from MacBooks or iPads using ssh and want to attach via hostname, not IP number.

Nslookup finds only the wlan IP so transfers from host to host default to wireless -- not what I want.

Manually turning off WiFi later does not work because the nslookup result persists until, I assume, lease time expires.

I do occasionally boot a RPi while not connected to ethernet in which case I do want wifi to be turned on.

Does anybody have this working at boot time?

Return to “General discussion”