修复Linux DNS

下面是一份完整的 Ubuntu 22.04(ifupdown)静态 IPv4 + IPv6 + DNS 配置示例。

# /etc/network/interfaces

# 本地回环接口
auto lo
iface lo inet loopback

# 主网卡
auto ens3

# IPv4 配置
iface ens3 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 1.1.1.1 8.8.8.8

# IPv6 配置
iface ens3 inet6 static
    address 2001:db8:1000:1::100
    netmask 64
    gateway 2001:db8:1000:1::1
    dns-nameservers 2606:4700:4700::1111 2001:4860:4860::8888

手动写入 DNS

查看是否为软链接:

ls -l /etc/resolv.conf

如果是软链接:

rm -f /etc/resolv.conf

创建 DNS 配置:

cat >/etc/resolv.conf <<EOF
nameserver 1.1.1.1
nameserver 8.8.8.8
nameserver 2606:4700:4700::1111
nameserver 2001:4860:4860::8888
EOF

验证:

cat /etc/resolv.conf

输出应为:

nameserver 1.1.1.1
nameserver 8.8.8.8
nameserver 2606:4700:4700::1111
nameserver 2001:4860:4860::8888

重载网络

systemctl restart networking

如果远程 SSH 管理服务器,不建议执行:

ifdown ens3 && ifup ens3

容易导致连接中断。


测试 IPv4

ping -c 4 1.1.1.1
ping -c 4 google.com

测试 IPv6

ping6 -c 4 2606:4700:4700::1111
ping6 -c 4 google.com

测试 DNS

getent hosts google.com

nslookup google.com

测试 APT

apt clean
rm -rf /var/lib/apt/lists/*
apt update

常用公共 DNS

服务商 IPv4 IPv6
Cloudflare 1.1.1.1 / 1.0.0.1 2606:4700:4700::1111 / 2606:4700:4700::1001
Google 8.8.8.8 / 8.8.4.4 2001:4860:4860::8888 / 2001:4860:4860::8844
Quad9 9.9.9.9 2620:fe::fe

实际部署时请将示例地址:

192.168.1.100
192.168.1.1
2001:db8:1000:1::100
2001:db8:1000:1::1

替换为你的 VPS 服务商提供的真实 IP、网关和 IPv6 网关。2001:db8::/32 是 RFC 文档专用地址,专门用于示例,不能在公网使用。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享