Networking

Setting Up a Personal DNS Server on a Raspberry Pi

Spread the love

Setting up a personal DNS server offers a great way to learn about DNS and control your local network. A Raspberry Pi, with its affordability and accessibility, makes this a surprisingly simple project. This guide will walk you through the process, enabling you to manage DNS records for your home network.

Table of Contents

Setting a Static IP Address on Your Raspberry Pi

Before configuring your DNS server, it’s crucial to assign a static IP address to your Raspberry Pi. This prevents IP address changes from disrupting DNS functionality. We’ll use the Raspberry Pi Configuration Tool:

  1. Access the Configuration Tool: Open a terminal on your Raspberry Pi (or via SSH) and type:
  2. sudo raspi-config
  3. Network Options: Navigate to “5 Interfacing Options” then “P5 Network Configuration”.
  4. Set Static IP: Select “Manual” configuration. Enter your desired static IP address, subnet mask, gateway, and DNS server addresses (use your router’s DNS servers temporarily). Ensure the IP address is within your network’s range and doesn’t conflict with other devices. Note down these settings.
  5. Save and Reboot: Save changes and reboot your Raspberry Pi using:
  6. sudo reboot
  7. Verify: After rebooting, verify your static IP using:
  8. ip addr show

Setting Up the DNS Server (Bind9)

We’ll utilize Bind9, a reliable and widely-used DNS server.

  1. Update Packages: Update your Raspberry Pi’s package list:
  2. sudo apt update
  3. Install Bind9: Install Bind9 and its utilities:
  4. sudo apt install bind9 bind9utils
  5. Configure Bind9: The primary configuration file is located at `/etc/bind/named.conf.local`. Add entries for your domains. For example, to serve records for `example.local`, add:
  6. zone "example.local" {
        type master;
        file "/etc/bind/db.example.local";
    };
  7. Create the Database File: Create `/etc/bind/db.example.local` with the following (replace placeholders with your information):
  8. $TTL    604800
    @       IN      SOA     raspberrypi.example.local. admin.example.local. (
                                            2023102702 ; Serial (increment this number with each change)
                                            86400      ; Refresh
                                            7200       ; Retry
                                            3600000    ; Expire
                                            604800     ; Negative Cache TTL
                                    )
    @       IN      NS      raspberrypi.example.local.
    raspberrypi IN A 192.168.1.100
    www      IN      A       192.168.1.100
  9. Restart Bind9: Restart Bind9 to implement changes:
  10. sudo systemctl restart bind9
  11. Firewall (Optional): If using a firewall (e.g., `ufw`), allow DNS traffic (port 53):
  12. sudo ufw allow 53/udp
    sudo ufw allow 53/tcp

Testing Your DNS Server

  1. Client Configuration: On a device in your network, change its DNS server settings to your Raspberry Pi’s static IP address.
  2. Test Resolution: Ping or browse to a hostname defined in `db.example.local` (e.g., `raspberrypi.example.local` or `www.example.local`). Successful resolution confirms your DNS server is operational. If not, double-check your configuration files.

Remember to replace placeholder values with your actual IP addresses and domain names. This is a basic setup; for advanced configurations, consult the Bind9 documentation. Always back up configuration files before making changes.

Leave a Reply

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