ubuntu18.04 dns异常修复

明白 ✅。在 Ubuntu 18.04 中，DNS 异常问题比较常见，原因通常出在 systemd-resolved、配置文件错误、或者本地缓存问题。下面我给你一个 完整的排查 + 修复步骤：
🔎 常见问题表现
ping www.baidu.com 报错：
Temporary failure in name resolution
能 ping 通 IP，但不能解析域名
/etc/resolv.conf 内容异常（指向 
127.0.0.53 但没解析）
🛠 修复步骤
1. 检查 systemd-resolved 服务
systemctl status systemd-resolved
如果未运行：
sudo systemctl enable systemd-resolved --now
2. 检查 resolv.conf
Ubuntu 18.04 默认 
/etc/resolv.conf 是指向 
/run/systemd/resolve/stub-resolv.conf 的符号链接。
先确认：
ls -l /etc/resolv.conf
如果不是符号链接，可能被修改了，可以恢复：
sudo rm -f /etc/resolv.conf
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
3. 手动设置 DNS 服务器
修改 systemd-resolved 配置：
sudo nano /etc/systemd/resolved.conf
添加或修改以下内容（推荐使用 Google + Cloudflare + AliDNS）：
[Resolve]
DNS=8.8.8.8 1.1.1.1 223.5.5.5
FallbackDNS=8.8.4.4 1.0.0.1 114.114.114.114
保存后重启服务：
sudo systemctl restart systemd-resolved
4. 如果你用 Netplan（18.04 默认网络管理）
编辑配置文件：
sudo nano /etc/netplan/01-netcfg.yaml
示例配置：
network:
  version: 2
  ethernets:
    ens33:   # 替换为你的网卡名（ip a 可查）
      dhcp4: yes
      nameservers:
        addresses: [8.8.8.8, 223.5.5.5]
应用配置：
sudo netplan apply
5. 清理 DNS 缓存
sudo systemd-resolve --flush-caches
然后测试：
dig www.baidu.com
nslookup www.google.com
🚑 快速一键修复脚本
如果想要简单点，可以用下面脚本：
#!/bin/bash
echo ">>> 修复 Ubuntu 18.04 DNS 异常中..."
# 1. 确保 systemd-resolved 运行
systemctl enable systemd-resolved --now
# 2. 恢复 resolv.conf 符号链接
rm -f /etc/resolv.conf
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
# 3. 修改 systemd-resolved 配置
cat >/etc/systemd/resolved.conf <<EOF
[Resolve]
DNS=8.8.8.8 1.1.1.1 223.5.5.5
FallbackDNS=8.8.4.4 1.0.0.1 114.114.114.114
EOF
# 4. 重启服务
systemctl restart systemd-resolved
# 5. 刷新缓存
systemd-resolve --flush-caches
echo ">>> 修复完成，请测试网络连接"
保存为 
fix-dns.sh，执行：
chmod +x fix-dns.sh
sudo ./fix-dns.sh
要不要我帮你写一个 检测并自动修复 Ubuntu18.04 DNS 的脚本（比如检测解析是否失败，如果失败就自动重置配置）？