Using ethernet + WLAN interfaces (not at the same time) in v6.30

Guides and tutorials for various stuff. Posted by DietPi users.
Post Reply
Akrai
Posts: 21
Joined: Fri Jun 19, 2020 9:43 am

Using ethernet + WLAN interfaces (not at the same time) in v6.30

Post by Akrai »

I wanted to use my raspberry 4 connected with ethernet and sometimes bring it to the tv where there is no ethernet cable and i have to use WLAN. In order to do it reliably and automatically, emulating what you get in desktop computers like a laptop, which disconnects to wifi when connected to ethernet and connects to wifi when there is no ethernet connected, i used ifplugd. Also, using this method you can have both interfaces with the same static ip, which may or may not work but it is up to your router (if you connect with one interface, and then disconnect it and connect with the second one, the router will probably reject the second connection or static ip assignation, as the first one, with the same ip, has been connected too recently and the router would probably think it is still connected. You would have to wait longer until connecting with the second one, or just reboot the router and connect with the second interface).

Note: this setup works reliably to change between one interface and the another, but i haven't tested the autoconnect wifi system of dietpi, and so right now i have been using this setup without autoconnect, and for several days it has been connected without any interruption but theoretically, right now, if for some reason my raspberry got any wifi interruption or whatever, it should stay disconnected to the wlan. This hasn't happened yet, but theoretically this setup doesn't have a WLAN autoreconnection system running.

Okay so steps from a clean flash (modify as you want to apply it to a running system):

Clean flash, and as I know two active interfaces have been problematic to me, I went throught the initial setup only with ethernet

Then I installed ifplugd:

Code: Select all

apt-get install ifplugd
to have it already installed in case internet connection doesnt work at some point throught the process, then i just set the internet using the dietpi-config for both ethernet, with static ip (you can use dynamic if you want), and then a reboot, and then the wifi with static (same, dynamic if wished) and both ssid, the country, the inboard wifi (in my case), and then a reboot, and then proceed to edit the config files of wpa_supplicant (it doesnt have to be modified, dietpi-config does it right, but i check it just in case, to see everything is set correctly; this file is where every wlan connection is configured, this is, the ssid and the passphrase, basically), /etc/network/interface (this file is where you write what kind of ip you want to get in each interface; for this setup, you must comment the hotplugging of eth0, and checking that both interfaces has correct static ips if you want static, or dynamic in any of those interfaces you want)
My /etc/network/interfaces file:

Code: Select all

# Location: /etc/network/interfaces
# Please modify network settings via: dietpi-config
# Or create your own drop-ins in: /etc/network/interfaces.d/

# Drop-in configs
source interfaces.d/*

# Local
auto lo
iface lo inet loopback

# Ethernet
#allow-hotplug eth0
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
#dns-nameservers 1.1.1.1 1.0.0.1

# WiFi
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
#dns-nameservers 1.1.1.1 1.0.0.1
wireless-power off
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Check that /etc/default/crda has REGDOMAIN=ES (for Spain, for other countries you have to check http://www.summitdata.com/Documents/Reg ... omains.pdf ), and finally edit /etc/ifplugd/ifplugd.action to contain:

Code: Select all

#!/bin/sh
set -e
if [ -z "$1" ] || [ -z "$2" ] ; then
       echo "Wrong arguments" > /dev/stderr
       exit 1
fi
if [ "$1" != "eth0" ]; then
       echo "Wrong interface!" > /dev/stderr
       exit 1
fi
if [ "$2" = "up" ] ; then
       echo "bring down WiFi"
       /sbin/ifdown wlan0 --force
       /sbin/ifup eth0 --force
       exit 0
elif [ "$2" = "down" ] ; then
       echo "bring up WiFi"
       /sbin/ifdown eth0 --force
       /sbin/ifup wlan0 --force
       exit 0
fi
exit 1
and /etc/default/ifplugd

Code: Select all

INTERFACES="eth0"
HOTPLUG_INTERFACES="auto"
ARGS="-q -f -u0 -d5 -w -I"
SUSPEND_ACTION="stop"
finally,
systemctl enable ifplugd.service
systemctl start ifplugd.service
reboot
and voilá, it is working
User avatar
Joulinar
Posts: 2072
Joined: Sat Nov 16, 2019 12:49 am

Re: Using ethernet + WLAN interfaces (not at the same time) in v6.30

Post by Joulinar »

some more explanation on the settings done within the config files

viewtopic.php?p=25620#p25620
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
Akrai
Posts: 21
Joined: Fri Jun 19, 2020 9:43 am

Re: Using ethernet + WLAN interfaces (not at the same time) in v6.30

Post by Akrai »

Okay so how to add autoreconnect wlan feature? easy, we add two lines that just enables or disables the dietpi-wifi-monitor.service when ethernet is plugged or unplugged, in the /etc/ifplugd/ifplugd.action file, so replace the content of that file with

Code: Select all

set -e
if [ -z "$1" ] || [ -z "$2" ] ; then
       echo "Wrong arguments" > /dev/stderr
       exit 1
fi
if [ "$1" != "eth0" ]; then
       echo "Wrong interface!" > /dev/stderr
       exit 1
fi
if [ "$2" = "up" ] ; then
       echo "bring down WiFi"
       /sbin/ifdown wlan0 --force
       /sbin/ifup eth0 --force
       /bin/systemctl disable --now dietpi-wifi-monitor.service
       exit 0
elif [ "$2" = "down" ] ; then
       echo "bring up WiFi"
       /sbin/ifdown eth0 --force
       /sbin/ifup wlan0 --force
       /bin/systemctl enable --now dietpi-wifi-monitor.service
       exit 0
fi
exit 1
(or just add the 2 systemctl lines in the correct places)
And reboot.
Post Reply