Sub topic 8
DNS: port 53 over UDP and TCP
This chapter assumes you know what an IP address is, how a client talks to a server, and how to run commands in a terminal. It also assumes you can read a short Python script. You should already know the difference between TCP and UDP from the earlier chapter.
What DNS does
The Domain Name System (DNS) turns names such as example.com into IP addresses. A client sends a query. A DNS server sends back an answer. Servers listen on port 53, and the same number is used for both UDP and TCP.
Why DNS uses both UDP and TCP
Most queries are small. One question and one answer fit in a single datagram. UDP suits this well, because there is no handshake and the server keeps no connection state. A lookup costs one packet each way.
TCP on port 53 is used when UDP is not enough:
- Large responses. The original limit for a UDP answer was 512 bytes. If the server cannot fit the answer, it sets the truncated (TC) flag. The client then repeats the query over TCP.
- Zone transfers. A secondary server copies a whole zone from a primary server (AXFR). This is bulk data and needs reliable delivery, so it uses TCP.
- Policy and newer features. Some resolvers use TCP for DNSSEC-sized answers or for privacy. Encrypted DNS uses other ports, such as 853 for DNS over TLS.
Seeing port 53 in practice
The client uses an ephemeral port as its source and port 53 as the destination. The dig tool lets you choose the transport:
# Normal query: UDP by default
dig example.com A
# Force TCP
dig +tcp example.com A
# Show the full response size and server details
dig example.com DNSKEY +dnssecThe Python standard library can look up the port number by service name:
import socket
print(socket.getservbyname("domain", "udp"))
print(socket.getservbyname("domain", "tcp"))Both lines print 53. The service is named domain in the services database, not dns.
Common errors
- Firewall allows only UDP 53. Small lookups work, but large answers and zone transfers fail or time out. Allow both UDP and TCP on port 53 where DNS is needed.
- Blocking TCP 53 "for safety". This breaks truncation fallback. Restrict zone transfers by server configuration or by source address instead.
- Exposing a resolver to the whole internet. An open resolver can be abused in amplification attacks, where a small spoofed query produces a large answer sent to a victim. Limit recursion to your own clients.
- Confusing the two server roles. A recursive resolver answers clients and queries other servers for them. An authoritative server holds the records for a zone. Both use port 53.
Exercise
A client can resolve short names, but a lookup for a domain with a very large record set times out. The firewall allows UDP 53 and blocks TCP 53. Explain why, and give the fix.
Show answer
The large answer does not fit in a UDP response, so the server sets the TC flag. The client retries over TCP port 53, which the firewall blocks, so the lookup times out. The fix is to allow TCP 53 between the client or resolver and the DNS server.
Exercise
Which transport does a zone transfer use, and why?
Show answer
TCP on port 53. A zone transfer moves a whole zone as bulk data, so it needs the reliable, ordered delivery that TCP provides.