Follow through this guide to learn how to make DNS permanent changes on resolv.conf in Linux.
According to man
resolv.conf, resolv.conf is a resolver configuration file. The resolver is a set of routines in the C library that provide access to the Internet Domain Name System (DNS). The resolver configuration file contains information that is read by the resolver routines the first time they are invoked by a process. The file is designed to be human readable and contains a list of keywords with values that provide various types of resolver information. The configuration file is considered a trusted source of DNS information (e.g., DNSSEC AD-bit information will be returned unmodified from this source).
If this file does not exist, only the name server on the local machine will be queried, and the search list contains the local domain name determined from the hostname.
Make Permanent DNS Changes on resolv.conf in Linux
Any changes made manually to the
/etc/resolv.conf configuration file are bound to be overwritten upon changes in the network or system reboot.
According to the comments made in the file, the file is dynamic. “DO NOT EDIT THIS FILE BY HAND — YOUR CHANGES WILL BE OVERWRITTEN“.
So take for example, if you want to add a DNS server on your Linux box, you would usually update this file by specifying the IP address of a name server that the resolver should query. See the command below,
which updates the resolv.conf file with the public primary DNS server for Google DNS, by running a command like;
echo "nameserver 220.127.116.11" > /etc/resolv.conf
If you make any changes, like a system reboot, or run the
dhclient command the line added above, will disappear.
So how do you make permanent DNS changes on resolv.conf in Linux?
Well, there are a number of ways in which you can you can make permanent DNS changes on resolv.conf in Linux.
- Use Resolvconf framework
- Set the name server IP address on your Interface settings
- Update the DNS server settings on dhclient.conf
Use Resolvconf framework
Resolvconf is a framework for keeping up to date the system’s information about name servers. It sets itself up as the intermediary between programs that supply this information (such as ifup and ifdown, DHCP clients, the PPP daemon and local name servers) and programs that use this information such as DNS caches and resolver libraries).
On Ubuntu/Debian distros, you can install resolvconf framework by running the command below;
sudo apt install resolvconf
Once the framework is installed, it is started and enabled to run on system boot.
Next, edit the configuration file,
/etc/resolvconf/resolv.conf.d/base, and enter your DNS settings. See example setting below;
sudo vim /etc/resolvconf/resolv.conf.d/base
domain kifarunix.com nameserver 18.104.22.168 nameserver 22.214.171.124
Save and exit the configuration file.
/etc/resolv.conf file to make permanent DNS changes;
sudo resolvconf -u
Update the DNS server settings on dhclient.conf
If you are using DHCPd for automatic IP address assignment, edit the
/etc/dhcp/dhclient.conf file and add the line;
supersede domain-name-servers IP1, IP2;. Replace IP1 and IP2 with your respective name server IP addresses;
supersede domain-name-servers 126.96.36.199, 188.8.131.52;
Save the file and exit.
Now, if you run dhclient, your
/etc/resolv.conf will be updated with the name servers defined in the dhclient.conf.
You can use the
prepend option instead of
supersede to add additional IP addresses to the default one provided by the ISP.
Set the name server IP address on your Interface settings
Edit your network interface configuration file and add the address of the name server.
In Ubuntu 18.04/20.04 you would update the Netplan configuration file like;
sudo vim /etc/netplan/01-network-manager-all.yaml
network: version: 2 renderer: networkd ethernets: wlp0s20f3: dhcp4: no addresses: [192.168.100.80/24] gateway4: 192.168.100.1 nameservers: addresses: [184.108.40.206]
We set the DNS to google public DNS server address, 220.127.116.11. It can be different for your case.
Apply the changes;
sudo netplan apply
On Debian and <= Ubuntu 16.04;
auto wlp0s20f3 iface wlp0s20f3 inet static address 192.168.100.80 netmask 255.255.255.0 dns-nameservers 18.104.22.168
Restart networking to apply the changes;
systemctl restart networking
On CentOS and similar derivatives, edit the interface in question as follows. Replace INTERFACE with your interface name.
nmcli con mod INTERFACE ipv4.dns 22.214.171.124
Also, disable network interface management by the NetworkManager daemon.
echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/ifcfg-INTERFACE
Apply the changes;
nmcli con down INTERFACE nmcli con up INTERFACE
You should now have static DNS set.
And that concludes our guide on how to make DNS permanent changes on resolv.conf in Linux.