In this article, we’ll learn how to change DNS on Linux! DNS or Domain Name Server is a standard protocol that helps Internet users discover websites using human-readable addresses. If the DNS of your device is misconfigured or you want to change it to bypass a website block, this article will explain exactly how to do it.
How to change DNS on Linux?
The DNS configurations are stored in a file named resolv.conf. Here is a step-by-step guide to change it.
Step 1: Open /etc/resolv.conf in a text editor with sudo privileges.
sudo nano /etc/resolv.conf
Step 2: Add the lines for Domain nameservers you want to use. For example, if you want to use 22.214.171.124 as your DNS, add nameserver 126.96.36.199 it to the file.
Step 3: Save resolv.conf. If you are using nano to edit the file, press
Ctrl + X and then Y when it asks you to save the file.
How to permanently change DNS on Linux
When you change DNs on Linux using the above method, the changes are temporary. You will see that resolv.conf will reset after a restart or anytime you run dhclient.
However, there are multiple ways to make them permanent. We will discuss some of them here.
1. Change systemd configurations
One way you can make the DNS changes permanent is by not changing it from /etc/resolv.conf but from the systemd config itself (which can not be overwritten).
To do that, open /etc/systemd/resolved.conf with sudo privileges.
sudo nano /etc/systemd/resolved.conf
Now uncomment the DNS and Fallback DNS lines and add your preferred name servers.
Now save the file to change the nameservers.
You can install a package called resolvconf to manage the DNS. This package acts as a mediator between programs that supply DNS information and applications that use DNS information.
When this package is installed, it autogenerates DNS settings and overwrites the information in /etc/resolv.conf with its own settings.
Hence if we change the default DNS settings in this program, our desired DNS settings will be the ones that are written in /etc/resolv.conf every time, hence making it permanent.
To install resolvconf:
- Debian/Ubuntu :
sudo apt install resolvconf
- Arch :
sudo pacman -S openresolv
Now we want to place our DNS in the header file of the settings that will be autogenerated. To do that open /etc/resolvconf/resolv.conf.d/head and add the nameservers just like we did for resolv.conf
sudo vim /etc/resolvconf/resolv.conf.d/head
How to check which DNS is in use
You can verify that the changes have been reflected by running the dig command.
Also read: dig command in Linux for DNS lookups
We have seen how to make temporary and permanent changes in the DNS settings as well as how to verify if those changes are applied. To learn more about DNS and the networking configs, you can head over to the man pages. Thank you and keep exploring!