CentOS7: Network Manager is using wrong search domain
Solution 1
After a few hours of poking around, I was able to resolve this. It turns out, this was being set via DHCP:
nmcli -f ip4 device show eth0
IP4.ADDRESS[1]: 172.31.53.162/20
IP4.GATEWAY: 172.31.48.1
IP4.DNS[1]: 172.31.0.2
IP4.DOMAIN[1]: ec2.internal
I was able to override IP4.DOMAIN[1] by overriding a network interface's ipv4.dns-search value:
nmcli connection modify uuid \`nmcli connection show --active | grep 802-3-ethernet | awk '{print $(NF-2)}' | tail -n 1` ipv4.dns-search d.sample.com
Or more simply,
nmcli connection modify System\ eth0 ipv4.dns-search "d.sample.com"
Then you have to restart NetworkManager
systemctl restart NetworkManager.service
I also found that because I was working with an Amazon instance, I needed to update my cloud.cfg file.
Solution 2
The /etc/resolv.conf file will always be overwritten when there is a change or update to the network. You can control what is written by editing files in the /etc/resolvconf/resolv.conf.d/
folder... namely the head
file.
Place this in the /etc/resolvconf/resolv.conf.d/head
file:
nameserver 172.31.0.2
search testing01.d.sample.com
Now this will be the header of the /etc/resolv.conf
each time it's updated.
Update
For Redhat based systems, use these steps:
Edit your network script which is located at: /etc/sysconfig/network-scripts
. You will see an entry for the network devices detected (i.e. ifcfg-eth0 for the network adapter eth0
).
Edit this file (/etc/sysconfig/network-scripts/ifcfg-eth0
):
Append this line:
DOMAIN=sample.com
Also run this command:
$ sudo hostnamectl set-hostname --static "testing01.d.sample.com"
You'll most likely have to reboot the system to make the changes take effect.
Solution 3
If "nmcli connection modify ..." has changed your connection file but not your active connection:
nmcli c load /etc/NetworkManager/system-connections/the-connection-name[-possibly-uuid-too]
man nmcli: connection-- load filename... Load/reload one or more connection files from disk. Use this after manually editing a connection file to ensure that NetworkManager is aware of its latest state.
Related videos on Youtube
James Shewey
I am an APU alumni who graduated with a a degree in Computer Science. During my time at APU, I gained a great love of Biblical Studies and aspire to learn as much as possible about the topic. I also frequently post or participate in the community of programming-related stack exchanges in the course of my duties as an IT DevOps Engineer. Website | Blog
Updated on September 18, 2022Comments
-
James Shewey over 1 year
With Network Manager in Red Hat 7, I am seeing an issue where the old/wrong search domain is being used after changing the hostname. In /etc/resolv.conf, I see:
# Generated by NetworkManager search **ec2.internal** d.sample.com nameserver 172.31.0.2
When I type
hostname
, I see my desired output:[root@testing01 ~]# hostname testing01.d.sample.com
But instead of replacing the search domains, it is appending the new domain name to the search domains. I want to completely get rid of ec2.internal and give this domain the ax altogether. Editing the
/etc/resolv.conf
file directly gets clobbered by Network Manager. I don't want to disable Network Manager, and I'd rather not disable NM's management of/etc/resolv.conf
unless I absolutely have to.So, 1) Why does NM keep reverting my search domain and 2) how can I fix this using nmcli or command line tools only?
-
James Shewey over 7 yearsThis did not work on Red Hat. The file did not exist and creating it did not solve the problem. It seems like this might work on Ubuntu/Debian only.
-
L. D. James over 7 years@JamesShewey I updated my answer to include the
Red Hat
specifics. By the way, there are usually a number of ways of achieving a desired result. I notice you also posted a variation. -
James Shewey over 3 yearsIt feels pretty clear from the man page that the author of the
hostname
command didn't intend this to be the case for 2 reasons: 1) them--short
switch which is toDisplay the short host name. This is the host name cut at the first dot.
it would make no sense to include this option if the command were not return an FQDN. 2) the man page states thathe FQDN is the canonical name returned by gethostbyname2(2) when resolving the result of the gethostname(2) name. The DNS domain name is the part after the first dot.
and that -
James Shewey over 3 years
hostname will print the name of the system as returned by the gethostname(2) function