2

CentOS7. Network is all OK.

# cat /etc/resolv.conf 
nameserver 192.168.1.1

Then I decide to change the host name.

# hostnamectl set-hostname host.domain
# reboot
# cat /etc/resolv.conf 
# Generated by NetworkManager
search domain


# 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

Why does hostnamectl set-hostname mess up my /etc/resolv.conf?

4
  • i think it is not becuse of set-hostname , it is because of reboot and then dhclient runs to populate the resolv.conf , you should tell the system not to update resolv.conf , so that you can make it kind of static by setting peer dns= no
    – Ijaz Ahmad
    Commented Sep 29, 2016 at 15:18
  • 1
    That should be PEERDNS=no in /etc/sysconfig/network-scripts/ifcfg-<inf>. It's not necessarily dhcpclient, but may be NetworkManager. If you specify DNS1, DNS2, and DOMAIN in the same file, it should create the correct /etc/resolv/conf for you.
    – MikeA
    Commented Sep 29, 2016 at 15:23
  • I added PEERDNS=no in /etc/sysconfig/network-scripts/ifcfg-<inf>. Rebooted. Didn't work. Same result. Commented Sep 29, 2016 at 15:40
  • If you don't have DNS1, DNS2, and DOMAIN in this file, you need to manually add the correct lines to your /etc/resolv.conf (i.e. search and nameserver).
    – MikeA
    Commented Sep 29, 2016 at 16:54

2 Answers 2

2

Just to reiterate, as an official answer, what MikeA says in the question's comments:

It's not necessarily dhcpclient, but may be NetworkManager. If you specify DNS1, DNS2, and DOMAIN in the same file, it should create the correct /etc/resolv/conf for you.

If you don't have DNS1, DNS2, and DOMAIN in this file, you need to manually add the correct lines to your /etc/resolv.conf (i.e. search and nameserver) - MikeA

I followed his comments and was successful (see further down for more detail).

NetworkManager (CentOS 7 - current way)

  1. Add following lines to /etc/sysconfig/network-scripts/ifcfg-<inf>: DNS1=8.8.8.8 DNS2=8.8.4.4 DOMAIN=mydomain.com
  2. Reboot


"static" resolv.conf (the old way)

  1. PEERDNS=no in /etc/sysconfig/network-scripts/ifcfg-<inf>
  2. Manually add correct lines to /etc/resolv.conf (i.e. search, nameserver) (This is essentially how



More Information & Details


I came to this question as result of a network error.

Server is a CentOS 7 virtual machine running in KVM environment.

OS was installed via SolusVM Control Panel template.

After installing CentOS 7, I did one of two ways to change the hostname, but I can't remember which:

  • hostname myserver.mydomain.com; or
  • Used SolusVM Control Pannel's Hostname tab

I'm fairly certain, I only did the latter, as I have an init GIT commit that shows hostname and resolv.conf using the old hostname, but hosts has a single entry with static IP and new hostname/domain. See below for diffs.

It seems this change did not persist for one reason or another, when the VM was suspended via SolusVM (or KVM manager), resulting in reboot, the hostname was reverted back to the original by systemd.

I then discovered that in CentOS 7 the hostname should be set using hostnamectl set-hostname myserver.mydomain.com. [1]

After doing this and rebooting, I found I had no DNS resolution, e.g. nslookup google.com and ping google.com resulted in DNS / no resolution errors.

It seems like whatever DNS configuration that may have been set via the SolusVM CentOS 7 install template was wiped out by the NetworkManager, although I can't say for certain who / what caused the changes, but it's definitely clear from GIT that changes were made to /etc/hostname, /etc/resolv.conf and /etc/machine-info right after running hostnamectl ... command (again, see below for diffs).

Here are the exact steps I did for anyone one in a similar situation, for troubleshooting, or as an explicit example:

Objective: Update hostname.domainname (fqdn)

(explicitly: myOLDhostname.myOLDdomain.com -> myNEWhostname.myNEWdomain.com)

  1. Initial state:

    • hostname: myOLDhostname.myOLDdomain.com
    • resolv.conf:
      # Generated by NetworkManager search myOLDdomain.com nameserver 8.8.8.8 nameserver 8.8.4.4
    • machine-info: /dev/null (i.e. not existing)
  2. Ran command hostnamectl set-hostname myNEWhostname.myNEWdomain

  3. Reboot. After restart, I re-ssh back into VM.
  4. Try nslookup google.com, ping google.com, get errors like: ping: unknown host google.com
  5. cat /etc/resolv.conf now shows:
    # Generated by NetworkManager
    search myNEWdomain.com


    # 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
  6. After searching, I find /etc/sysconfig/network-scripts/ifcfg-eth0 has no DNS1, DNS1, and DOMAIN entries, this causes missing nameserver entries in /etc/resolv.conf.
  7. I append the following via sudoedit /etc/sysconfig/network-scripts/ifcfg-eth0:
    DNS1=8.8.8.8
    DNS2=8.8.4.4
    DOMAIN=myNEWdomain.com
  8. I again run command hostnamectl set-hostname myNEWhostname.myNEWdomain
  9. However, I still have no DNS, nslookup and ping still fail.
  10. I try to restart NetworkManager as recommended systemctl restart NetworkManager.service
  11. Still no DNS, so I run command reboot
  12. After reboot, I login via ssh again and IT WORKS. ping and nslookp are both successful.
  13. Now, I check cat /etc/resolv.conf and see included nameserver entries:
    # Generated by NetworkManager
    search myNEWdomain.com
    nameserver 8.8.4.4
    nameserver 8.8.8.8

For GIT, I use etckeeper (essentially manages a GIT repository in /etc) and it shows the initial configuration, and that explicitly, there had been no other changes to the network related files of /etc/{hostname,resolv.conf,hosts} until running the hostnamectl command.

1
  • Thanks for such a detailed answer. Can't believe that the configuration of my VPS (SolusVM, CentOS 7) is exactly the same as OP and it similarly ran into the DNS issue after I set a new hostname.
    – Ray Jasson
    Commented Jan 15, 2022 at 8:00
2

You can tell Networkmanager not to change resolv.conf by adding a nodns snippet in the main section in /etc/NetworkManager/conf.d/nodns.conf:

[main]
dns=none

(don't forget systemctl restart NetworkManager)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .