Sub topic 15
Reverse zones and PTR records
A forward lookup maps a name to an address. A reverse lookup maps an address back to a name, using a PTR record. This chapter assumes you can already write a BIND zone file, serve it as primary, and test with dig. Those steps are covered in the earlier chapters.
How reverse lookups work
DNS can only look names up in a tree, and the tree is organised by name, not by address. To make addresses searchable, the resolver turns an IPv4 address into a name under the special domain in-addr.arpa. It reverses the octets, because an address gets more specific from left to right while a DNS name gets more specific from right to left.
The address 192.0.2.20 becomes 20.2.0.192.in-addr.arpa. The zone that holds PTR records for the whole 192.0.2.0/24 network is 2.0.192.in-addr.arpa.
The chain shows that your reverse zone only works if the owner of the address block delegates it to your server. For provider-assigned addresses, you usually ask the provider to delegate the zone or to create PTR records for you. Running the zone on your own server does nothing until that delegation exists. Inside a private network or a lab, you can skip delegation and point your own resolvers at the server.
Writing the reverse zone file
A reverse zone file looks like a forward zone file. It has the same SOA and NS records, but the other records are PTR records. The owner name of each PTR record is the part of the address that is not already in the zone name. For 2.0.192.in-addr.arpa. that is only the last octet.
$TTL 3600
@ IN SOA ns1.example.com. hostmaster.example.com. (
2026112801 ; serial
7200 ; refresh
3600 ; retry
1209600 ; expire
3600 ) ; negative caching TTL
IN NS ns1.example.com.
IN NS ns2.example.com.
10 IN PTR ns1.example.com.
11 IN PTR ns2.example.com.
20 IN PTR www.example.com.
25 IN PTR mail.example.com.The target of a PTR record is a full name, so it needs a trailing dot. Without the dot, BIND appends the zone name and you get www.example.com.2.0.192.in-addr.arpa., which is a common mistake.
Declare the zone in named.conf the same way as a forward zone:
zone "2.0.192.in-addr.arpa" {
type primary;
file "/etc/bind/db.192.0.2";
allow-transfer { 192.0.2.11; };
};Check the file and reload. Then query by address with -x, which builds the reversed name for you:
sudo named-checkzone 2.0.192.in-addr.arpa /etc/bind/db.192.0.2
sudo rndc reload 2.0.192.in-addr.arpa
dig @127.0.0.1 -x 192.0.2.20 +short
dig @127.0.0.1 20.2.0.192.in-addr.arpa PTR +norecurseBoth queries should return www.example.com. The second form shows the real name being queried, which helps when you debug.
Keeping forward and reverse records in step
DNS does not link a PTR record to the matching A record. They live in different zones and nothing checks that they agree. You must keep them consistent yourself.
Many services run a forward-confirmed reverse DNS check. They look up the PTR for the client address, look up the A record for the name that came back, and require the original address to be in the result. Mail servers use this check often, and some SSH and logging setups use it too. A mismatch can cause rejected mail or slow logins.
Follow these rules:
- Give every address that connects out to other systems exactly one PTR record, and make sure the target name has an A record pointing back at it.
- Use more than one PTR record on one address only when you have a reason. Large answers can break some software.
- Raise the SOA serial every time you change either zone, or secondaries keep serving old data.
IPv6 reverse zones
IPv6 uses the ip6.arpa domain. The name is built from every hexadecimal digit (nibble) of the fully expanded address, reversed, with a dot between each digit. The address 2001:db8::1 expands to 32 digits, and its PTR owner name has 32 labels. You rarely write that by hand. Let dig build it:
dig @127.0.0.1 -x 2001:db8::1A zone for a whole 2001:db8::/32 prefix is named 8.b.d.0.1.0.0.2.ip6.arpa. The prefix length must fall on a nibble boundary, so use /32, /48, /56 or /64 for zone names. Zone files stay readable if you use $ORIGIN lines or generate them from your address-management data.
Classless delegation
An ISP may give you less than a /24, for example a /26. The reverse zone for the /24 belongs to the ISP, so they cannot hand you the zone. RFC 2317 describes a workaround. The ISP adds a CNAME for each of your addresses, pointing into a zone with a made-up name such as 0-63.2.0.192.in-addr.arpa., and delegates that zone to you. You then serve PTR records there. If your provider uses this scheme, they will give you the exact zone name. Use that name in named.conf and in the zone file.
Reverse data in Unbound
Unbound can answer PTR queries for local data without a full reverse zone. The local-data-ptr option, shown in the earlier Unbound chapter, creates the PTR record from an address and a name:
server:
local-zone: "lab.example." static
local-data: "nas.lab.example. IN A 192.0.2.20"
local-data-ptr: "192.0.2.20 nas.lab.example."Unbound is a good fit for a small internal network where you want names and reverse names but no secondaries and no zone transfers. For a delegated reverse zone with transfers, use BIND.
Common problems
| Symptom | Likely cause |
|---|---|
| `dig -x` returns NXDOMAIN from your server | The zone name is wrong (octets not reversed) or the PTR owner name is wrong |
| Names look like `www.example.com.2.0.192.in-addr.arpa.` | Missing trailing dot on the PTR target |
| Works on your server, fails from the internet | The block owner has not delegated the reverse zone to you |
| Secondary shows old PTR data | The serial was not increased, or NOTIFY and transfers are blocked |
| Mail is rejected for a failed reverse check | The PTR name has no A record that points back to the same address |
Exercise
You host the network 198.51.100.0/24. Give the reverse zone name, and the owner name of the PTR record for 198.51.100.77 inside that zone file. Then say what the target host.example.net would become if you left off the trailing dot, in a zone file that has no $ORIGIN override.
Show answer
The zone name is 100.51.198.in-addr.arpa. The owner name in the zone file is 77. Without the trailing dot, the target host.example.net is taken as relative, so BIND appends the zone name and the record points to host.example.net.100.51.198.in-addr.arpa.
Exercise
A mail server at 192.0.2.25 has a PTR record for mail.example.com. The A record for mail.example.com. points to 192.0.2.26. What happens during a forward-confirmed reverse DNS check, and how do you fix it?
Show answer
The check looks up the PTR, gets mail.example.com., looks up its A record, and finds 192.0.2.26, which does not match the connecting address 192.0.2.25, so the check fails. Fix it by making the A record and the PTR agree, either by changing the A record to 192.0.2.25 or by moving the PTR to the 26 owner name. Then raise the serial in each zone you changed.