I know there are numerous articles about this but I am posting the solution that worked for me and is indeed latest
My requirement:
As I use my pi primarily as a dns server for my home lan, I have it always connected via eth0 and would like to be able to use it even when I place it somewhere else within my network using wireless. With this in mind and also the need that I may want to someday carry the pi to someone elses network without a monitor or pre-supplied wifi config, I left the eth0 to dhcp and configured a static ip in my home router for it say 192.168.1.10 using its mac address for same ip via dhcp.
What I also wanted was that the wlan and eth0 should keep same ip of 192.168.1.10 so my dns is reachable weather its plugged in or not.
I configured wlan0 as static for two reasons, since I do not want to guess the wlan0 ip when its unplugged from eth0 and I can not assign it the same ip since I have already made the static reservation in home router with eth0 mac address.
I hope you are with me on this.
So here is how my interface file looks like:
pi@MyPi / $ sudo cat /etc/network/interfaces
auto lo
iface lo inet loopback
#
allow-hotplug eth0
iface eth0 inet dhcp
metric 0
auto wlan0
iface wlan0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
metric 1
Explanation:
We use the "ifplugd" to do what we need.
1) "ifplugd" does not monitor the interfaces which are set for "auto" startup. So we do not assign "auto eth0" for the eth0 interface
2) So above "ifplugd" is monitoring the eth0 interface, which means, ifplud will act everytime it sees a link down or link up on eth0. We use this monitoring to get out result. As per part of this monitoring ifplugd executes a script ( ifupdown ) . We will modify that script to bring the WLAN0 Down when Eth0 is available and UP when Eth0 link is lost.
Go to "/etc/ifplugd/action.d ".
Make copy of original "ifupdown" i.e sudo cp ifupdown ifupdown.original
We edit this file i.e sudo nano ifupdown
Paste the following :
#!/bin/sh
set -e
case "$2" in
up)
/sbin/ifup "$1"
if [ "$1" = eth0 ]; then /sbin/ifdown wlan0 ; fi # To bring the WLAN down
;;
down)
/sbin/ifdown "$1"
if [ "$1" = eth0 ]; then /sbin/ifup wlan0 ; fi # To bring the WLAN up
;;
esac
Ctr + x , Y, save it exit
sudo shutdown -r now
You should see that eth0 is working now , while wlan0 is not. Unplug the eth0 and that should kick start the wlan0. Plug the eth0 again and that should kill the wlan0.
If you want, you can assign wlan0 another ip address as per your liking. I hope it helps !!
Resources:
http://www.aoakley.com/articles/2013-07 ... orking.php
http://www.debian.org/doc/manuals/debia ... zas-in-eni