# How to setup a fallback DNS server if DHCP failed

## hans_da

The idea is really simple. I would like to have HDCP to detect possible IP address. If not, than use a fallback static IP address. My conf.d/net is as follow:

config_eth0=( "dhcp" )

dhcpcd_eth0="-t 5 -L"

fallback_eth0=( "134.60.14.66 netmask 255.255.255.0 brd 134.60.14.255" )

fallback_route_eth0=( "default via 134.60.14.1" )

However, everytime when I start eth0 the DHCP overwrite the DNS(resolv.conf), no matter whether the DHCP is successful or not. How can I configure a default dns here for eth0, when the DHCP failed?

Thanks!

----------

## mno

What version of dhcpcd are you running? There was a bug in 4.0.1 and you should go to 4.0.2...

----------

## h017ah

Did you find out about this, hand_da? I had to hack around in /lib/dhcpcd/dhcpcd-hooks/20-resolv.conf to make dhcpcd not discard the nameservers set by /etc/init.d/net.eth0, and the solution I came up with is kinda hackish...

I also have a problem with getting fallback_route_eth0="default via <gw>" to work. No matter how I format it, it gets ignored.

Also the notation fallback_eth0="<ip> netmask <netmask> brd <broadcast>" doesn't work, I had to write it in CIDR-notation, e.g. fallback_eth0="192.168.0.1/32"

I'm running openrc-0.4.3-r2 and dhcpcd-4.0.12

----------

## UberLord

dhcpcd-4.99.18 allows the use of fallback profiles, so you can configure this in /etc/dhcpcd.conf

```
interface eth0

fallback static

profile static

static ip_address=192.168.0.10/24

static domain_name_servers=192.168.0.1

static domain_name=foo.com
```

----------

## h017ah

... for this issue to get resolved this quickly and elegantly, who would have thought? :)

Of course this worked as a charm, UberLord, thanks for letting us know :) Just remember to enable zeroconf, either by setting the use-flag zeroconf when building dhcpcd, or by commenting out "noipv4ll" from dhcpcd.conf.

[Solved]

----------

## maxxim

Hi there,

I was also trying to find a solution for this for quite a while. My goal was to have an all-in-one-solution using only /etc/conf.d/net.

Here's what I came up with:

```
# /etc/conf.d/net

#

# Use DHCP for eth0. DHCP configuration: be quiet, wait 15 seconds

# for a response, disable IP address checking via ARP (faster boot).

#

# In case DHCP fails, the specified fallback configuration is used.

# Note that 'fallback_dns_eth0' is handled by postup() since there

# is no native support for this.

#

# Configure DHCP for eth0

config_eth0="dhcp"

dhcpcd_eth0="--quiet --timeout 15 --noarp"

# Configure fallback for eth0

fallback_eth0="10.10.10.10 netmask 255.255.255.0"

fallback_routes_eth0="default via 10.10.10.254"

fallback_dns_eth0=("10.10.10.254")

# Define postup function for DNS fallback

# (refer to '/usr/share/doc/netifrc-0.2.2/net.example' for details)

function postup {

   # Determine if there is a fallback configuration for the given interface

   fb_var="fallback_dns_${IFACE}"

   fb_arr="fallback_dns_${IFACE}[@]"

   if [ -n "${!fb_var}" ]; then

      

      # Check /etc/resolv.conf for nameserver entries

      ebegin Verifying DNS configuration

      if ! grep -q nameserver /etc/resolv.conf; then

         eend 1

         

         # Add fallback name server(s) to /etc/resolv.conf

         for nameserver in "${!fb_arr}"; do

            ewarn Adding fallback name server $nameserver ...

            echo "nameserver $nameserver" >> /etc/resolv.conf

            eend $?

         done

      else

         eend 0

         einfo DNS is already configured, no fallback required

      fi

   

   else

      einfo "Nothing to do for '${IFACE}'"

      eend 0

   fi

   # Always return 'success'

   return 0

}

```

This is the output for '/etc/init.d/net.eth0 start':

```
 * Bringing up interface eth0

 *   dhcp ...

 *     Running dhcpcd ... [ !! ]

 *   Trying fallback configuration 10.10.10.10 netmask 255.255.255.0

 *   10.10.10.10 ... [ ok ]

 *   Adding routes

 *     default via 10.10.10.254 ... [ ok ]

 *   Running postup ...

 *     Verifying DNS configuration ... [ !! ]

 *     Adding fallback name server 10.10.10.254 ... [ ok ]

```

This is the output for '/etc/init.d/net.lo start':

```
 * Bringing up interface lo

 *   127.0.0.1/8 ... [ ok ]

 *   Adding routes

 *     127.0.0.0/8 via 127.0.0.1 ... [ ok ]

 *   Running postup ...

 *     Nothing to do for 'lo' [ ok ]

```

----------

