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,198
  • 3
  • 21
  • 37

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
  • 40,606
  • 15
  • 76
  • 189
  • From your answer what things I have to change in that file? Like in ethernet option what should be the address netmask gateway etc. There are my ethernet connection (Like in my hostel, library) so what should be the changes? – Ujjwal Gupta Feb 15 '18 at 11:21
  • First to use failover you have to set ethernet and wifi up and running. Then you can use `bonding` for this two interfaces. I suggest to use `systemd-networkd`. I'm not willing to investigate in old technology `networking with ifupdown`. Please follow this [Howto migrate from networking to systemd-networkd with dynamic failover](https://raspberrypi.stackexchange.com/q/78787/79866) step by step. If something is unclear, ask at that Question/Answer. – Ingo Feb 15 '18 at 17:16