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

Posted
Comments 0

Set permanent DNS nameservers on Ubuntu or 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.

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:

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

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:

sudo systemctl status resolvconf.service

If you get the following message “Unit: resolvconf.service could not be found” then skip ahead and install resolvconf.

resolvconf service is not installed

If you get this message “Active: active (exited)“ then resolvconf is already installed. Skip to step 2.

resolvconf service is installed

Let’s install the resolvconf package:

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:

resolvconf service is installed

If you see a message “Active: inactive (dead)” you’ll need to enable and start the service.

resolvconf service is installed but not active

Let’s enable and start the resolvconf service:

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:

resolvconf service is installed

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
resolvconf head file, enter DNS IP addresses

Then save ctrl + o, ENTER and exit nano ctrl + x.

We need to update resolv.conf to use the new nameservers:

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 your DNS server IP’s for the nameserver option:

resolvconf with your chosen DNS IP addresses

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

Conclusion

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.

Newsletter Signup







Privacy Policy

See Also

Further Reading

Author
Categories Debian, Ubuntu

Comments

There are currently no comments on this article.

Comment

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.





PLEASE NOTE: You must preview a comment before submitting

Comments use Textile formatting