i came accross a problem i was yet unable to solve with my raspberry pi.

what i want is the following:

  • startup wifi on boot with no ethernet connected
  • when plugging in ethernet cable switch to ethernet and stop wifi
  • when removing ethernet cable restart wifi

before posting my config files i will explain what is happening right now:

  • wifi starts up on boot, i can login via ssh and everything works fine
  • when i plugin ethernet, the wifi goes down and the pi is then accessible through the ethernet ip.

    But here is where the problems start:

    i cannot access the internet through this, as there is no default gateway set!

    sure i can just type route add default gw xxx.xxx.xxx.xxx but i want this to work

    automatically.

  • when i remove the ethernet cable the wifi seems to startup again (light flickering on the dongle) but it is not accessible anymore

    neither via ssh nor through port 80 or antyhing else

i already invested many hours on google to find information on the subject, and honestly there is alot of information out there. sadly many answers are very different, yet they seem to work for someone or the other. my files currently look like a mix of the many different things i found on the web. i hope you can help me to get this working, or explain to me that what i want is not possible and why.

/etc/default/ifplug

INTERFACES=""

HOTPLUG_INTERFACES="eth0"

ARGS="-q -f -u0 -d10 -w -I"

SUSPEND_ACTION="stop"

/etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

update_config=1

network={

ssid="MYSSID"

psk="MYPSK"

id_str="wifi"

}

/etc/network/interfaces

auto lo

iface lo inet loopback

iface eth0 inet static

   address 192.168.0.99

   netmask 255.255.255.0

   gateway 192.168.0.254

   post-up route add default gw 192.168.0.254

   pre-down route del default gw 192.168.0.254

allow-hotplug wlan0

iface wlan0 inet manual

wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

wireless-power off

iface wifi inet static

   address 192.168.0.89

   netmask 255.255.255.0

   #gateway 192.168.0.254

   post-up route add default gw 192.168.0.254

   pre-down route del default gw 192.168.0.254

iface default inet dhcp

post-up route add default gw 192.168.0.254

pre-down route del default gw 192.168.0.254

as you can see i disabled the power management on the wifi and tried to add the gateway after either interface comes up.

as far as i can tell these pre and post lines don't have any effect at all, since, as described earlier, there is no gateway set after i plug in the ethernet cable.

i also tried setting the gateway on both eth0 AND wlan0 which resulted in the wlan not being accessible at all. so i have it commented out on one of them.

i hope you guys can help me with an answer, as it's driving me nuts.

thank you very much for your time and effort.

  • Do you want to use DHCP iface default inet dhcp or not - make up your mind? /etc/network/interfaces seems unnecessarily complex. If possible reserve addresses in your router rather than fiddle with static – Milliways Sep 23 '14 at 7:25
  • @Milliways what exactly does the iface default line do? in many of the "guides" i found during my research it explains this line just with "this line has to be in there"..? – buzzdx Sep 23 '14 at 8:18
  • /etc/network/interfaces can set id_str="name" to define the interface name, but if not set it is "default". This means that this then calls dhcp on the interface. PS I agree the documentation is obscure, aand I am not sure I understand all the implicationss – Milliways Sep 23 '14 at 11:16
  • @Milliways i'm trying another configuration right now which seems to work at least partially the way i want. i will test it and post if testing goes well. i'm new to stackexchange posting, should i post it as answer even if it does work only partially? – buzzdx Sep 23 '14 at 22:20
up vote 0 down vote accepted

so after some more testing i have a more or less working solution now.

the pi now connects via wifi on boot with a static ip address and all works fine. when i plug in the ethernet cable the pi is also accessible via the static ip assigned to eth0.

what is not working as i wanted, is that the wifi doesn't go down when i plug in the ethernet cable. also, if i remove the ethernet cable again, wifi won't work anymore.

but since i wanted to use wifi as main connection with the ability to just plug in the ethernet as a backup method i feel like the solution i found is good enough to post as answer.

/etc/network/interfaces

auto lo
iface lo inet loopback

#allow-hotplug eth0
iface eth0 inet static
        address 192.168.0.99
        netmask 255.255.255.0
        gateway 192.168.0.254

allow-hotplug wlan0
iface wlan0 inet static
        address 192.168.0.89
        netmask 255.255.255.0
        gateway 192.168.0.254
        wpa-ssid "buzznet"
        wpa-psk "schiffjeglotzje"
wireless-power off

#allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#wireless-power off
#iface wifi inet dhcp

as you can see no additional wpa configuration file was used and all works as decribed above.

to keep the wifi stable i added a router ping every minute via crontab and execute this script https://raw.githubusercontent.com/dweeber/WiFi_Check/master/WiFi_Check every 2 minutes in case wifi went down (because as far as i know without wpa-roam it won't automatically reconnect).

the system is running for half and a day now without any problems.

Try assigning lower metric to the eth0 connection, edit the /etc/dhcpcd.conf file and add at the end of the file:

interface eth0
metric 200

interface wlan0
metric 300

Assigning lower metric give precedence over the other interfaces in terms of where the internet connection comes from (at least that is what I understood). This will result in the eth0 making all the internet connection when present and when it doesn't the other connection you want will work with the internet.

I had the same problem when I needed the wlan0 to work through another network from the eth0 connection.

That way you don't have to edit multiple line and fiddle through so many settings on the interfaces. At least for me it works with DHCP, but you can reserve the ip on the router so it shouldn't be any problem.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.