How to set DNS in CentOS/RHEL 7 & prevent NetworkManager from overwriting /etc/resolv.conf?
Source : [enter link description here]
The NetworkManager configuration is located here: /etc/NetworkManager/NetworkManager.conf Open this file using vim or your favorite text editor.
Search for the [main] section in this file. It should look something like this:
Add dns=none just after the [main] tag like this:
Go ahead and save the file.
Let’s restart the NetworkManager.service service so that it picks up the changes we made to the configuration.
sudo systemctl restart NetworkManager.service
Note that the service name NetworkManager.service is case-sensitive.
Now, let’s add our nameservers to /etc/resolv.conf
Open this file in you favorite text editor and specify the name servers as follows:
# Generated by NetworkManager
CentOS / RHEL : DNS servers in /etc/resolv.conf change after a reboot/network service restart. How to make them permanent
Source : [https://www.thegeekdiary.com/centos-rhel-dns-servers-in-etcresolv-conf-change-after-a-rebootnetwork-service-restart-how-to-make-them-permanent/]
You would face this issue after a reboot or a network service restart. This usually happens as the scripts /etc/sysconfig/network-scripts/ifup-post and /etc/sysconfig/network-scripts/ifdown-post checks for the parameters “RESOLV_MODS=no” or “PEERDNS=no” in the network interface configuration file such as /etc/sysconfig/network-scripts/ifcfg-*. If these either of these parameters are not present, it will replace the contents of /etc/resolv.conf with /etc/resolv.conf.save. By default, PEERDNS and RESOLV_MODS are null.
The /etc/resolv.conf file will be overwritten if any network interfaces use DHCP for activation. To prevent this, ensure such interfaces have PEERDNS=no set in their ifcfg file, for example:
The ifcfg-file directives DNS1 and DNS2 can also lead to modification of resolv.conf. To prevent this, either remove said directives or use chattr(1) to make resolv.conf immutable to changes, i.e.:
#chattr +i /etc/resolv.conf