MichaIng / DietPi

Lightweight justice for your single-board computer!
https://dietpi.com/
GNU General Public License v2.0
4.91k stars 499 forks source link

How to setup WiFi hotspot on boot if Ethernet link is down? #5879

Open DrCWO opened 2 years ago

DrCWO commented 2 years ago

Creating a bug report/issue

Required Information

Additional Information (if applicable)

Steps to reproduce

Expected behaviour

I can set a static IP address for wlan0 in /etc/network/interfaces But the file tells me to use dietpi-config to set it. I can't find where to set it.

Actual behaviour

Extra details

I want to implement a feature that first runs dietpi as a hotspot. In the mode the user can connect via a http interface I offer via script. In this page the user shall be able to enter the AP in his network where the Pi should connect to After connection the Pi shall reboot and connect to this AP instead of offering an Ap by itself.

Question: How to disable WiFi Hotspot and reenable mormal WiFi mode so it I can connect via /etc/wpa_supplicant/wpa_supplicant.conf settings?

MichaIng commented 2 years ago

You can change the IP in /etc/network/interfaces and for the DHCP server in /etc/dhcp/dhcpd.conf. Also you need to change the related NAT iptables rule, like:

sed -i 's/192\.168\.42/192.168.123/g' /etc/network/interfaces /etc/dhcp/dhcpd.conf
iptables -t nat -D POSTROUTING -s 192.168.42.0/24 -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 192.168.123.0/24 -o eth0 -j MASQUERADE
iptables-save > /etc/iptables.ipv4.nat
reboot

Indeed a way to set that IP on WiFi Hotspot install or via dietpi-config is currently missing.

How to disable WiFi Hotspot and reenable mormal WiFi mode so it I can connect via /etc/wpa_supplicant/wpa_supplicant.conf settings?

dietpi-software uninstall 60
dietpi-config 8 1 # to setup regular WiFi
DrCWO commented 2 years ago

Michael, thank you for clarifying this! I already thought I was too blind to find the setting in dietpi-config ;-)

My last question was more about to only disable (not uninstall) the AP mode (60) and reenable the normal WiFi connection mode. Maybe I was not clear enough in describing this. I have to do this with a script. Please let me explain my use case:

This means I need a way to disable/enable AP mode and disable/enable WiFi connection mode with a script. Here is a description how it can be done with Raspbian but this did not work with DietPi :-(

Best DrCWO

Joulinar commented 2 years ago

You could have a look to our install and uninstall steps we do

https://github.com/MichaIng/DietPi/blob/master/dietpi/dietpi-software#L8362 https://github.com/MichaIng/DietPi/blob/master/dietpi/dietpi-software#L14601

Maybe this can give an idea

MichaIng commented 2 years ago

Here are instructions extracted, with some enhancements also for Wi-Fi 5: https://dietpi.com/forum/t/dietpi-as-wifi-router/14679/33?u=michaing

It is for using with two WiFi adapters, sharing Internet through one of them. Let's adjust this a little for your case, where no shared internet is needed and wlan0 as host adapter:

apt install hostapd isc-dhcp-server
systemctl unmask hostapd
systemctl disable --now hostapd isc-dhcp-server # You do not want them to start automatically at boot

# DHCP server settings
cat << '_EOF_' > /etc/dhcp/dhcpd.conf
ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;

subnet 192.168.42.0 netmask 255.255.255.0 {
        range 192.168.42.10 192.168.42.50;
        option broadcast-address 192.168.42.255;
        option routers 192.168.42.1;
        option domain-name "local";
        option domain-name-servers 9.9.9.9, 149.112.112.112;
}
_EOF_
echo 'INTERFACESv4="wlan0"' > /etc/default/isc-dhcp-server

# Network interface
cat << '_EOF_' > /etc/network/interfaces.d/wlan0.conf
# No allow-hotplug/auto wlan0, as you do not want this to be configured automatically
iface wlan0 inet static
address 192.168.42.1/24
wireless-power off
_EOF_

# hostapd
cat << '_EOF_' > /etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
ssid=yourSSID
hw_mode=g
ieee80211n=1
ieee80211ac=1
wmm_enabled=1
channel=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=yourPassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
_EOF_
chmod 0600 /etc/hostapd/hostapd.conf
echo 'DAEMON_CONF="/etc/hostapd/hostapd.conf"' > /etc/default/hostapd

Now, on boot, you could do something like this to bring up the WiFi AP if not Ethernet cable is connected:

#!/bin/dash
ethtool eth0 | grep -q 'Link detected: yes' && exit 0
ifup wlan0
systemctl start hostapd isc-dhcp-server