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.

Bridgey
  • 223
  • 1
  • 3
  • 7

6 Answers6

5

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.

starbeamrainbowlabs
  • 344
  • 2
  • 7
  • 15
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.

Seamus
  • 23,558
  • 5
  • 42
  • 83
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

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.

user30747
  • 101
  • 2
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

xgqfrms
  • 146
  • 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>;
Mark Smith
  • 1,293
  • 8
  • 9