Setting up WiFi over SSH, without using DietPi-config

This is what dietpi-config says

DietPi - Network Options: Adapters ├─────────────┐
│ │
│ Ethernet : Available | Enabled | Connected │
│ Wifi : Available | Enabled | Disconnected │
│ IPv6 : Disabled │
│ Internet : Please run Internet Test │
│ Proxy : Disabled │

Under DietPi - Wireless Network Options my ssid is correct, pw is correct and mode is dhcp.

Why is it not connecting?
Gord_W

Edit: Current interfaces file
root@DietPi:/etc/network# cat interfaces
#/etc/network/interfaces
#Please use DietPi-Config to modify network settings.

Local

auto lo
iface lo inet loopback

Ethernet

allow-hotplug eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4

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 Williams
wireless-key xxx
wireless-mode Managed
wireless-power off
wpa-ssid Williams
wpa-psk xxx
#dns-nameservers 8.8.8.8 8.8.4.4

What I am eventually hoping to do is have ethernet on 10.0.0.x network and wifi on 192.168.1.x network.

I change the above interfaces file to:

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 Williams
#wireless-key xxx
wireless-mode Managed
wireless-power off
wpa-ssid Williams
wpa-psk xxx
#dns-nameservers 8.8.8.8 8.8.4.4

And the wifi is working to the point that I can ssh into .103 but it is back to the original problem with wlan0 is getting the Rx and the Tx reply is coming back through the ethernet. :imp:

Here is some information that is way beyond me. Maybe someone can decipher it and explain what is happening.

root@DietPi:~# ip route show
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.103

root@DietPi:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0

root@DietPi:~# ip link list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether b8:27:eb:9e:47:6e brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DORMANT group default qlen 1000
link/ether b8:27:eb:cb:12:3b brd ff:ff:ff:ff:ff:ff

:thinking: What is state UP mode DEFAULT/DORMANT above?

ARP
root@DietPi:~# ip neigh show
192.168.1.101 dev wlan0 lladdr 00:22:15:c6:68:4a STALE
192.168.1.101 dev eth0 lladdr 00:22:15:c6:68:4a REACHABLE
192.168.1.1 dev eth0 lladdr 64:66:b3:f4:35:f8 STALE

:open_mouth: So, I unplugged the eithernet expecting the whole thing to stop working but I was able to ssh in with the wifi. Go figure!

Anyway, enough for tonight.

I left it on overnight and came back this morning. Ethernet unplugged, using wifi. Everything working so I thought. Then tried to download package from internet and found that external connections were not working. Only local network.

I halted and powered down and up. Tried to ssh through wifi and couldn’t get a connection. ssh: connect to host 192.168.1.103 port 22: Connection timed out NOTE! it didn’t say “No route to host”

Router Wireless Status says MAC address is associated and small number of packets sent and received.
Router DHCP says dietpi with correct MAC is at 192.168.1.103

So I’m guessing but I suspect, even though the eithernet cable is disconnected it is trying to send ssh replies back to eth0 instead of wlan0 as have shown before in my earlier posts.

Something is seriously wrong here.

Looking for help or suggestions here. I’m a beginner and this is beyond my capabilities.

When I couldn’t get to the internet and before I powered down and up discussed above I did a few tests.

root@DietPi:~# ip route show
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.103

root@DietPi:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0

Note this was - first entry now missing.

root@DietPi:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0

root@DietPi:~# ip neigh show
192.168.1.101 dev wlan0 lladdr 00:22:15:c6:68:4a REACHABLE
192.168.1.1 dev wlan0 lladdr 64:66:b3:f4:35:f8 DELAY

root@DietPi:~# ping http://www.google.com
connect: Network is unreachable

root@DietPi:~# ip neigh show
192.168.1.101 dev wlan0 lladdr 00:22:15:c6:68:4a REACHABLE
192.168.1.1 dev wlan0 lladdr 64:66:b3:f4:35:f8 REACHABLE

I’m also having problems setting up a rpi3 with wifi after using ethernet for a time headless.
Looking for help or suggestions here.

Hi Gordon,

Lets take a look at this for you.

Ok, so from what I can read, WiFi isn’t working correctly, no route to external sources? If that is the case its most likely a DNS/routing issue. As for the cause, not sure yet, but we can run some tests.

Few questions and tests:

  • Check signal strength (dB) on the RPi, maybe its dropping out:
iwconfig
  • Is this onboard WiFi on RPi 3, or, USB ? (sorry if i missed you mention this in another post, cant find it)
  • Have you tried using static IP on WiFi?
  • Try removing netplug and reboot system:
apt-get purge netplug -y

Just to recap the above posts:

  1. rpi3 connected to same AP with one wireless, one wired
  2. Seems like wired gets rooting priority. Wired gets gateway. Any info received on wireless the reply comes back on wired. i.e. wireless Rx operates, but Tx does not and every goes through wired Tx.
  3. When wired is unplugged. The Tx and gateway don’t get activated on wireless. Can sent info into device but get no reply from device.
  4. Tried static and dchp.
  5. No problem with wifi signal. Good signal strength, no errors.

So the main problem is switching over from wired to wireless when using the same AP. See the routing table in the above posts.

I currently have ethernet on 10.0.0.x network and wifi on 192.168.1.x network. This is working as expected. The networks are being used independently and I’m not expecting any switching to occur so this is a different situation.

From what I recall removing netplug didn’t make any difference.

Ah yes, does not fallback to wlan when eth is unplugged.

Lets try bonding eth0 and wlan0:
Edit your /etc/network/interfaces file to this (change both WIFI_KEY and ssid’s (FuzonWifi) entries):

#/etc/network/interfaces
#Please use DietPi-Config to modify network settings.

# Local
auto lo
iface lo inet loopback

# Ethernet
allow-hotplug eth0
iface eth0 inet dhcp
address 192.168.0.100
netmask 255.255.255.0
gateway 192.168.0.1
#dns-nameservers 8.8.8.8 8.8.4.4
    bond-master bond0
    bond-primary eth0
    bond-mode active-backup

# 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 FuzonWifi
wireless-key WIFI_KEY
wireless-mode Managed
wireless-power off
wpa-ssid FuzonWifi
wpa-psk WIFI_KEY
#dns-nameservers 8.8.8.8 8.8.4.4
    bond-master bond0
    bond-primary eth0
    bond-mode active-backup

# Define bond
auto bond0
iface bond0 inet dhcp
    bond-slaves none
    bond-primary eth0
    bond-mode active-backup
    bond-miimon 100

Reload with SystemD

systemctl daemon-reload

Reboot system:

reboot

Now when you unplug eth, you should get route:

root@DietPi:~# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.0.1     0.0.0.0         UG    0      0        0 wlan0
192.168.0.0     *               255.255.255.0   U     0      0        0 wlan0
192.168.0.0     *               255.255.255.0   U     0      0        0 eth0

Hello Everyone, greetings from Colombia!

Currently my Diet-pi is setup with the Hotspot package, using a Wifi dongle (wlan0) and Ethernet (eth0) as internet source.

I’m interested on adding a second WiFi adapter (wlan1) as I wish to be able to switch to wlan1 if internet eth0 gets disconnected.

This is my current setup:

#/etc/network/interfaces
#Please use DietPi-Config to modify network settings.

# Local
auto lo
iface lo inet loopback

# 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

# Wifi Hotspot
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0
#gateway 192.168.0.1
#dns-nameservers 8.8.8.8 8.8.4.4

# IP tables
up iptables-restore < /etc/iptables.ipv4.nat

From your previous post:

  1. Do yo thing the following setup would do the trick? (My second Wifi dongle has not arrived, so cannot test right now)
  2. How can I set up DHCP in wlan1 in a way that does not require the IP Adress, Netmask and Gateway to be specified=
#/etc/network/interfaces
#Please use DietPi-Config to modify network settings.

# Local
auto lo
iface lo inet loopback

# 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
    bond-master bond0
    bond-primary eth0
    bond-mode active-backup

# Wifi Hotspot
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0
#gateway 192.168.0.1
#dns-nameservers 8.8.8.8 8.8.4.4

# IP tables
up iptables-restore < /etc/iptables.ipv4.nat

# Wifi Fallback
allow-hotplug wlan1
iface wlan0 inet dhcp
address 192.168.0.100
netmask 255.255.255.0
gateway 192.168.0.1
wireless-essid WIFI_SSID
wireless-key WIFI_KEY
wireless-mode Managed
wireless-power off
wpa-ssid WIFI_SSID
wpa-psk WIFI_KEY
#dns-nameservers 8.8.8.8 8.8.4.4
    bond-master bond0
    bond-primary eth0
    bond-mode active-backup

# Define bond
auto bond0
iface bond0 inet dhcp
    bond-slaves none
    bond-primary eth0
    bond-mode active-backup
    bond-miimon 100

Thank you!

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:

# 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:
# 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:

iptables-save > /etc/iptables.ipv4.nat

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.

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.

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

/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

OK, I think I made it.

Thank you