Learning, Raspberry Pi Projects

Change the Raspberry Pi DNS Settings

In this quick tutorial we will cove the right way to change the Raspberry Pi DNS settings so you can change your DNS server to Google DNS, OpenDNS, or even the DNS server from your ISP.

DNS or Domain Name Service is the service which your Raspberry Pi (and all internet connected devices) convert the name of a host (or website) into an IP address that your network can contact.  We’ll go over the very simple process of changing these settings to something that better suits your needs.  Many people wish to switch away from their ISPs DNS for privacy reasons or performance gains.  Many Internet Service Provider DNS servers are not only very slow, but these ISPs also capture and sell this data to other companies for marketing purposes! Sneaky bastards!

Parts list for this project:

You’ll of course need a Raspberry Pi and accessories for this project, here’s a simple list to get started.

Changing the DNS Servers on a Raspberry Pi

Before starting this project it is highly recommended that you update Raspbian to the latest version of the operating system.  Almost every problem reported to us is fixed by doing this first.  To update your Raspberry Pi do the following:

[code language=”bash”]
sudo apt update
sudo apt upgrade
[/code]

Be careful of instructions floating around the web telling you to modify the /etc/resolv.conf file! This will not work on Raspbian will not change raspberry pi DNS settings!  Raspbian uses a newer method of controlling DNS settings, as part of the /etc/dhcpcd.conf file. If you make changes directly to resolv.conf they will be overwritten at various times and on reboot.

Run the following command to edit dhcpcd.conf:

[code language=”bash”]
sudo nano /etc/dhcpcd.conf
[/code]

Once opened, add one of the following lines to the very end of the file:

For Google DNS Servers:

[code language=”bash”]
static domain_name_servers=8.8.4.4 8.8.8.8
[/code]

For Open DNS Servers:

[code language=”bash”]
static domain_name_servers=208.67.222.222 208.67.220.220
[/code]

For Cloudflare’s DNS Servers:

[code language=”bash”]
static domain_name_servers=1.1.1.1 1.0.0.1
[/code]

For your ISP, change the IP addresses to your own ISPs servers.  For your router, your address will be something like 192.168.0.1, or 192.168.1.1 in most cases.

After making these changes its time to save the /etc/dhcpcd.conf file.  In Nano, we save files by pressing CTRL + O. Then press CTRL + X to exit Nano.  Now restart the the Raspbian dhcpd service to cause Raspbian to start using these new DNS server settings.

[code language=”bash”]
sudo service dhcpcd restart
[/code]

That’s all it takes to change raspberry pi DNS settings!  But before you go, let’s test them to make sure they work!

Testing the Raspberry Pi DNS Server Changes

If you’re a true geek, you’ll want to do what a geek does and open the hood to see your changes.  Use Nano again to open the /etc/resolv.conf and you’ll see your new DNS servers are now present (assuming you did everything above correctly).

[code language=”bash”]
sudo nano /etc/resolv.conf
[/code]

Of course, the simplest way to see if they worked is to run nslookup.  Type the following command:

[code language=”bash”]
nslookup www.thegeekpub.com
[/code]

You should be greeted with the following message if everything worked as planned (in this example, we used Google DNS):

[code language=”bash”]
Server: 8.8.8.8
Address: 8.8.8.8#53

Non-authoritative answer:
www.thegeekpub.com canonical name = thegeekpub.com.
Name: thegeekpub.com
Address: 107.20.215.52
[/code]

I hope this quick tutorial helped you along the way in setting up and using your Raspberry Pi!  Check out some of our many other 50+ Raspberry Pi projects before you go!

[amazon_link asins=’B01H4ZLZLQ,B01LPLPBS8,B01EV6LJ7G,B01EV70C78′ template=’ProductGrid’ store=’murraynet-20′ marketplace=’US’ link_id=’c98daf12-a9c4-49c2-8a92-850bbd2bb86f’]

5 thoughts on “Change the Raspberry Pi DNS Settings

  1. Mike Murray says:

    4.5

  2. tmaster says:

    How do you stop it from changing on every reboot

  3. Dale says:

    nslookup command does not exist on my pi3

    1. ghostccj says:

      you need install the package ———>sudo apt-get install dnsutils

Leave a Reply

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