How to make a custom DNS server
Everything that you need to know about in a DNS server
What is a DNS server?
The Domain Name Server (DNS) is a system that maps device IP addresses to specific domain names. For example, if the IP address 10.10.205.11 is assigned to a server, the DNS server can map it to the domain Bego.com. Here’s how it works: When a user types Bego.com into their browser, the browser sends a request to the DNS server associated with the user’s home router. The DNS server then looks up the corresponding device IP address for the domain and forwards the request to the matching device. The device processes the request and sends a response back to the user’s device.

Foundation of a DNS server?
In dns server. we are able to see query types and dns records. these are syntax to a dns server. as a programmer we know to program we need to know the syntax of a programming languages like that dns has a foundation with those syntax’s.
Query types and DNS records
query types are ways used to send and receive DNS records. DNS query types define how requests are made to DNS servers and how responses are returned. The three main query types are
- Recursive — In a recursive query, the DNS server takes full responsibility for resolving the domain name and returning the final answer, contacting other DNS servers if necessary.
- Iterative — In an iterative query, the DNS server responds with the best information it has, such as a referral to another DNS server, leaving the client to follow up.
- Non-recursive. — A non-recursive query is used when the DNS server already has the requested information in its cache and can respond immediately.
DNS records are data entries in the Domain Name System that store information about domains and their associated resources. These records enable the mapping of domain names to IP addresses and provide other essential details for internet functionality. The six main dns records are
- A records — map a domain to an IPv4 address.
- AAAA records — map a domain to an IPv6 address.
- CNAME records — used to create domain aliases. for example www.bego.com -> bego.com
- MX records — mail servers for email routing
(domain: bego.com; type: MX; piority:10; value: mail.bego.com;(lower the number higher the piority)
- TXT records — allow custom data to be associated with a domain.
- NS records — authoritative DNS servers for a domain.
Coding….
import socket
from dnslib import DNSRecord, QTYPE, RR, A
DNS_HOST = "127.0.0.1"
DNS_PORT = 53
FORWARDER = "8.8.8.8"
DOMAIN_MAP = {"Bego.com": "10.10.205.11"}
def handle_query(data, addr, sock):
query = DNSRecord.parse(data)
response = query.reply()
for question in query.questions:
qname = str(question.qname)
qtype = QTYPE[question.qtype]
print(f"Query for {qname}, Type: {qtype}")
if qname in DOMAIN_MAP and qtype == "A":
response.add_answer(RR(qname, QTYPE.A, ttl=60, rdata=A(DOMAIN_MAP[qname])))
else:
# Forward unknown queries
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as forward_sock:
forward_sock.sendto(data, (FORWARDER, DNS_PORT))
forward_data, _ = forward_sock.recvfrom(512)
sock.sendto(forward_data, addr)
return
sock.sendto(response.pack(), addr)
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.bind((DNS_HOST, DNS_PORT))
print(f"DNS Server started on {DNS_HOST}:{DNS_PORT}")
while True:
data, addr = sock.recvfrom(512)
handle_query(data, addr, sock)
How to use
After deploying the above code to a server, it will function as your custom DNS server. However, even though it is operational, you might not notice any effect because your home router is still configured to use external DNS servers, such as Cloudflare or Google. To make your custom DNS server work as expected, log in to your home router and change its DNS server settings to point to the IP address of your newly created DNS server. Once this is done, your custom DNS server will handle DNS queries. If a domain name is not found, your server can be configured to forward the query to an external DNS server, such as Google’s 8.8.8.8.