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.
What you need
- A CentOS 7 or a Red Hat Enterprise Linux (RHEL) 7 server
- A couple of minutes
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
The NetworkManager configuration is located here:
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 ...
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
Open this file in you favorite text editor and specify the name servers as follows:
# Generated by NetworkManager nameserver 18.104.22.168 nameserver 22.214.171.124
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