# Override DNS Servers for DHCP Connection

## wswartzendruber

I have eth0 setup in /etc/conf.d/net to take DHCP.  I want to manually specify DNS, though, and dhcpd seems to overwrite /etc/resolv.conf.  Interestingly, dns_servers_eth0 seems to have no effect.

----------

## palmer

I have a few ideas, one of which is bound to work at least a little

You can try adding the DNS entries you want to /etc/resolv.conf.head (or /etc/resolv.conf.tail), which should get prepended (or appended) to /etc/resolv.conf every time it is updated.

If you're running dhcpcd, you can pass "-C resolv.conf" to stop it from overwriting /etc/resolv.conf (you can probably pass this option in a config file somewhere, maybe /etc/conf.d/net?) and then manage /etc/resolv.conf yourself.

Try modifying the scripts in /lib64/dhcpcd/dhcpcd-hooks/ to fit whatever you want them to do.

Have your DHCP server pass the DNS server IP to use (I use dnsmasq for this).

I would recommend the dnsmasq method, as it works for all machines on your network.  This also has the benefit of acting as a caching DNS server which can make browsing feel a lot snappier.

----------

## MarkYork

If you are looking to keep /etc/resolv.conf file structure, I would recommend that you set the immutable flag to the file by doing:

# chattr +i /etc/resolv.conf

That would prevent your /etc/resolv.conf file from being modified by you or any other daemons. If you need to modify resolv.conf file content, you will need to unset the immutable flag by:

# chattr -i /etc/resolv.conf

----------

## jathlon

I do part of it in /etc/dhcpcd.conf.

Part of the original unedited /etc/dhcpcd.conf

```

# A list of options to request from the DHCP server.

option domain_name_servers, domain_name, domain_search, host_name

option classless_static_routes
```

Remove the "domain_name_servers" from the option list.

Then just to be on the safe side I use the /etc/resolv.conf.tail file to make sure that the dns server/servers I want are added to the file every time /etc/init.d/dhcpcd is run.

I classify this as one of those "It Works For Me" * things.  YMMV :)

joe

*IWFM in my carefully crafted, fragile little enviroment.

----------

## ycUygB1

For me,  neither dhcpcd.conf modifications nor resolv.conf.tail had any effect.

The solution for me was setting the following in /etc/conf.d/net

```
dns_domain= cheap-domains-r-us.com

dns_servers="8.8.8.8 8.8.4.4"

dhcp="nodns"

```

----------

## hika

You can put a file resolve.conf.head or resolve.conf.tail in /etc those will be prepended and/or appended to resolve.conf.

I use resolve.conf.head to put the dns server on my laptop ahead of local ones.

resolve.conf.head

```
domain pc-asus.dkw

nameserver 127.0.0.1
```

It works with me

----------

## ddriver

 *hnaparst wrote:*   

> For me,  neither dhcpcd.conf modifications nor resolv.conf.tail had any effect.
> 
> The solution for me was setting the following in /etc/conf.d/net
> 
> ```
> ...

 

I came here tonight looking for an answer to the same question, and this was the perfect answer. Thank you.

----------

## Gentree

I got around my resolv.conf getting dumped on by adding the following to /etc/conf.d/net

```
dhcpcd_eth0=" -C resolv.conf"
```

This probably needs to be repeated for any interface you don't want to dump on resolv.conf so some of the above may be more reliable in the case of a new interface happening by.  Like an extra wifi for example. 

 :Cool: 

----------

## UberLord

Or, dare I say it, add it to /etc/dhcpcd.conf in the top section and BOOM - applies to all interfaces.

----------

## Gentree

man dhcpd:

 *Quote:*   

>             So to stop dhcpcd from touching your DNS or MTU settings you
> 
>              would do:-
> 
>                    dhcpcd -C resolv.conf -C mtu eth0
> ...

 

man dhcpcd.conf

 *Quote:*   

>   Most of the options found in dhcpcd( 8 ) can be used here.

 

So it looks like Uber is suggesting this single line at the top of /etc/dhcpcd.conf

```
# A sample configuration for dhcpcd.

# See dhcpcd.conf(5) for details.

-C resolv.conf

```

----------

## Gentree

Dare I ask which "it" you are referring to? 

 *Gentree wrote:*   

> man dhcpd:
> 
>  *Quote:*               So to stop dhcpcd from touching your DNS or MTU settings you
> 
>              would do:-
> ...

 

Well, that gets spat out , so I guess not. 

Perhaps Uber can be a bit more informative that "it".    :Cool: 

----------

## desultory

s/-C/C/

Or, more legibly:

s/-C/nohook/

----------

## Gentree

I now have 

```
# dhcpd options (without leading "-")

C resolv.conf

```

at the top of  /etc/dhcpcd.conf

but I see something like "dhcpcd unknown option -- C  flash past during boot. 

No trace of this anywhere in dmesg or /var/log/messages 

caught it with scroll lock: 

```
dhcpcd unknown option -- C
```

Note space before C.

OH, BTW none of that works either, resolv.conf is still getting dumped on by dhcp   :Mad: 

----------

## Gentree

Ok the following does work:

```

# dhcpd options (without leading "-")

nohook resolv.conf

```

It seems these options get passed with "--" pre-pended so the short forms don't work. 

desultory 	

PostPosted: Mon Oct 28, 2013 3:54 am    Post subject:

s/-C/C/   # wrong

Or, more legibly:

s/-C/nohook/   # correct

UberLord 	

PostPosted: Sun Oct 27, 2013 12:45 pm    Post subject:

Or, dare I say it, add it to /etc/dhcpcd.conf in the top section and BOOM - applies to all interfaces.

# dare I say it, terse but wrong.

----------

## orionbelt

I have a similar, though not identical, problem. My /etc/conf.d/net is empty, so DHCP (udhcpc) is used by default. However, i now need to add some options to /etc/resolv.conf, which however is overwritten. As per net.example documentation, i put this single line in my /etc/conf.d/net:

```
dns_options="rotate single-request-reopen"
```

 Alas, its has no effect.

Using /etc/resolv.conf.tail file did not help either, but i would prefer the /etc/conf.d/net option anyway.

Could someone explain why it doesn't work and, ideally, how to make it work?

----------

## Logicien

For Dhcpcd not touch /etc/resolv.conf, domain_name_servers must be remove from any option containing it and the line

```
nohook resolv.conf
```

must be in /etc/dhcpcd.conf. To configure the dhcp request per interface the line master must be at the top of the file. Than you can configure Dhcpcd per interface. For example

```
master

interface eth0

dhcpcd

nohook wpa_supplicant

option domain_name_servers

interface wlan0

dhcpcd

env wpa_supplicant_driver=nl80211

nohook resolv.conf
```

----------

## orionbelt

Thanks for the comment. As i said, i did not touch /etc/resolv.conf, and i am not using dhcpcd but udhcpc.

The only file i touched was /etc/conf.d/net. As far as i can tell, according to the documentation it was supposed to work but it didn't...

----------

