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)
- Add following lines to
/etc/sysconfig/network-scripts/ifcfg-<inf>
:
DNS1=8.8.8.8
DNS2=8.8.4.4
DOMAIN=mydomain.com
- Reboot
"static" resolv.conf (the old way)
PEERDNS=no
in /etc/sysconfig/network-scripts/ifcfg-<inf>
- 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)
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)
Ran command hostnamectl set-hostname myNEWhostname.myNEWdomain
- Reboot. After restart, I re-ssh back into VM.
- Try
nslookup google.com
, ping google.com
, get errors like: ping: unknown host google.com
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
- 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
.
- 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
- I again run command
hostnamectl set-hostname myNEWhostname.myNEWdomain
- However, I still have no DNS,
nslookup
and ping
still fail.
- I try to restart NetworkManager as recommended
systemctl restart NetworkManager.service
- Still no DNS, so I run command
reboot
- After reboot, I login via ssh again and IT WORKS.
ping
and nslookp
are both successful.
- 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.
PEERDNS=no
in/etc/sysconfig/network-scripts/ifcfg-<inf>
. It's not necessarily dhcpclient, but may be NetworkManager. If you specifyDNS1
,DNS2
, andDOMAIN
in the same file, it should create the correct/etc/resolv/conf
for you.DNS1
,DNS2
, andDOMAIN
in this file, you need to manually add the correct lines to your/etc/resolv.conf
(i.e.search
andnameserver
).