8

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?

4 Answers 4

11

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.

2
  • 3
    Restarting NetworkManager to apply network configuration is wrong. Instead, reactive the connection profile via nmcli connection up, nmcli device connect, nmcli device reapply or nmcli device modify.
    – thaller
    Commented Jan 12, 2017 at 9:28
  • Actually, whether or not to restart NetworkManager can depend on whether or not you change /etc/NetworkManager/system-connections/<interface>. And very likely what you changed there. I had a system that stubbornly and silently refused to update resolv.conf until I fixed a UUID entry in that file and had to restart NetworkManager.
    – fbicknel
    Commented Feb 16, 2023 at 15:50
6

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.

2
  • This 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. Commented Jan 12, 2017 at 5:44
  • @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. Commented Jan 12, 2017 at 20:48
1

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.

-2

I would like to add: your first misunderstanding is what the host name is. When you type hostname at the command line it should only output the name of the server(host). The FQDN is a concatenation of the hostname and the domain.

If the command hostname outputs host.domain.com then you have named your host incorrectly. The reason why this is wrong is because you will append the search orders to the host name. Any resolution that needs to happen in the same domain would end up with a result of hostname.doimain.com.domain.com

  • The /etc/hosts file should be formatted like so: IP, hostname, hostname+domain the host belongs to
    xx.xx.xx.xx host host.domain.com
    
  • The /etc/resolve.conf file should contain:
    search domain.com
    
  • /etc/hostname file should contain
    host
    

The entries in the /etc/hosts file will allow the hostname and the FQDN to resolve the IP. DNS resolution will resolve the host and then append the search order to see if its in that domain.

2
  • 1
    It 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 to Display 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 that he 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 Commented Nov 13, 2020 at 16:35
  • 1
    hostname will print the name of the system as returned by the gethostname(2) function Commented Nov 13, 2020 at 16:35

You must log in to answer this question.

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