No announcement yet.

Turning off WiFi when a wired network cable is plugged in

  • Filter
  • Time
  • Show
Clear All
new posts

  • Turning off WiFi when a wired network cable is plugged in

    A copy and paste from here:

    Turning off WiFi when a wired network cable is plugged in

    I want the same static IP at home regardless of whether I'm using WiFi or a wired network cable. However, if both the <tt class="CODE">eth0</tt> and <tt class="CODE">wlan0</tt> interfaces try to use the same IP address at the same time, this can cause a lot of problems; usually an unresponsive network.

    There are a gazillion ways around this problem. Look up "Linux route metrics" if you want to learn the hard-core way of doing it. My solution is a lot more simple: turn off WiFi if a wired network cable is inserted.

    The <tt class="CODE">ifplugd</tt> daemon provides a neat foundation that we can build on. If your <tt class="CODE">eth0</tt> interface is declared as hot-pluggable, <tt class="CODE">ifplugd</tt> runs a special script whenever a cable is inserted or removed. We can customise this script to turn the <tt class="CODE">wlan0</tt> interface off or on.

    Recent distributions of Raspbian have ifplugd installed by default. If you don't have it (for example, if <tt class="CODE">type ifplugd</tt> tells you "not found"), you can use <tt class="CODE">apt-get install ifplugd</tt> to install it. If the installer prompts you to configure it, accept the default configuration values of <tt class="CODE">auto</tt> for static interfaces and <tt class="CODE">all</tt> for hotplugged interfaces. If you mess this up, you can use <tt class="CODE">dpkg-reconfigure ifplugd</tt> to correct the configuration at any time.

    Let's start by customizing the <tt class="CODE">/etc/ifplugd/action.d/ifupdown</tt> script:

    set -e 
    case "$2" in 
      /sbin/ifup $1 
      if [ "$1" == eth0 ]; then /sbin/ifdown wlan0 ; fi # This is a new bit 
      /sbin/ifdown $1 
      if [ "$1" == eth0 ]; then /sbin/ifup wlan0 ; fi # Another new bit 

    Next, we need to make sure that <tt class="CODE">eth0</tt> is defined as <tt class="CODE">allow-hotplug</tt> in <tt class="CODE">/etc/network/interfaces</tt> file, and set up the static IP address for <tt class="CODE">eth0</tt> to be the same as the <tt class="CODE">wlan0</tt> interface.

    auto lo 
    iface lo inet loopback 
    allow-hotplug eth0 
    iface eth0 inet static 
    auto wlan0 
    iface wlan0 inet manual 
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf 
    iface default inet dhcp 
    iface aoakley-home inet static 

    I'll repeat my previous bit of advice here: don't set the <tt class="CODE">wlan0</tt> interface as hot-pluggable, as in any case, most attempts to remove or re-insert a USB WiFi adaptor whilst your Raspberry Pi is switched on, will probably result in your Pi rebooting (or worse, not rebooting... ever... and a puff of magic smoke). My guess is that this is because a USB WiFi adaptor consumes a lot more milliamps than a mouse or a keyboard, and the Pi is already pretty sensitive to amperage fluctuations. It's a twenty-five quid computer; if you need to remove or insert the WiFi adaptor, just shut down the Pi first. The <tt class="CODE">auto</tt> parameter for <tt class="CODE">wlan0</tt> defines the WiFi interface as something that should be brought up at boot time and not disconnected until shutdown. However if the dongle isn't there, the Pi will continue to boot without it.

    Reboot with <tt class="CODE">shutdown -r now</tt> , and you should be done.
    If it all goes wrong, restore your backup, or connect a screen to see what's happening, or put the SD card in a desktop/laptop Linux PC and examine the <tt class="CODE">/etc</tt> files there.

    By the way, this setup has also been tested on a Raspberry Pi Model A which doesn't have an <tt class="CODE">eth0</tt> interface at all. It'll boot up and connect to WiFi just fine, so if you have lots of Raspberry Pis, you can use this recipe for all of them.

    Testing this out with my Squeezebox players - O2 Jogglers / Openpeak 2 tablets which are running Ubuntu 14.04 on their eMMC memory.
    Last edited by Pete; September 23rd, 2017, 06:30 PM.
    - Pete

    Auto mator
    Homeseer 3 Pro - (Linux) - Ubuntu 18.04/W7e 64 bit Intel Haswell CPU 16Gb- Mono 6.8X
    Homeseer Zee2 (Lite) - (Linux) - Ubuntu 18.04/W7e - CherryTrail x5-Z8350 BeeLink 4Gb BT3 Pro - Mono 6.8X
    HS4 Pro - V4.0.5.0 - Ubuntu 18.04/W7e 64 bit Intel Kaby Lake CPU - 32Gb - Mono 6.8X
    HS4 Lite -

    X10, UPB, Zigbee, ZWave and Wifi MQTT automation. OmniPro 2, Russound zoned audio, Smartthings hub, Hubitat Hub, and Home Assistant