3

I have already read the very useful question about dhcpcd: How do I set up networking/WiFi/static IP address?

My situation seems to be slightly unusual:

  • DHCP server on LAN is set to always give the Pi a specific IP address (based on its MAC). Let's say 192.168.0.100.
  • The Pi is running bind, listening on 192.168.0.100:53.

I want to configure the Pi to still get its IP address via DHCP, but to force the primary DNS server to be 192.168.0.100 (itself).

I understand I should update /etc/dhcpcd.conf with: nohook resolv.conf

Is the "correct" way, on the Pi, to set the DNS server to:

  1. Add the DNS configuration to /etc/resolv.conf.tail, OR
  2. Add static domain_name_servers=... to /etc/dhcpcd.conf?

Are there any particular advantages or disadvantages to either method?

Also, I do not want to configure the DHCP server to give out 192.168.0.100 as the DNS server.

1
  • Hi @Milliways, thanks for the comment. I'm not quite sure why you think it doesn't makes sense? As I say in the post, the Pi is running the Bind DNS server so that's why I want to tell the Pi to use itself as the DNS server and also as I say in the post, I don't want to give out the Pi's IP address via DHCP (because I don't want other devices on the network to use the Pi as the DNS server).
    – Bridgey
    Nov 10, 2016 at 11:51

6 Answers 6

0

I think the answer you're looking for is here.

Blatantly plariarised:

You can add the following line to /etc/dhcp/dhclient.conf:

prepend domain-name-servers <working DNS IP(s) here>;

This adds the DNS IP address(es) you specify before that/those provided by the DHCP. If you would like to add it/them after the address(es) provided by the DHCP, just use

append domain-name-servers <working DNS IP(s) here>;

If, instead you would like to ignore the DNS address(es) provided by the DHCP altogether, use

supersede domain-name-servers <working DNS IP(s) here>;
5
  • Thank you very much for this - very helpful! I think this is for dhclient rather than dhcpcd, but I guess the principle is the same: specify it in he dhcp client configuration and it should get passed through to '/etc/resolve.conf'. I'll give it a while to see if anybody posts a dhcpcd-specific answer, but if not I'll accept yours. Thanks!
    – Bridgey
    Nov 10, 2016 at 13:22
  • dhcpd is the DHCP server, isn't it? The thing which hands out IP addresses. That's not what you want to be running on your Pi if you have a router already doing it (which is what I interpreted from your post). dhclient is the DHCP client which goes to the DHCP server and gets an IP address (and other things). If you really are wanting the Pi to be the DHCP server, then that's a special case I don't know about -- how the DHCP server's IP address is allocated.
    – Mark Smith
    Nov 10, 2016 at 13:31
  • dhcpcd - note the c :) This is a DHCP client not server :) This is currently the recommended DHCP client for the Pi (I believe!)
    – Bridgey
    Nov 10, 2016 at 13:46
  • Ah! My mistake! :-)
    – Mark Smith
    Nov 10, 2016 at 13:48
  • This is the selected answer, but it is not the correct answer. It will probably work if you're using dhclient, but if you're using deprecated software you probably know this.
    – Seamus
    Aug 25, 2020 at 5:23
4

The correct way to set DNS servers is in /etc/dhcpcd.conf. This will override the value supplied by the DHCP server, without interfering with other DHCP functions. Look for a line that looks like this (create it if it doesn't exist):

static domain_name_servers=1.0.0.1 8.8.4.4 8.8.8.8

Separate multiple IP addresses with a single space ( ).

You can verify your DNS settings by executing nslookup <somewhere.com> from the command line. You should see the DNS servers you've specified in /etc/dhcpcd.conf listed in the output of nslookup.

I was updating the DNS servers for a Pi of mine, and found that I'd already specified the DNS servers here.

2
  • 1
    This is the correct answer if you're using dhcpcd instead of systemd-networkd. You should not be writing to /etc/dhcp/dhclient.conf as that is what dhcpcd was "hired to do" - the selected answer is incorrect unless you're still using the now deprecated dhclient.
    – Seamus
    Aug 25, 2020 at 5:18
  • tried this but it just augments the static domain name servers to the dhcp domain name servers
    – Lamp
    Oct 2, 2022 at 18:50
1

A Correct Way to Specify a DNS Server when Using DHCP:

This answer provides one correct way to override the DNS server(s) obtained by dhcpcd through the DHCP process. Here's another way (REF: dhcpcd documentation):

Create a file named /etc/resolv.conf.head. List your preferred DNS servers here using the same format as in /etc/resolv.conf. dhcpcd will prepend this file to /etc/resolv.conf.

dhcpcd was "hired" to write /etc/resolv.conf through resolvconf. If you write to it directly, your manually-generated DNS entries may be overwritten, possibly creating chaos in your system.

A Correct Way to Specify a "Fixed" IP Address:

Don't be tempted to use the static_ipaddress option in /etc/dhcpcd.conf; man dhcpcd.conf is specific about this:

For IPv4, you should use the inform ipaddress option instead of setting a static address.

If you use the inform ipaddress option in /etc/dhcpcd.conf, you will effectively have a static IP address, and you will see it in your router's DHCP table of leases.

0

works on raspberryPi 4, dont use it in another linux disro even if it is the same copy.. it will break something for sure. in RB4, you can use this method after you change the network config, type : raspi-config then go to Advanced options then Network config then dhcpcd

there is no standard in the Gnu/Linux Operating system, and thats why it will never be a stable operating system like Windows.

many times DNS dose not works in the mighty Linux operating system, the devs need some carrot to wake up and focus on reliability and stability operating system and try to copy what windows 10..

anyways, if you have raspberryPi 4 at this date, you can use this method, if you here after some years, DO NOT try it, Devs are crazy and for sure, they will change something that is extremally deep in the system to feed thier orgazims .

sudo nano /etc/dhcpcd.conf

then

static domain_name_servers=1.0.0.1 8.8.4.4 8.8.8.8

AS ALWAYS, DEVS WILL CHANGE SOMETHING TO RUNE YOUR LIFE, DO NOT PUT ( , ) USE A SPACE BETWEEN THE DNS

2
  • 1
    Windows is definitely NOT a stable operating system.
    – Chenmunka
    Jan 3, 2023 at 18:47
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 3, 2023 at 18:48
0

According to man dhcpcd, there is a --static or -S option that you can use on use via the command line to set static values such as the domain name server address:

dhcpcd --static domain_name_servers=192.168.0.1

I'm not sure exactly how it's supposed to work though; as the /etc/dhcpcd.conf file is left untouched as well as /etc/resolv.conf.

0

How to config the Raspberry Pi's static IP address and DNS servers

Solution 1: modify the /etc/dhcpcd.conf file (recommend 👍)

It will automatically write the static domain_name_servers configuration to the /etc/resolv.conf file.

$ sudo vim /etc/dhcpcd.conf
# Add the following content
interface wlan0
    # static IP ✅
    static ip_address=192.168.0.100/24
    static routers=192.168.0.1
    # static DNS servers ✅
    static domain_name_servers=8.8.8.8 8.8.4.4
    # OR, static DNS server
    # static domain_name_servers=8.8.8.8

$ cat /etc/dhcpcd.conf

Solution 2: modify the /etc/resolv.conf file (not recommend 👎)

This configuration is valid only once, you have to manually change it every time after login.

$ sudo vim /etc/resolv.conf

# Add the following content
nameserver 8.8.8.8
nameserver 8.8.4.4

$ cat /etc/resolv.conf | grep "nameserver"
nameserver 8.8.8.8
nameserver 8.8.4.4

demos

enter image description here

enter image description here

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.