This guide will show you how to edit, add, or remove the DNS settings on your Linux device.
We will cover the following:
Configure custom DNS on Linux
-
Open the terminal with the CTRL + ALT + T combination and type in the following commands:
sudo rm -r /etc/resolv.conf
sudo nano /etc/resolv.conf
-
You will be asked for your root password after each command line. Type it in and press Enter.
-
When the text editor opens, you will have to type in these lines:
nameserver 162.252.172.57
nameserver 149.154.159.92
- Close and save the file by clicking Ctrl + X and pressing Y.
-
After that, please type in one last command in the terminal and press Enter:
sudo chattr +i /etc/resolv.conf
In case the chattr +i command does not work, try using the following command instead:
sudo chattr -f +i /etc/resolv.conf
- Restart your Linux device.
Alternative solution to configure custom DNS (with Raspberry Pi)
-
Open the terminal with the CTRL + ALT + T combination and type in the following commands:
sudo nano /etc/dhcpcd.conf
- You will be asked for your root password. Type it in and press Enter.
-
Find the section related to your VPN network interface (Typically, it’s going to be named tun0 or tap0 for OpenVPN connections or wg0 for Wireguard connections). It might look something like this:
interface tun0
NOTE: If this section does not exist, you can create it.
-
Add or modify the static domain_name_servers line to specify the DNS servers you want to use. For example, to use Surfshark’s public DNS servers, modify it like so:
interface tun0
static domain_name_servers=162.252.172.57 149.154.159.92
- Save the changes by pressing CTRL + O, then press Enter. To exit, press CTRL + X.
NOTE: Changes to /etc/dhcpcd.conf are generally more persistent and less likely to be overridden by automatic processes, so you do not need to apply the chattr +i command on it. -
Restart the network service to apply the new DNS settings using this command:
sudo service dhcpcd restart
- Alternatively, you can reboot your Linux system.
You may also be interested in: