LabHub
学习 学习路径 课程

Linux 网络诊断

getent 和 dig 走的是两条路

在 LabHub 中继续学习

一句话总结

getent 经过名称服务交换机制,dig 则直接查询 DNS;两者结果不同,通常说明问题位于应用的 stub resolver 路径。

概念图: 一句话总结 · 解析路径 · 原文命令与标识符

解析路径

应用通常通过 getaddrinfo,按 nsswitch 配置依次查询 hosts 文件、DNS 或本地解析服务;dig 会绕过其中大部分步骤。完整路径还包括 resolv.conf、递归缓存、根与 TLD 委派以及权威服务器。

应逐段提问:用 getent 模拟应用结果,用 dig 查询选定解析器,用 norecurse 检查缓存,用 trace 追踪委派。resolv.conf 中 nameserver、timeout、attempts、ndots 与 search 会直接决定延迟;外部完整域名加末尾点可避免多余搜索。

DNS 变更并非主动传播,而是等待 TTL 到期;NXDOMAIN 与无该类型记录不同,否定答案也会被缓存。

原文命令与标识符

hosts: files mdns4_minimal [NOTFOUND=return] dns
getent hosts api.example.com      # nsswitch 를 그대로 따른다
getent ahostsv4 api.example.com   # IPv4 만
resolvectl status
resolvectl query api.example.com

getent

dig

dig api.example.com

getaddrinfo()

/etc/nsswitch.conf

hosts:

files dns

/etc/hosts

/etc/resolv.conf

/etc/hosts

/etc/hosts

/etc/nsswitch.conf

/etc/resolv.conf

getent

dig

dig @서버 +norecurse

dig +trace

/etc/resolv.conf

nameserver

options timeout:n

options attempts:n

options ndots:n

ndots

NOERROR

SERVFAIL

REFUSED

aa

dig

dig

getaddrinfo()

/etc/nsswitch.conf

files

/etc/hosts

mdns4_minimal

[NOTFOUND=return]

.local

getent

/etc/resolv.conf

search

ndots:5

api.example.com

api.example.com.

/etc/resolv.conf

127.0.0.53

networkaddress.cache.ttl

/etc/resolv.conf

/etc/nsswitch.conf

/etc/hosts