This guide shows you how to set custom DNS entries for CentOS 7 / RedHat 7 and ensure that the settings are persistent even after a reboot.
In CentOS and Red Hat Enterprise Linux (RHEL) 7, any custom DNS entries are stored in the file /etc/resolv.conf
. However, if we simply go ahead and add our nameservers to this file, we’ll notice that after a reboot or a restart of the network.service, the file is overwritten by NetworkManager.
In this guide, we will first configure NetworkManager to not overwrite this file. Then, we will go ahead and actually add our custom nameservers to /etc/resolv.conf
.
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.
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
nameserver 8.8.8.8
nameserver 8.8.4.4
That’s it! You’re done. The nameservers added to /etc/resolv.conf
will now persist even after a reboot. NetworkManager will not longer overwrite this file.
Photo by Steve Johnson on Unsplash