nameservers erased after systemctl restart network.service

52,454

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.

Share:
52,454

Related videos on Youtube

csny
Author by

csny

Updated on September 18, 2022

Comments

  • csny
    csny over 1 year

    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.

    • Ray
      Ray over 9 years
      In your ifcfg-ens160 file, did you specify the DNS servers? DNS{1} = x.x.x.x, DNS{2}=x.x.x.x
    • csny
      csny over 9 years
      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
    • Ray
      Ray over 9 years
      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".
    • csny
      csny over 9 years
      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
      csny over 9 years
      Found this: unixmen.com/setting-dns-server-centos-7 It doesn't say that ifcfg file has to include DNS entries
  • Manoj Kumar
    Manoj Kumar about 8 years
    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.