3

I wanted my Raspberry Pi 3 to automatically connect to a known wifi, so I edited the file /etc/network/interfaces:

auto wlan0
iface wlan0 inet dhcp
wpa-ssid ssid
wpa-psk  password

and then I ran this command sudo dhclient wlan0

And it works fine but now when I want to connect it to my LAN, its not working properly. I am doing all things headless. So how can I configure my Pi so that it may 1st try to connect to my known wifi and if it fails, connect to my LAN?

Aurora0001
  • 6,357
  • 3
  • 25
  • 39
Ujjwal Gupta
  • 129
  • 4

1 Answers1

3

This is a typical failover scenario and this is handled by bonding. You can find information at Debian - bonding. Here for example I have made a tested configuration with Raspbian on a Raspberry Pi 3 that will bond wlan0 and eth0 and switch transparent between this two interfaces.

Who are interested for doing this with systemd-networkd can look at Howto migrate from networking to systemd-networkd with dynamic failover.

For the old style networking you have to configure /etc/network/interfaces but I couldn't get it together with dhcpcd. It's too complicated with network/interfaces and dhcpcd and wpa-supplicant and resolvconf and bonding. So for this example I disabled dhcpcd and use an old style networking configuration. First install needed packages:

pi@raspberrypi:~ $ sudo apt install ifenslave
pi@raspberrypi:~ $ sudo apt install resolvconf

Then setup /etc/network/interfaces like this:

pi@raspberrypi:~ $ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

# bond eth0 together with wlan0
# for status look at: cat /proc/net/bonding/bond0
auto bond0
iface bond0 inet static
    bond-slaves wlan0 eth0
    bond-primary wlan0
    bond-mode active-backup
    bond-miimon 100
    address 192.168.10.60
    netmask 255.255.255.0
    gateway 192.168.10.1
    dns-nameserver 192.168.10.10
    dns-search home.hoeft-online.de

allow-bond0 wlan0
iface wlan0 inet manual
    bond-give-a-chance 120
    wpa-bridge bond0
    wpa-ssid "wlan@hoeft-online.de"
    wpa-key-mgmt WPA-PSK
    wpa-psk "dontBelieveItsMyPw"
pi@raspberrypi:~ $

You should give this file chmod 600 /etc/network/interfaces.

Disable dhcpcd:

spi@raspberrypi:~ $ sudo systemctl stop dhcpcd
spi@raspberrypi:~ $ sudo systemctl disable dhcpcd

Reboot

Look with cat /proc/net/bonding/bond0 if it's working.

Ingo
  • 42,961
  • 20
  • 87
  • 207