September 25, 2023


Networking, Computer, PBX, IT, DIY Solution

DIY project: How to use Raspberry Pi to build DNS server?

4 min read

DIY project: How to use Raspberry Pi to build DNS server?


DIY project: How to use Raspberry Pi to build DNS server? 

Self-built DNS server can avoid DNS hijacking by ISP and speed up network access to a certain extent. In addition, after customizing some configurations, ad blocking and other functions can be realized. Here we use dnsmasq on the Raspberry Pi to build such a DNS server.

Install dnsmasq

sudo apt-get install dnsmasq


Configure dnsmasq

First back up the original configuration file, the command line is as follows:

sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

Then edit the configuration file, the command line is as follows:

sudo vi /etc/dnsmasq.conf

Here, we first press esc to enter the command mode, then use the shortcut key yddG to clear the file, then use i to enter the insert mode, ctrl+v to paste the following configuration content, press esc after pasting is complete, and finally use :wq to save the file:


Which the Raspberry Pi LAN ip, address=/ order to test the rules and raspberry come to the site to resolve


Other explanations:

  • resolve-file: Define the upstream DNS server of dnsmasq, the default is /etc/resolv.conf. But resolv.conf will be reset by the system every time the Raspberry Pi restarts, so we specify the resolve-file file


  • strict-order: Means to perform DNS resolution from top to bottom in strict accordance with the order in the resolv-file file


  • listen-address: Define the address that dnsmasq listens to. The default is to monitor all network cards of the machine. In order to use other devices on the LAN, you need to add the LAN ip of the Raspberry Pi


  • cache-size: the number of caches


  • address: The ip address for custom domain name resolution, take the domain name as an example. It can also be configured in other files, and the configuration file can be placed in /etc/dnsmasq.d/. For example, you can create a new file test.conf under /etc/dnsmasq.d/, add this rule, and the effect is equivalent

Edit my_dns.conf, the command line is as follows:

sudo nano /etc/my_dns.conf

Paste the following content:


ctrl+o to save, then press Enter to confirm, then ctrl+x to exit.

Note, major dns (DNSpod recommended):

Google DNS:

Cloudflare DNS:





Ad blocking and other configuration

dnsmasq will first detect the hosts file and parse the ip through it. We can modify the Raspberry Pi hosts file to achieve the need for blocking advertisements and other requirements.

Create a hosts folder in the user directory (/home/pi), place a script in the folder, and modify the file permissions:

cd ~
mkdir hosts
cd hosts
sudo chmod 777

After editing the file, the command line is as follows:


Paste the following content:

cd /home/pi/hosts/
echo “***download g hosts file***”
wget –no-check-certificate -O hosts_g.txt;
echo “***download ad hosts file***”
wget –no-check-certificate -O hosts_ad.txt;
echo “***merge hosts file***”
cat hosts_g.txt hosts_ad.txt > hosts.txt
echo “***copy hosts file***”
sudo cp hosts.txt /etc/hosts
echo “***hosts file update done”

ctrl+o to save, then press Enter to confirm, then ctrl+x to exit.

Run it after editing, the command line is as follows:

sudo sh

Set the timing to execute the update hosts script, the command line is as follows:

sudo crontab -e

Add the following content to the end of the file (executed once every day at 3:30):

30 3 * * * sh /home/pi/hosts/

Restart dnsmasq

sudo service dnsmasq restart

View dnsmasq status

sudo service dnsmasq status


View network status

You can use dig to check the relevant URL to test whether dns is effective.

If dnsutils is not installed, install it through the following command:

sudo apt-get install dnsutils

Then you can simply test through the following command line:




Use DNS on other devices

It can be set individually only on the device, as follows:



Open network settings-advanced-dns and configure it as Raspberry Pi ip.



Control Panel-Network and Internet items-View network status and tasks-Change adapter settings-Right-click the local connection being used-Properties-Double-click Internet Protocol 4 (TCP/IPV4), and fill in the content to the relevant section.

Here, it is recommended to configure the router, that is, configure the DNS of the router’s LAN port as the Raspberry Pi ip, so that there is no need to configure the device separately.

For example: ASUS router (RT-AC68U), set the internal network-DHCP server-DNS Server 1 to the Raspberry Pi ip, then apply it, other devices are similar.




Leave a Reply

Copyright © All rights reserved. | Newsphere by AF themes.