萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Linux 能 ping IP 不能 ping 域名?/etc/nsswitch.conf 解析順序配置錯誤

經典網路故障: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 域名」疑難雜症。

copyright © 萬盛學電腦網 all rights reserved