Sub topic 11
Reverse DNS and PTR Records
This chapter assumes you know IPv4 and IPv6 address notation, the idea of a DNS zone and delegation, and the record types A, AAAA, NS and SOA. You should also be comfortable running dig in a terminal.
What reverse DNS does
Forward DNS maps a name to an address. Reverse DNS maps an address back to a name. The record that holds the answer is the PTR (pointer) record.
Reverse lookups are used for:
- Log readability. Tools such as
traceroute,mtrand many log viewers show a name next to each address. - Mail reputation. Many mail servers check that the connecting IP has a PTR record, and that the name in it resolves back to the same IP.
- Diagnostics and access policies. Some older services still use reverse names for allow lists. This is weak practice, because the owner of an IP range controls its PTR records and can put any name in them.
DNS is built to look up by name, and the tree is organised by name. An address cannot be searched for efficiently in that tree. So reverse DNS reuses the same tree by placing addresses under a special domain.
The in-addr.arpa tree for IPv4
IPv4 reverse names live under in-addr.arpa. To build the name, write the four octets in reverse order and append in-addr.arpa.
For 192.0.2.10 the reverse name is:
10.2.0.192.in-addr.arpa.The octets are reversed because domain names get more specific from right to left, while IP addresses get more specific from left to right. Reversing the octets makes the most general part of the address (the network prefix) sit nearest the root. That lets an address block be delegated just like a domain:
in-addr.arpa.is delegated to the regional internet registries and then to the organisations that hold address blocks.192.in-addr.arpa.covers everything that begins with 192.2.0.192.in-addr.arpa.covers the192.0.2.0/24network.- The final label,
10, is the host.
A PTR record in the zone 2.0.192.in-addr.arpa. looks like this:
$ORIGIN 2.0.192.in-addr.arpa.
$TTL 3600
@ IN SOA ns1.example.com. hostmaster.example.com. (
2026112701 ; serial
7200 ; refresh
3600 ; retry
1209600 ; expire
300 ) ; negative caching TTL
IN NS ns1.example.com.
IN NS ns2.example.com.
10 IN PTR example.com.
25 IN PTR mail.example.com.The data of a PTR record is a domain name. Because the zone file sets $ORIGIN, the owner 10 expands to 10.2.0.192.in-addr.arpa.. The names on the right end with a dot, so they are absolute. If you leave the dot off, the zone origin is appended, and you get a name like mail.example.com.2.0.192.in-addr.arpa.. This is a very common mistake.
The ip6.arpa tree for IPv6
IPv6 reverse names live under ip6.arpa. The rule is the same idea, applied to nibbles. A nibble is one hexadecimal digit, which is 4 bits. To build the name:
- Write the address in full, with no
::shortening and with leading zeros restored. - Remove the colons, which leaves 32 hexadecimal digits.
- Reverse the order of the digits.
- Put a dot between each digit and append
ip6.arpa.
For 2001:db8::10:
- Expanded:
2001:0db8:0000:0000:0000:0000:0000:0010 - Digits:
20010db8000000000000000000000010 - Reversed:
01000000000000000000000008bd1002 - Result:
0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.
Doing this by hand is error-prone, so use a tool. This Python function does it with the standard library:
import ipaddress
for text in ["192.0.2.10", "2001:db8::10"]:
ip = ipaddress.ip_address(text)
print(text, "->", ip.reverse_pointer)reverse_pointer returns the name without a trailing dot. The output is 10.2.0.192.in-addr.arpa for the first address and the 32-nibble name for the second.
Because each nibble is its own label, an IPv6 block can be delegated at any 4-bit boundary. An organisation with a /48 receives a zone that is 12 nibbles long, for example 8.b.d.0.1.0.0.2.ip6.arpa. for 2001:db8::/32.
Querying with dig
The -x option builds the reverse name for you and asks for the PTR type:
dig -x 192.0.2.10 +noall +answer
dig -x 2001:db8::10 +noall +answerA successful answer looks like this:
10.2.0.192.in-addr.arpa. 3600 IN PTR example.com.The fields are the owner name, the remaining TTL, the class, the type and the target name. You can run the same query without -x, which shows that nothing special is happening:
dig 10.2.0.192.in-addr.arpa. PTR +noall +answerTo see who is responsible for a reverse zone, ask for its NS records:
dig 2.0.192.in-addr.arpa. NS +noall +answerThe addresses 192.0.2.0/24 and 2001:db8::/32 are reserved for documentation. Real queries for them will normally return NXDOMAIN or answers from special-purpose servers. Use your own addresses to see real records.
Who controls the reverse zone
The forward zone example.com is managed by whoever owns the domain. The reverse zone is managed by whoever holds the IP block, and that is usually a different party.
- If you rent a server, the hosting provider holds the block. You change PTR records through its control panel or a support request.
- If you hold a large block, the registry delegates the reverse zone to your name servers, and you publish PTR records yourself.
- If you hold a block smaller than a
/24, the registry cannot delegate a single zone cleanly, because the zone boundary falls on an octet. The common solution is classless delegation (RFC 2317). The provider adds CNAME records in the parent zone that point each address to a name inside a zone you control.
A sketch of a classless delegation for 192.0.2.128/25, written in the provider's 2.0.192.in-addr.arpa. zone:
$ORIGIN 2.0.192.in-addr.arpa.
128/25 IN NS ns1.customer.example.
129 IN CNAME 129.128/25.2.0.192.in-addr.arpa.
130 IN CNAME 130.128/25.2.0.192.in-addr.arpa.The customer then runs a zone called 128/25.2.0.192.in-addr.arpa. that holds the PTR records. The / is allowed in a label by this convention, although some software uses - instead.
Forward-confirmed reverse DNS
A PTR record is only a claim. Anyone who controls the reverse zone can say that their address belongs to www.bank.example. To make the claim meaningful, a service performs forward-confirmed reverse DNS (FCrDNS):
- Look up the PTR record for the client address and get a name.
- Look up the A or AAAA records for that name.
- Accept the result only if the original address is among them.
The next script performs the check with the system resolver:
import socket
def fcrdns(ip):
try:
name, _, _ = socket.gethostbyaddr(ip)
except socket.herror:
return None, False
try:
infos = socket.getaddrinfo(name, None)
except socket.gaierror:
return name, False
addresses = {info[4][0] for info in infos}
return name, ip in addresses
print(fcrdns("192.0.2.10"))For the documentation address the PTR lookup will normally fail, so the function returns (None, False). Run it with a public address you control to see (name, True).
Common errors
| Symptom | Likely cause |
|---|---|
| `NXDOMAIN` for a reverse name | The block is not delegated to any server, or the PTR record does not exist. |
| `SERVFAIL` | The delegated name servers are unreachable or the zone is broken. |
| PTR name does not resolve back to the IP | The forward A or AAAA record is missing or points elsewhere. |
| PTR target looks like `mail.example.com.2.0.192.in-addr.arpa.` | The target name in the zone file has no trailing dot. |
| Multiple PTR names for one address | Legal, but many programs read only the first. Publish a single PTR for mail servers. |
| Mail is rejected for "no reverse DNS" | The sending IP has no PTR, or the PTR does not match the name the server uses in its SMTP greeting. |
Reverse DNS and caching
PTR records are cached like any other record. The TTL controls how long a resolver keeps them. A missing PTR is cached as a negative answer for the time set by the SOA negative caching field of the reverse zone. If you add a PTR record and a test still fails, a cached NXDOMAIN is often the reason. Query the authoritative server directly to check the real state:
dig @ns1.example.com -x 192.0.2.10 +noall +answer +authorityExercise
What is the reverse DNS name for the IPv4 address 203.0.113.7, and why are the octets reversed? Who can usually change its PTR record for a rented server?
Show answer
The name is 7.113.0.203.in-addr.arpa. The octets are reversed because DNS names are more specific on the left and more general on the right, while an IP address is more general on the left. Reversing puts the network prefix next to the root, so address blocks can be delegated like domains. The PTR record is controlled by the holder of the IP block, which for a rented server is normally the hosting provider, not the owner of the forward domain.
Exercise
A mail server at 192.0.2.25 has the PTR record "mail.example.com." but the A record for mail.example.com points to 192.0.2.99. Does the address pass forward-confirmed reverse DNS? What should you change?
Show answer
No. The PTR name resolves to 192.0.2.99, which does not include the original address 192.0.2.25, so the check fails. Change the A record of mail.example.com to 192.0.2.25 if that host is the mail server, or change the PTR to a name whose A record is 192.0.2.25.