How to change DNS servers on Linux

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to configure resolv.conf file. resolv.conf  is the file that contains all the configurations for the DNS that system uses for resolving a dns query.

Whether you want to use safe DNS servers for safely browsing on the internet, or you want specific domain to resolve to a custom IP, then you can do this easily by configuring the resolv.conf file.

Step 1 - Edit resolv.conf

Run the following command to open resolv.conf with nano text editor. You can use any text editor to edit the resolv.conf file.

sudo nano resolv.conf

💡
In order to be able to save the changes you make on this file, sudo permission is required. Make sure you have sudo permission on the account that you are using to do this task. If you don't have sudo permission changes won't be able to be saved.

Step 2 - Set up DNS

Once you open resolv.conf with the text editor, you need to set your own configuration. For example, if you want to set dns servers, use the following syntax

nameserver IP
nameserver IP

For example, to use OpenDNS family shield DNS you need to replace the IP with the following IPs

nameserver 208.67.222.123
nameserver 208.67.220.123

The resolv.conf should look like this

💡
Make sure you trust the service that you are going to use as your main DNS on your machine, because all the dns traffic will be going through these servers. We chose OpenDNS as the DNS for this tutorial because it is a safe service offered by Cisco.

Now, save the changes by pressing the CTRL + X then press Y to confirm.

Step 3 - Make changes permanent

With the current configuration, the dns servers that we set on resolv.conf will be overwritten by the network manager the next time we boot the system. To avoid, this we can use a tool called resolvconf, this tool will make those changes permanent and your system will be using the DNS config you set even after rebooting.

Let's install resolvconf first

sudo apt install resolvconf

Next, open the following config file with a text editor

sudo nano /etc/resolvconf/resolv.conf.d/head

Now, on this file you need to set the same DNS servers that you put on resolv.conf.

nameserver 208.67.222.123
nameserver 208.67.220.123

Once done, apply the changes you made by using the command below

sudo resolvconf -u

Now, your system should be using the dns servers you set on resolv.conf. You can view the content of resolv.conf using the cat command

cat /etc/resolv.conf

Output

nameserver 208.67.222.123
nameserver 208.67.220.123

However, to make sure that the dns servers are configured correctly, you can use the resolvectl command

resolvectl

And in the ouput it will show you the DNS servers for each network device that your system is using

Conclusion

In this tutorial, you learned how to change the DNS servers on resolv.conf. We also explained how to make these changes permanent so whenever your system boots you won't lose the configuration of resolv.conf.