經典網路故障:ping 192.168.1.1 正常,但 ping www.example.com 提示 unknown host。根因在於 /etc/nsswitch.conf 中 hosts 解析順序未包含 dns。
[root@itmop ~]# ping www.downcc.com
ping: unknown host www.downcc.com
[root@itmop ~]# ping 192.168.1.1
PING 192.168.1.1 ... 正常回應
[root@itmop ~]# cat /etc/host.conf
order bind,hosts # 此檔較舊,已被 nsswitch.conf 取代
[root@itmop ~]# grep hosts /etc/nsswitch.conf
hosts: files # 只有 files,缺少 dns!
vi /etc/nsswitch.conf
# 找到 hosts 行,修改為:
hosts: files dns
# 存檔離開,即時生效,無需重啟服務
[root@itmop ~]# ping www.downcc.com
PING www.downcc.com (x.x.x.x) 56(84) bytes of data.
64 bytes from ... 正常解析回應
/etc/nsswitch.conf 控制 Name Service Switch 解析順序,files 為 /etc/hosts,dns 為向 /etc/resolv.conf 指定的 DNS 伺服器查詢hosts: files dns myhostname(systemd-resolved 環境)/etc/host.conf 為舊版 libc 相容保留,現代系統以 nsswitch.conf 為準/etc/resolv.conf nameserver 設定及防火牆 UDP 53 端口一行配置修正,恢復域名解析,排除「能 ping IP 不能 ping 域名」疑難雜症。