12

Is it possible to get an IP address over DHCP but force a DNS server instead of using the ones from the DHCP?

I should be able to do so with a static IP addresses, but I would like to know how to do it getting an IP address over DHCP. I thought that changing /etc/resolv.conf would be permanent, but it dismisses the changes after a reboot.

3
  • 1
    Shouldn't that file contain a line »DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN« ?
    – FUZxxl
    Commented Mar 1, 2013 at 15:21
  • 1
    not in raspbian... Commented Mar 2, 2013 at 15:14
  • Not in stock debian either -- learned my lesson lol.
    – Allison
    Commented Oct 31, 2017 at 3:43

5 Answers 5

9

The only thing that worked for me was:

Edit /etc/dhcp/dhclient.conf and un-comment the line:

prepend domain-name-servers 208.67.222.222;

Anyway thanks for your for your help!

5

dns-nameservers (and all others dns-xxx directives in /etc/network/interfaces) does not work in raspbian out of the box, since resolvconf is not installed by default.

sudo apt-get install resolvconf solves the issue.

3

I fixed this following the steps from cuonglm's answer on Unix & Linux Stack Exchange.

Edit the file /etc/dhcp/dhclient.conf and add

supersede domain-name-servers 8.8.8.8, 8.8.4.4;

Then restart network or run dhclient to make changes.

From man dhclient.conf:

The supersede statement

   supersede [ option declaration ];

   If for some option the client should always  use  a  locally-configured
   value  or  values rather than whatever is supplied by the server, these
   values can be defined in the supersede statement.
1
  • Raspbian version?
    – Fabian
    Commented Apr 21, 2018 at 13:34
1

You can define them with a line in /etc/network/interfaces.

Underneath your interface, add the following line, replacing the x's with the actual IP of the DNS server:

dns-nameservers xxx.xxx.xxx.xxx

2
  • Thanks for your answer. It didn't work for me, it keeps getting the DNS server from the DHCP. The one I used on the /etc/network/interfaces file was in second place. Commented Mar 3, 2013 at 1:53
  • This is a wheezy only answer. Does not apply to Jessie
    – Ray Foss
    Commented Dec 22, 2019 at 16:24
0

It's an old threat, but for current Raspbian/Debian you have to add at the end of /etc/dhcpcd.conf:

static domain_name_servers=1.1.1.1 8.8.4.4
3
  • 1
    Why are you still using Jessie in 2020? Commented Mar 21, 2020 at 10:41
  • Sorry, my answer actually is for current Rapsbian (Buster) ! Please remove your downvote... Commented Mar 22, 2020 at 11:05
  • Sorry but I didn't downvote, and there is no way to tell who did. Commented Mar 24, 2020 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.