RHEL 7
How to set DNS in CentOS/RHEL 7 & prevent NetworkManager from overwriting /etc/resolv.conf?
Source : [enter link description here][1]
Step 1
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:
...
[main]
#plugins=ifcfg-rh,ibft
...
Add dns=none just after the [main] tag like this:
...
[main]
dns=none
#plugins=ifcfg-rh,ibft
...
Go ahead and save the file.
Step 2
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.
Step 3
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
nameserver 8.8.8.8
nameserver 8.8.4.4
RHEL 6
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/][2]
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:
#cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
DEVICE=eth0
BOOTPROTO=dhcp
PEERDNS=no
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