Setting up WiFi over SSH, without using DietPi-config

Have some feedback, questions, suggestions, or just fancy a chat? Pop it in here.
User avatar
MichaIng
Site Admin
Posts: 2295
Joined: Sat Nov 18, 2017 6:21 pm

Re: Setting up WiFi over SSH, without using DietPi-config

Post by MichaIng »

@sibero80
Reasonable idea. Currently DietPi(-Config) does not handle well two WiFi devices.

The currently used iptables rules /etc/iptables.ipv4.nat will always use the Ethernet device to forward connected client requests to.

So what would be required is using a different iptables rule set based on which interface is enabled and remove the other rule set instead. As well to avoid interferences and have one default gateway only at a time, eth0 and wlan1 should be never up/configured in parallel.
I am not sure about the bond setup you did, but you could try the following:

Code: Select all

# Local
auto lo
iface lo inet loopback

# Wifi Hotspot
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0

# Ethernet
allow-hotplug eth0
iface eth0 inet static
address 192.168.0.18
netmask 255.255.255.0
gateway 192.168.0.1
dns-nameservers 8.8.8.8
# - De-configure wlan1 before starting eth0
pre-up ifdown --force wlan1
# - Enable forwarding from wlan0 AP
up iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
up iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
up iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
# - When interface is de-configured, remove forwarding as well
down iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
down iptables -D FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
down iptables -D FORWARD -i wlan0 -o eth0 -j ACCEPT

# Wifi Fallback
allow-hotplug wlan1
iface wlan0 inet dhcp
wireless-essid WIFI_SSID
wireless-key WIFI_KEY
wireless-mode Managed
wireless-power off
wpa-ssid WIFI_SSID
wpa-psk WIFI_KEY
# - Stop if eth0 is connected
pre-up ! grep -qs 'up' /sys/class/net/eth0/operstate
# - De-configure eth0 before starting wlan1
pre-up ifdown --force eth0
# - Enable forwarding from wlan0 AP
up iptables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE
up iptables -A FORWARD -i wlan1 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
up iptables -A FORWARD -i wlan0 -o wlan1 -j ACCEPT
# - When interface is de-configured, remove forwarding as well
down iptables -t nat -D POSTROUTING -o wlan1 -j MASQUERADE
down iptables -D FORWARD -i wlan1 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
down iptables -D FORWARD -i wlan0 -o wlan1 -j ACCEPT
For sure there will be a more elegant way, but in theory it contains what is required:
- If eth0 is connected, wlan1 will never be configured. I hope when bringing up the interfaces on boot, networking.service respects the order so eth0 will always be configured first.
- If eth0 is not connected, wlan1 will be configured and as first step the obsolete eth0 will be de-configured to avoid interferences.
- eth0, when being configured, as well assures that wlan1 is down.
- Both interfaces will apply correct AP forwarding when being configured and remove them when being de-configured.

In combination the above assures that eth0 and wlan1 are never up together and that there is always only one AP forwarding rule set active at a time.

Only thing, when eth0 is disconnected and wlan1 up, then you plug eth0, it is not configured automatically. ifup eth0 is required to achieve that. Alternatively the ifplugd package can do that job: https://packages.debian.org/de/stretch/ifplugd. It only needs to be assured that it only handles eth0 and not wlan1 (which it is obviously capable of).

Note that when using dhcp, address+netmask+gateway are ignored anyway and dns-nameservers overridden in most cases. All this info is only required for static mode and retrieved via dhcp automatically otherwise.



I was now trying to understand the bonding setup:
- This is actually nice as well if you have multiple adapters/interfaces on the same network and only one should be used.
- But the issue in case of WiFi Hotspot is that the iptables forwarding rules need to be set. And when watching the route output of Fourdee, I think it is not possible to set the rules for the bond0 interface. Although it could be tested.
- So you could try to use the interfaces file you posted with bond0 setup and then check if this works:

Code: Select all

# Remove existing rules first
iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
iptables -D FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i wlan0 -o eth0 -j ACCEPT
# Re-add them with bond0 as www interface instead
iptables -t nat -A POSTROUTING -o bond0 -j MASQUERADE
iptables -A FORWARD -i bond0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i wlan0 -o bond0 -j ACCEPT
- If then connected clients can access www regardless of eth0 being currently connected or not, this is of course a much easier config with less overhead.

Actually it seems to work indeed: https://unix.stackexchange.com/a/343224

So after testing the above to make these forwarding rules persistent:

Code: Select all

iptables-save > /etc/iptables.ipv4.nat
sibero80
Posts: 3
Joined: Sat Mar 30, 2019 5:06 am

Re: Setting up WiFi over SSH, without using DietPi-config

Post by sibero80 »

Thank you @MichaIng ! Would have never thought about everything you took into account.
I'll be giving your settings and suggestions a try as soon as mi second adapter arrives in the mail and report back with test results.
User avatar
MichaIng
Site Admin
Posts: 2295
Joined: Sat Nov 18, 2017 6:21 pm

Re: Setting up WiFi over SSH, without using DietPi-config

Post by MichaIng »

@sibero80
Great. This would be also good as a start to make DietPi-Config more flexible, enabling/disabling single network devices and as well have multiple ones active in parallel. If this bonding works well then this is great if you want to use multiple adapters for the same purpose indeed.
bricolodu13
Posts: 26
Joined: Fri Oct 23, 2015 11:51 am

Re: Setting up WiFi over SSH, without using DietPi-config

Post by bricolodu13 »

Hello,

I'm reopening this old post.

I'm trying to implement a script that allow to go from HotSpot mode to Wifi AP mode.
I don't want to use DietPi-config as in Hotspot mode the pi provides a webpage to enter the wifi AP mode credentials and then autoconnect to the selected SSID.

Unfortuantely, I've not yet found a way to do so... :cry:

Here is my code for review and comment.
Thank you

Code: Select all

/DietPi/dietpi/dietpi-services stop
#Remove Hotspot
apt-get remove -y hostapd isc-dhcp-server
apt --purge autoremove -y

#How to inform DietPi ???
WIFI_HOTSPOT=0 # ?

#Create /etc/network/interfaces
cat << _EOF_ > /etc/network/interfaces
# 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 dhcp
address 192.168.1.26
netmask 255.255.255.0
gateway 192.168.1.254
#dns-nameservers 127.0.0.1

# WiFi
#allow-hotplug wlan0
iface wlan0 inet dhcp
address 192.168.0.100
netmask 255.255.255.0
gateway 192.168.0.1
wireless-essid Home
wireless-key jaimelesansfil
wireless-mode Managed
wireless-power off
wpa-ssid Home
wpa-psk jaimelesansfil
#dns-nameservers 8.8.8.8 8.8.4.4
_EOF_

/DietPi/dietpi/func/dietpi-set_hardware wifimodules enable

# Hardware
WIFI_HARDWARE=1
onboard_wifi_enabled=1
WIFI_DISABLED=0

#Network_ApplyChanges
/DietPi/dietpi/func/dietpi-set_hardware wifimodules onboard_enable
/DietPi/dietpi/func/dietpi-set_hardware preferipv4 1

# - Update WiFi db/wpa_supplicant
/DietPi/dietpi/func/dietpi-wifidb 1

# Update Current Mode for network restart
#ETH_MODE=0
WIFI_MODE=1 #0=static 1=dhcp

#WIFI_COUNTRYCODE='FR'
/DietPi/dietpi/func/dietpi-set_hardware wificountrycode "FR"

# Restart net
# Update WiFi db/wpa_supplicant
/DietPi/dietpi/func/dietpi-wifidb 1

/DietPi/dietpi/dietpi-services start
ifup wlan0
bricolodu13
Posts: 26
Joined: Fri Oct 23, 2015 11:51 am

Re: Setting up WiFi over SSH, without using DietPi-config

Post by bricolodu13 »

OK, I think I made it.

Thank you
Post Reply