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:
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.
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.
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:
Code:
#!/bin/sh set -e case "$2" in up) /sbin/ifup $1 if [ "$1" == eth0 ]; then /sbin/ifdown wlan0 ; fi # This is a new bit ;; down) /sbin/ifdown $1 if [ "$1" == eth0 ]; then /sbin/ifup wlan0 ; fi # Another new bit ;; esac
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.
Code:
auto lo iface lo inet loopback allow-hotplug eth0 iface eth0 inet static address 192.168.0.5 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.254 auto wlan0 iface wlan0 inet manual wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp iface aoakley-home inet static address 192.168.0.5 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.254
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.