常见原因:DNS服务器配置错误(如/etc/resolv.conf中nameserver无效)、网络连接中断(无法访问DNS服务器)、/etc/hosts文件配置错误(手动解析记录有误)、DNS缓存包含过期或错误结果。
解决方法:
cat /etc/resolv.conf确认nameserver是否为有效地址(如8.8.8.8、114.114.114.114);若使用systemd-resolved,编辑/etc/systemd/resolved.conf添加DNS=8.8.8.8,然后重启服务:sudo systemctl restart systemd-resolved。ping 8.8.8.8,若不通则检查网络接口(ip a)、路由(ip route)或防火墙设置。/etc/hosts文件:确保无错误的域名映射(如127.0.0.1 example.com会覆盖DNS解析)。sudo systemd-resolve --flush-caches(或sudo resolvectl flush-caches)。常见原因:DNS服务器响应慢(如公共DNS拥堵)、DNS缓存未更新(旧记录导致重复查询)、解析顺序不合理(优先查询慢速DNS)。
解决方法:
/etc/resolv.conf(临时)或/etc/systemd/resolved.conf(永久),添加国内公共DNS(如114.114.114.114、223.5.5.5),并设置options timeout:1 attempts:2(缩短超时时间)。sudo systemd-resolve --flush-caches,避免旧记录影响解析速度。/etc/nsswitch.conf中的hosts行,确保为hosts: files dns(优先查询本地/etc/hosts文件,再查询DNS)。常见原因:未正确应用netplan配置(Ubuntu 20.04+默认使用netplan)、/etc/resolv.conf被systemd-resolved覆盖、NetworkManager未同步配置。
解决方法:
/etc/netplan/*.yaml(如00-installer-config.yaml),在对应网卡下添加nameservers: {addresses: ["8.8.8.8", "114.114.114.114"]},然后执行sudo netplan apply。nmcli connection show查看连接名称,执行nmcli connection up <连接名称>重新激活连接。systemctl status systemd-resolved,确保服务运行正常;若未运行,执行sudo systemctl enable --now systemd-resolved。常见原因:本地DNS缓存未及时更新(如修改DNS记录后仍返回旧IP)、缓存服务异常(如systemd-resolved缓存损坏)。
解决方法:
sudo systemd-resolve --flush-caches(Ubuntu 22.04+)或sudo service systemd-resolved restart(旧版本)。nslookup或dig命令重新查询域名(如nslookup example.com),验证是否返回最新结果。/etc/systemd/resolved.conf,添加Cache=no,然后重启服务。常见原因:内网DNS服务器响应慢、DNS查询循环(如内网DNS转发到公网DNS导致延迟)、网络路径丢包(如DNS服务器与客户端之间网络不稳定)。
解决方法:
/etc/hosts文件,添加<内网IP> <域名>(如10.14.1.30 nx-cpu030.deepscience.cn),优先使用本地解析。/etc/resolv.conf,将内网DNS放在首位(如nameserver 10.14.1.1),备用公网DNS(如223.5.5.5)。mtr -n <DNS服务器IP>跟踪路径,检查是否有丢包(如Loss%列不为0),若有丢包需联系网络管理员修复。常见原因:Ubuntu防火墙(ufw/iptables)默认禁止UDP 53端口(DNS查询端口),导致DNS请求无法发送到服务器。
解决方法:
sudo ufw status,若显示active,则允许DNS端口:sudo ufw allow 53/udp。sudo iptables -L -v -n | grep 53,若存在DROP规则,添加允许规则:sudo iptables -A INPUT -p udp --dport 53 -j ACCEPT(需持久化规则,如使用iptables-save)。