Setting custom DNS servers on Linux can increase performance, security and even thwart some websites using Geo-blocking via DNS. There are several ways to do this including Network Manager GUI that’s included in many Linux distros like Debian, Ubuntu and derivatives, Netplan which is now included as standard in Ubuntu 20.04; or using resolv.conf
(not directly, but via the head file). This guide will use resolv.conf, see my other guide for setting custom DNS servers using Network Manager or Netplan.
FYI: The resolv.conf
file is overwritten on each boot so we can’t edit this file directly. Instead, we edit one of the two files used to create the resolv.conf
file, those being the head
and base
files. We’ll be editing the head
file so that each boot-up, resolv.conf
gets written with our custom DNS servers at the top.
Contents
Free DNS providers
Before changing DNS servers, you’ll need to find a third-party DNS provider, there are plenty of good (and free) services available. I recommend Google DNS which is what I use and have never had an issue. I will list here the most popular DNS providers:
- Primary IPv4: 8.8.8.8
- Secondary IPv4: 8.8.4.4
- Preferred IPv6: 2001:4860:4860::8888
- Alternate IPv6: 2001:4860:4860::8844
- OPENDNS
- Primary: 208.67.222.222
- Secondary: 208.67.220.220
- Preferred IPv6: 2620:0:ccc::2
- Alternate IPv6: 2620:0:ccd::2
- DNS.WATCH
- Primary: 84.200.69.80
- Secondary: 84.200.70.40
- LEVEL3
- Primary: 209.244.0.3
- Secondary: 209.244.0.4
- NORTON
- Primary: 199.85.126.10
- Secondary: 199.85.127.10
- COMMODO
- Primary: 8.26.56.26
- Secondary: 8.20.247.20
- VERISIGN
- Primary: 64.6.64.6
- Secondary: 64.6.65.6
- MORE SERVERS…
- See: Free and Public DNS Servers
- See: Public DNS Server List (extensive)
1. Installing resolvconf package
Depending on which version of Ubuntu/Debian you’re using, resolvconf may or may not be installed, so let’s check before we continue.
Open up a Terminal window and enter the following code:
sudo systemctl status resolvconf.service
If you get the following message “Unit: resolvconf.service could not be found” then skip ahead and install resolvconf.
If you get this message “Active: active (exited)” then resolvconf is already installed. Skip to step 2.
Let’s install the resolvconf package. Enter the following code:
sudo apt update
sudo apt install resolvconf
Let’s make sure resolvconf was successfully installed and is running:
sudo systemctl status resolvconf.service
You should see “Active: active (exited)” message as show below:
If you see a message “Active: inactive (dead)” you’ll need to enable and start the service.
To enable and start the resolvconf service, enter the following code:
sudo systemctl enable resolvconf.service
sudo systemctl start resolvconf.service
sudo systemctl status resolvconf.service
After the last command, you should see the “Active: active (exited)” message:
2. Set DNS servers in resolv.conf using head file
Now we get to the meat of this article. Let’s open the head file:
sudo nano /etc/resolvconf/resolv.conf.d/head
Enter your nameservers below the comments (I’m using Google’s DNS servers).
nameserver 8.8.8.8
nameserver 8.8.4.4
Now save Ctrl+o and exit Ctrl+x
We need to update resolv.conf to use the new nameservers. Enter the following code:
sudo resolvconf --enable-updates
sudo resolvconf -u
Now open resolv.conf to confirm our nameservers have been written to it.
sudo nano /etc/resolv.conf
You should see the following:
Video Guide
Summary
I hope this guide helped you to set your custom (and permanent) DNS servers for your Ubuntu or Debian machine. If you had any trouble or just want to say hi, leave a comment and I’ll help you out.
Saurabh says
Hi,
I have follow all the process and set DNS, but the issue is this after some days content written in resolv.conf is being reset automaticall and it became blank. Is there any solution for this??
Richie says
Hi, are you editing /etc/resolv.conf directly or /etc/resolvconf/resolv.conf.d/head file?
cheers
Rich
James says
Thanks Rich, very helpful!
Richie says
no worries James 🙂
Joshua says
I have run through this a couple of times, but the outcome is always the same. Everything works perfectly as described until I run the ‘sudo resolvconf -u’ command and I get the following error:
/etc/resolvconf/update.d/libc: Warning: /etc/resolv.conf is not a symbolic link to /run/resolvconf/resolv.conf
Checking the resolv.conf file it just shows what it did previously.
Richie says
Hi Joshua, that issue can usually be resolved buy reconfiguring resolve.conf, enter the following code:
sudo dpkg-reconfigure resolvconf
and say yes to “prepare /etc/resolve.conf for dynamic updates?” and reboot.let me know how you go mate.
cheers
Rich