Static network config with dhcp fallback

/etc/network/interfaces:

auto wan
iface wan inet manual
	up ifup eth0=eth0-static
	post-up /etc/network/checkConnectivity.sh eth0 192.168.0.1 10.10.0.2

iface eth0 inet manual

#static eth0 config
iface eth0-static inet static
	address 192.168.0.97
	netmask 255.255.255.0
	gateway 192.168.0.1
	dns-nameservers 192.168.0.1 10.10.0.2
	dns-search example.net

#dhcp eth0 config
iface eth0-dhcp inet dhcp

/etc/network/checkConnectivity.sh:

#!/bin/bash
nif=${1}
shift


sleep 1

for i in ${@}; do
	ping -c 2 ${i} \
		&& exit 0
done

# if nothing responds
ifdown ${nif}
ifup ${nif}=${nif}-dhcp

manually override:

  • ifup eth0=eth0-static
  • ifup eth0=eth0-dhcp