Posts: 1
Joined: Wed Apr 25, 2018 7:12 am

dhcpcd: Fallback to static IP configuration if DHCP fails

Wed Apr 25, 2018 7:17 am

The fallback works for me.
But does anybody knows if the RPI should get an IP address from the DHCP server when it comes back online? That part doesn't seem to work for me....

Posts: 2
Joined: Fri Sep 21, 2018 10:21 am

Re: dhcpcd: Fallback to static IP configuration if DHCP fails

Fri Sep 21, 2018 10:22 am

Did you find any solution? Im facing the same issue: I want my ip fallback cancelled when the DHCP server get's back online.

Posts: 2
Joined: Fri Sep 21, 2018 10:21 am

Re: dhcpcd: Fallback to static IP configuration if DHCP fails

Fri Sep 21, 2018 3:39 pm

I made this script to regain DHCP lease after a static profile has been chosen. Use a cronjob or systemd time to execute periodically. I prefer the systemd timer because i like to gather my logs with journalctl. It seems to be working nicely... I've checked reboots, unlinking and router reset. When the router resets, i get a static fallback ip, two minutes later, my Pi is back online by DHCP. You need nmap installed to detect the DHCP server (in a different subnet).


Code: Select all


function get_upstream() {
        # Look for a default gateway
        UPSTREAM=$(ip r | grep default | cut -d ' ' -f 3)

function reach_upstream() {
        # exit code 0 = reachable, 1 = unreachable
        return $(ping -q -w 1 -c 1 ${UPSTREAM} > /dev/null)

function reach_dhcp_server() {
	# Find nics by ifconfig
	nics=($(ifconfig | egrep -ow 'eth[0-9]{1,2}'))
	for nic in ${nics}; do
		# If a DHCP server is found, set it on $NIC and break
		reach_dhcp_server_on_interface ${nic} && { NIC=${nic}; return 0; }

function reach_dhcp_server_on_interface() {
	local interface=${1}
	# nmap command to find a DHCP server on an interface
	local nmap_command="sudo nmap --script broadcast-dhcp-discover -e ${interface}"

	# exit code 0 = router, 1 = no router
	return $(${nmap_command} 2>/dev/null | grep Router | egrep -wo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' > /dev/null)

function reset_ip() {
	# Tell dhcpcd to rebind $NIC
	dhcpcd -n ${NIC}

# Detect if there's an upstream and if it's reachable. If so, exit
if ! [ -z ${UPSTREAM} ]; then
	echo "Trying to ping ${UPSTREAM}"; reach_upstream && echo "Ping succeeded, keeping IP"; exit 0 || echo "Ping failed..."

# Check for a DHCP server. If not found, exit
echo "No upstream, checking for DHCP server"; reach_dhcp_server || { echo "DHCP server not found"; exit 0; }

# Lets go
echo "DHCP server found, resetting IP"; reset_ip

Here's the systemd stuff


Code: Select all

Description=Restarts dhcpcd if there's no internet but a DHCP server is found


Code: Select all

Description=Run dhcp-recheck every 2 minutes

#When missed a cycle, do not rechedule on reboot

To start running:

Code: Select all

systemctl enable dhcp-recheck.timer && systemctl start dhcp-recheck.timer
Last but not least i'll include my dhcpcd.conf bc with this configuration you can add additional NIC to the Pi and it doesn't matter which end you connect... One side works by DHCP and the other end is still available on switching cables ends up the same :)

Code: Select all

profile static_eth
static ip_address=


# Try DHCP first, else fall back to static
interface eth0
fallback static_eth

# Try DHCP first, else fall back to static
# This enables us to plug in an additional ethernet adapter;
# one of them will be accessible for internet, the other one for a direct connection
interface eth1
fallback static_eth
Use nmap to find your pi (look for port 22). This is in my routers subnet 192.168.1.x/24

Or just connect on the local domain if your network services allow.
ssh [email protected]

Return to “Arch”