7

I'm working on centos 7, and having problematic behaviour when setting network interface from dhcp to static ip configuration.

I edit /etc/resolv.conf, and run systemctl restart network.service
The changes that I made are gone, and a generic file is created:

cat /etc/resolv.conf
# Generated by NetworkManager


# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com

NOTICE: PEERDNS="yes" in ifcfg-ens160 file.
PEERDNS=, where is one of the following: yes — Modify /etc/resolv.conf if the DNS directive is set. If using DHCP, then yes is the default.
no — Do not modify /etc/resolv.conf.

Taken from here: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/3/html/Reference_Guide/s1-networkscripts-interfaces.html

I guess it has something to do with it, but it's working well when setting to dhcp, so I expect that if it configures /etc/resolv.conf automatically it will succeed.

A workaround is to edit /etc/resolv.conf after service is restarted.
But I want to understand the behavior, and how can I avoid the file being reset to this default failure message.

  • In your ifcfg-ens160 file, did you specify the DNS servers? DNS{1} = x.x.x.x, DNS{2}=x.x.x.x – Ray Oct 23 '14 at 19:44
  • No. Should I? By the way, same procedure works well on CentOS 6.2. Maybe something changed in the way services are handled - systemctl tool – csny Oct 26 '14 at 7:33
  • I don't think the DNS servers will populate if you do not define them. Per RedHat, DNS{1,2}=<address>, where <address> is a name server address to be placed in /etc/resolv.conf if the PEERDNS directive is set to yes. I am willing to bet that the ifcfg file on the CentOS6.2 build you mention has the DNS servers defined or PEERDNS="no". – Ray Oct 27 '14 at 19:42
  • You loose the bet :) The DNS ips are written only to /etc/resolv.conf, and PEERDNS=yes. How can it be that it worked before? – csny Oct 28 '14 at 8:33
  • Found this: unixmen.com/setting-dns-server-centos-7 It doesn't say that ifcfg file has to include DNS entries – csny Oct 29 '14 at 15:00
9

You're probably mixing the classic /etc/init.d/network (which gets translated to network.service) with NetworkManager.service. While those are expected to partially coexist, it's much better to choose just one of them and stop and disable the other.

Either way, it's better not to write /etc/resolv.conf directly but instead properly configure /etc/sysconfig/network and/or /etc/sysconfig/network-scripts/ifup-* files.

You should either enable dhcp or set the name servers manually in /etc/sysconfig.

Example (DHCP):

BOOTPROTO=dhcp

Example (static):

BOOTPROTO=none
DNS1=192.168.1.1

If you really want to set /etc/resolv.conf directly and you want to make sure NetworkManager won't overwrite it, you can set it up in /etc/NetworkManager/NetworkManager.conf.

[main]
dns=none

Regarding your additional question on the number of name servers, you should never need more than one or two name servers in /etc/resolv.conf. You shouldn't expect much from the libc resolver behavior, it just attempts the name servers in order and you'll experience long delays if you have defunct name servers in the list.

I don't know your reasons to use more than three name servers. But if there is one, you definitely need to configure a local forwarding DNS server like unbound or dnsmasq and point /etc/resolv.conf to 127.0.0.1. For the best experience with dynamic configuration you should use NetworkManager in this case.

NetworkManager with dnsmasq has been long supported and is the default on Ubuntu and possibly other distributions.

[main]
dns=dnsmasq

NetworkManager with unbound is in alpha quality in the lastest NetworkManager versions and currently also needs dnssec-trigger as the main use case is to provide DNSSEC validation on the local host.

[main]
dns=unbound

Both dnsmasq and unbound plugins configure /etc/resolv.conf to nameserver 127.0.0.1 for you and each of them configures the respective local DNS server.

  • Pavel, Thanks. Your insight helped me. I just set BOOTPROTO=none and added DNS1,DNS2,DNS3,DNS4 servers and restarted the network using service network restart. Now, the restart did not erase the entries in /etc/resolv.conf. What I found ,however, is it could add only up to 3 DNS servers in the /etc/resolve.conf file. Adding the fourth DNS logs a message in the file : ####NOTE: the libc resolver may not support more than 3 nameservers. ###The nameservers listed below may not be recognized. – Manoj Kumar Mar 1 '16 at 9:39
0

Use dhclient.conf to specify the name servers. It gives you fine grained control over the order of name server entries and is in line with the "Red Hat Way" of configuring the network.

dhclient.conf does not exist by default but you can create it:

interface "eth0" {
    prepend domain-name-servers 8.8.8.8;
    append domain-name-servers 8.8.4.4;
}

Resulting resolv.conf:

# Generated by NetworkManager
search example.com
nameserver 8.8.8.8
nameserver 10.0.2.3
nameserver 8.8.4.4

Tested with Vagrant:

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provision "shell", inline: <<VAGRANTEOF
  cat <<EOF > /etc/dhcp/dhclient-eth0.conf
interface "eth0" {
    prepend domain-name-servers 8.8.8.8;
    append domain-name-servers 8.8.4.4;
}
EOF
  sudo service network restart
VAGRANTEOF
end

Further reading:

  • dhclient.conf manpage
  • /usr/share/doc/dhclient-4.2.5/dhclient.conf.example
0

I would recommend using the NetworkManager GUI tool, to configure resolv.conf with permanent values - which will remain after network restart:

$ nmtui

Or:

$ nm-connection-editor

Add your DNS servers, Search Domains, etc. : enter image description here

Now verify those values are still defined after network restart:

$ sudo systemctl restart network
$ cat /etc/resolv.conf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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