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.
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 126.96.36.199 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.
You will need the following equipment for this guide on setting the DNS server on the Raspberry Pi.
Micro SD Card if you’re using a Raspberry Pi 2, 3 or B+
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.conf
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 188.8.131.52 and 184.108.40.206 which are Google’s Public DNS servers.
static domain_name_servers=220.127.116.11 18.104.22.168
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 22.214.171.124, 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 126.96.36.199 and 188.8.131.52
The second popular alternative to the Google Public DNS is OpenDNS which is run by Cisco. It operates using the IP’s 184.108.40.206 and 220.127.116.11.
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 restart
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.conf
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 18.104.22.168 nameserver 22.214.171.124
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 dnsutils
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”.
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 126.96.36.199 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: 188.8.131.52#53(184.108.40.206) ;; 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.