Change DNS Server in Ubuntu

Changing the DNS server on Ubuntu is an easy task, but needs to be done carefully. Here's how to do it in the command line.

Changing the DNS server on Ubuntu is an easy task, but needs to be done carefully.

This article shows you how to do it easily.

Step 1: Check your current DNS Server

The first step is to always check, and note down the currently used DNS Servers – in case the change in DNS Server does not result in intended changes.

To show the current DNS servers that are being used per interface, use the 'resolvectl' command:

resolvectl status

For systems running Ubuntu 20.04 or newer, please use the following command:

systemd-resolve --status

Running either of these commands will show you the DNS server(s) being used by each network interface.

Let us see the output of the 'resolvectl' command on my computer:

$ resolvectl status
       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub

Link 2 (enp1s0)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server:
       DNS Servers:

As evident from the output, my current DNS provider is Google.

I'd like to change that to something else, maybe Cloudflare.

Step 2: Temporarily change DNS (to test it out)

Before you change the DNS permanently, it is best to change it temporarily and see the results.

If there is regression, no need to revert as this is only a temporary change. If the results are as expected, we will make it permanent.

To temporarily change the DNS server, edit the /etc/resolv.conf file.

In /etc/resolv.conf, the line which starts with the keyword nameserver deals with DNS Servers.

NEVER remove the line that says nameserver Comment it out by putting a pound/hash symbol at the beginning of that line.

Add one or two lines that begin with nameserver and specify the IP address next to it. Below is what it should look like (if I want Cloudflare as my DNS provider):


The first line is your primary DNS server and second line is the fall-back DNS server. The fall-back DNS server is used when [for some reason] the primary DNS server is unavailable.

Once these two lines are added to your /etc/resolv.conf file, you should immediately see the results.

You can verify if the DNS server changed with the help of dig command.

$ dig | grep SERVER

Grep-ing the output, we see that Cloudflare's DNS servers are being used. That confirms that the temporary change in DNS server was in effect immediately.

After temporarily switching DNS, if you do not like the change, remove the lines that you had added and uncomment the nameserver line.

Available DNS Providers

In case you are not aware of IP addresses for DNS servers, below is the table of a few popular DNS providers, and their primary and fall-back addresses:

Provider Primary IP Address Fall-back IP Address
Cloudflare (malware blocking)

To use Cloudflare (with malware blocking) as my DNS provider, I will use the and IP addresses.

Step 3: Permanently change DNS

After temporarily changing your DNS provider, if you are satisfied with the results, it is time to make this change permanent.

Method 1: The easy way :)

To permanently change your DNS server, install the resolvconf package using the following command:

sudo apt-get install resolvconf

Once that is installed, edit the /etc/resolvconf/resolv.conf.d/head file and add the same nameserver lines to it like so (assuming Cloudflare as DNS provider):


Once that is done, start the resolvconf.service with the following command:

sudo systemctl enable --now resolvconf.service

That is all!

Method 2: The not-so-easy way

One way to permanently change your DNS server is to edit the YAML file that resides in the /etc/netplan/ directory.

Before that, note down the name of your network interface beforehand. You can do so using the ip command:

ip addr

That will list various network interfaces. Locate the interface and note it down.

Usually, there is only one file in /etc/netplan/ directory, but the name is mostly different. If there are multiple files, grep all files for your interface name. That should narrow down the candidate file to one.


Once you know the filename, open it in an editor. You should see something similar to my output:

      dhcp4: true
  version: 2

My network interface is called 'enp1s0', yours might be different.

Under my interface, I will add the nameservers field (below, not under dhcp), and another filed called addresses under it as well. I will specify the address in a bracket, separated by commas, like so:

      dhcp4: true
        addresses: [,]
  version: 2

Once that is done, save changes and exit.

Then, run the following command to make changes effective:

sudo netplan apply

Done! You have now permanently changed your DNS server :)