在 dnsmasq 中配置 DNS 反向解析(即 PTR 记录,将 IP 地址解析为域名)需要编辑 dnsmasq 的配置文件,并添加相应的反向解析规则。以下是详细步骤:
dnsmasq 默认支持正向(A/AAAA)和反向(PTR)解析,但需要正确配置 /etc/dnsmasq.conf 和 /etc/hosts 或自定义数据文件。
/etc/hosts 文件配置(简单方式)dnsmasq 默认会读取 /etc/hosts 文件,并自动为其生成正向(A)和反向(PTR)解析记录。
/etc/hosts 文件:sudo nano /etc/hosts192.168.1.10 对应 example.com):192.168.1.10 example.comexample.com。/etc/dnsmasq.conf 直接配置 PTR 记录如果不想依赖 /etc/hosts,可以在 dnsmasq.conf 中直接定义 PTR 记录。
/etc/dnsmasq.conf:
sudo nano /etc/dnsmasq.confptr-record=<反向域名>,<IP>):
ptr-record=10.1.168.192.in-addr.arpa,example.com
10.1.168.192.in-addr.arpa 是 192.168.1.10 的反向域名格式(IPv4 反向解析格式:<最后一段>.<第三段>.<第二段>.<第一段>.in-addr.arpa)。example.com 是要解析的域名。如果反向解析记录较多,可以创建一个单独的文件(如 /etc/dnsmasq.d/ptr.conf),然后在 dnsmasq.conf 中引用它。
/etc/dnsmasq.d/ptr.conf:sudo nano /etc/dnsmasq.d/ptr.confdnsmasq.conf 包含该文件:sudo nano /etc/dnsmasq.conf
添加:conf-dir=/etc/dnsmasq.d/,*.conf如果需要配置 IPv6 的反向解析(ip6.arpa 格式),方法类似:
ptr-record=0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa,ipv6.example.com(::1 的 IPv6 反向解析格式)
使用 dig 或 nslookup 验证反向解析是否生效:
dig -x 192.168.1.10或
nslookup 192.168.1.10预期输出:
10.1.168.192.in-addr.arpa. 0 IN PTR example.com.