Ubuntu

Set permanent DNS nameservers on Ubuntu/Debian with resolv.conf

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.

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:

  • GOOGLE
  • 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.

Links & Resources

Share
Richie

I was a musician till the age of 30 with a keen interest in technology. I did a short web design course which sparked my interest in programming and in 2009 I launched Ricmedia Guitar, then Ricmedia PC Help. I now publish tech articles and tutorials on the main ricmedia.com domain.

View Comments

  • 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??

    • Hi, are you editing /etc/resolv.conf directly or /etc/resolvconf/resolv.conf.d/head file?

      cheers
      Rich

  • 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.

    • 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

Recent Posts

Build a Mini-ITX NAS RAID Server with Enterprise SAS Hard Drives

Today I'll show you how to build a rock-solid home/office NAS server using a Mini-ITX… Read More

1 year ago

Reset Apple Safari to default settings

Since Safari version 9.0 (macOS El Capitan), Apple have removed the menu option to Reset… Read More

1 year ago

Enable God Mode on Windows 10

How would you like to see all the hidden expert settings available on your Windows… Read More

1 year ago

Perform disk error check (chkdsk) on Windows 10

Today, I'll show you how to run Windows Check Disk (chkdsk) to find and fix… Read More

1 year ago

How to install Debian Linux – Complete Guide

Today, I'll show you how to download and install Debian Linux, simply follow each step… Read More

1 year ago

Change browser language in Mozilla Firefox on Android

Today, I'll show you how to change the default display language in Mozilla Firefox on… Read More

1 year ago