Preferred way to add a new name server to a host in Red Hat or CentOS

6,440

Solution 1

I would

  • add it to the DHCP server's domain name-server option list
  • add it to /etc/resolv.conf

The client will start using it immediately and the next time it refreshes it's DHCP lease/options it will get it from the server too.

You could also add the new server to the DHCP server's domain name server list and restart networking on the client so that it immediately updates from the server

service network restart

If you don't want to change the DHCP server configuration then you can use the /etc/dhcp/dhclient.conf file to append or prepend an address to the domain-name-servers list

append domain-name-servers 8.8.4.4;

would add the name server at 8.8.4.4 to the end of the list of nameservers in /etc/resolv.conf. Using prepend would put it at the beginning.

Be aware that the maximum number of entries in the nameserver list in /etc/resolv.conf is 3.

Solution 2

The way I have always done it was to modify /etc/network/interfaces.

The entry for all my hosts looks like this:

dns-nameservers 8.8.8.8 8.8.4.4

This sets 8.8.8.8 and 8.8.4.4 as your DNS servers, naturally you could replace those with whichever DNS you decide to use. After modifying that file make sure to issue /etc/init.d/networking restart so the changes take affect.

Solution 3

Changing this at /etc/resolv.conf by adding the appropriate entries is the most consistent approach across Linux distributions/versions. Plus, it is immediately active and persists across reboots. Consistency is key here.

I would not rely on the ifcfg-ethX files or something like dhclient's configuration file. The system probably shouldn't be using DHCP (unless it's a workstation), and that's an assumption that doesn't scale...

Share:
6,440

Related videos on Youtube

ChaimKut
Author by

ChaimKut

Updated on September 18, 2022

Comments

  • ChaimKut
    ChaimKut over 1 year

    I am managing a particular host machine, and I would like to locally add a new name server for it to query (i.e. not changing its name server settings via the dhcp server). It seems that there are multiple ways to add a new name server.

    • Adding a 'nameserver ' directive directly to the resolv.conf file
    • Adding a 'DNS{1,2}=address' directive to a ifcfg-ethX file
    • Adding a 'append domain-name-servers' directive to a dhclient.conf file. (Let's assume the host is using dhcp).

    Perhaps there are even more ways...

    Which way is preferred? If I want the name server address to be persisted following restarts, and also be available for use immediately in the next lookup by the host, which option is best?

    Thanks

  • ChaimKut
    ChaimKut almost 11 years
    Sorry, I was slightly unclear. I'm on the host side, not the DHCP server side. I want to add a name server that my host will use, and leave the DHCP server as-is.
  • user9517
    user9517 almost 11 years
    CentOS doesn't have that file.
  • user9517
    user9517 almost 11 years
    This would be overwritten by dhclient as per the OPs third bullet point.
  • ewwhite
    ewwhite almost 11 years
    @ChaimKut Why? Is static configuration an option?
  • ChaimKut
    ChaimKut almost 11 years
    @ewwhite I'm working on an application intended to run on a regular host. The application has no ability to alter the DHCP server sitting elsewhere in the network.