Raspberry Pi DNS Settings: How to Change the DNS

In this Raspberry Pi DNS settings tutorial, we will be showing you how to change the DNS server that your Raspberry Pi connects to and uses for looking up domain names.

Raspberry Pi DNS Settings

The process of changing the DNS server on your Raspberry Pi is a pretty simple process and involves modifying a single file. In addition to showing you what file you will need to edit, we will also walk you through a couple of methods of ensuring your Raspberry Pi is using your newly set DNS.

There are many reasons why you might want to utilize a different DNS server to your ISP’s default. One of the top reasons being performance. Many ISP provided DNS servers can be incredibly slow at lookups and sometimes can take ages to update and add domains.

There is also the privacy factor to be considered, typically your ISP’s DNS server will leak your IP address as well as cache details about what websites you are visiting. Alternatives like Cloudflare 1.1.1.1 offer increased privacy by not logging any identifiable data and offering HTTPS over DNS.

If you’re looking to set up a dynamic DNS for accessing your Raspberry Pi then our tutorial on port forwarding and setting up Dynamic DNS will help you out. This tutorial simply goes into the steps of changing the way your Pi resolves domain names.

Equipment List

You will need the following equipment for this guide on setting the DNS server on the Raspberry Pi.

Recommended

Optional

Setting the DNS Servers on your Raspberry Pi

We recommend that you do this tutorial on the latest version of Raspbian to ensure that everything will work correctly. If you have any problems, please let us know in the comments at the bottom of this page.

1. Changing your DNS servers is a straightforward process on the Raspberry Pi. It just requires modifying one file, in this case, its the dhcpcd configuration file.

While in some setups you can modify the /etc/resolv.conf file you shouldn’t do this on Raspbian installations.

The reason for this is because Raspbian comes with a program called resolvconf, this file dynamically generates the resolv.conf file.

This means any changes to it can be overwritten at random making editing the “resolv.conf” file an unreliable solution.

Instead, we will be modifying the /etc/dhcpcd.conf file by running the following command on your Raspberry Pi.

sudo nano /etc/dhcpcd.confCopy

2. Now that we have the interfaces file open we need to add the following line to the bottom of the file.

This line will define the name servers for the Raspberry Pi to utilize. The resolvconf program will automatically use this value to regenerate the resolv.conf file.

Any IP Addresses specified after "static domain_name_servers=” will be utilized to try and make DNS requests.

In our example below you will see that we have defined both 8.8.4.4 and 8.8.8.8 which are Google’s Public DNS servers.

static domain_name_servers=8.8.4.4 8.8.8.8Copy

If you don’t want to utilize Google’s DNS Servers for your Raspberry Pi, there are a few other popular alternatives. The two most popular alternatives are Cloudflare Public DNS and OpenDNS.

One of the newest Public DNS is Cloudflare’s 1.1.1.1, with it being one of the fastest and most private public DNS available, even implementing modern technologies such as DNS over HTTPS (DoHS).

DoHS means that your connection between your computer and the DNS server is encrypted, unlike most DNS providers.

You can utilize the Cloudflare Public DNS by using the IP addresses 1.1.1.1 and 1.0.0.1

The second popular alternative to the Google Public DNS is OpenDNS which is run by Cisco. It operates using the IP’s 208.67.222.222 and 208.67.220.220.

OpenDNS openly attempts to block certain websites from loading such as phishing websites. It additionally allows you to setup your own filtering categories so you can filter out websites where desired.

3. With these changes now made to the /etc/dhcpcd.conf file you can now save it by pressing the following keys CTRL + X, then Y and finally ENTER.

4. Now that we have made changes to our dhcpcd configuration we need to restart its service by running the following command.

sudo service dhcpcd restartCopy

Testing the DNS Changes

1. Upon restarting the DHCPCD service, the resolvconf software will of automatically updated your resolv.conf configuration file.

You can verify that the new domain name server IP’s are being set correctly on your Raspberry Pi by opening the resolv.conf file with the following command.

sudo nano /etc/resolv.confCopy

2. Within this file, you should now see something like we have shown below, with the IP’s of the domain name servers that you set earlier in this Raspberry Pi DNS settings tutorial.

Just press CTRL + X to exit the file.

# Generated by resolvconf
domain home
nameserver 8.8.8.8
nameserver 8.8.4.4

3. If you are still unsure of whether your Raspberry Pi is using your newly set DNS servers, you can utilize the dig tool.

Before we can utilize the “dig” tool, we must first install the dnsutils package.

We can install the package by running the following command within the terminal on the Raspberry Pi.

sudo apt-get install dnsutilsCopy

4. Now to use the dig tool you just need to type in “dig” followed by a domain name.

In our example command below we will try using it on “pimylifeup.com“.

dig pimylifeup.comCopy

5. From this command, you should see some text such as what we have below.

This bit of text tells you what IP address that your Raspberry Pi queried to look up that domain name.

As you can see below our query was made with the Google Public DNS IP 8.8.8.8 which tells us that everything is working as it should be and that we have successfully changed the DNS server on our Raspberry Pi.

;; Query time: 26 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Dec 23 11:44:52 UTC 2018
;; MSG SIZE rcvd: 75

I hope that this tutorial has shown you how easy it is to change the DNS server on the Raspberry Pi to something like the Google Public DNS or other alternative DNS providers.

If you have any feedback on this guide on changing the Raspberry Pi DNS settings, then feel free to post a reply below.

Leave a Reply

Your email address will not be published. Required fields are marked *

4 Comments

  1. Avatar for Jeff in ABQ
    Jeff in ABQ on

    I’ve come back to this several times over the last couple of years (because my memory is weak as I am old) and each time it saves my bacon.

  2. Avatar for Mike
    Mike on

    Thank you for this great tutorial, works perfectly fine

  3. Avatar for Leigh
    Leigh on

    To echo the previous comment, great tutorial. Many thanks.

  4. Avatar for Mickael
    Mickael on

    Thank you for this very clear and concise tutorial, works like a charm and very nice to follow