查看DNS
首先确认你的DNS有谁在管理:
ls -l /etc/resolv.conf
-> /run/systemd/resolve/stub-resolv.conf:这表示系统正在使用 systemd-resolved 服务来管理DNS。
-> /run/resolvconf/resolv.conf:这表示系统正在使用传统的 resolvconf 工具来管理DNS。
查看resolv.conf文件,这个方法无论是哪种工具在管理DNS都可以进行查看。
cat /etc/resolv.conf
或者使用resolvectl命令,这种方法仅使用systemd-resolved来管理DNS才可以查看。如果报错,则是你没有systemd-resolved服务。
resolvectl status
修改DNS
对于使用 systemd-resolved
nano /etc/systemd/resolved.conf
其中DNS=为主DNS服务器,FallbackDNS=为备用DNS服务器,示例如图。
[Resolve]
DNS=8.8.8.8 1.1.1.1
FallbackDNS=8.8.4.4
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#DNSOverTLS=no
#Cache=no-negative
#ReadEtcHosts=yes
重启服务使配置生效
systemctl restart systemd-resolved.service
对于使用 resolvconf
1、确定你的公网网卡名字
ip addr
你大概会看到类似的输出:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
... (lo 是本地回环,忽略)
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
...
其中eth0就是你的网卡名。
开始修改/etc/network/interfaces文件
nano /etc/network/interfaces
可能看到两种,
一种是静态IP配置
auto eth0
iface eth0 inet static
address 123.45.67.89
netmask 255.255.255.0
gateway 123.45.67.1
一种是dhcp
# The primary network interface
auto eth0
iface eth0 inet dhcp
2.1、对于 static 修改DNS
在你刚才确认的网卡名(这里是 eth0 )那里的底下增加dns-nameservers,这里以Google和Cloudflare为例。多个DNS地址用空格隔开。
auto eth0
iface eth0 inet static
address 123.45.67.89
netmask 255.255.255.0
gateway 123.45.67.1
dns-nameservers 8.8.8.8 1.1.1.1
2.2、对于 dhcp 修改DNS
如果是dhcp,这会复杂一点,不推荐像下面的那样直接修改,因为这样可能会产生冲突!
auto eth0
iface eth0 inet dhcp
dns-nameservers 8.8.8.8 1.1.1.1
你应该去修改/etc/dhcp/dhclient.conf文件
nano /etc/dhcp/dhclient.conf
在文件的最后增加:
supersede domain-name-servers 8.8.8.8, 8.8.4.4;
supersede这是告诉dhclient:“不管DHCP服务器返回什么DNS服务器,你都把它们替换成我写的这两个。”
3、重启网络服务
systemctl restart networking.service
或者重启网卡,根据你的网卡名字,这种方式有可能会导致SSH连接中断!
sudo ifdown eth0 && sudo ifup eth0
4、可能遇到的额外问题
可能的额外的一些问题,有时候重启网卡后旧的DNS记录还存在/etc/resolv.conf里。这时候我们查看这个文件会发现可能内容变多了,而不是被修改了。
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "resolvectl status" to see details about the actual nameservers.
nameserver 114.114.114.114
nameserver 8.8.8.8
nameserver 1.1.1.1
search local
虽然这个文件上提示我们可以使用resolvectl status来查看具体的详情,但现在的情况是使用resolveconf在管理DNS,而不是systemd-resolved和resolveconf混合管理。我们先查看DNS信息的来源
ls -l /run/resolvconf/interface/
大概会看到这样的内容,或者是更多的内容,比如还有lo.inet。然后我们使用cat命令逐个查看看是哪个的问题。
-rw-r--r-- 1 root root 51 Nov 13 11:54 eth0.inet
我这里发现的是lo提供了114的DNS地址,所以我们将这个文件直接删除掉。请注意这个文件有可能是脚本等文件加载的,所以请确认它的来源。这里我确认其来源是运营商的初始安装脚本,所以可以直接移除。
rm /run/resolvconf/interface/lo # 假设lo是来源
清理完后我们还需要更新配置:
resolvconf -u
最后再检查一次,确认修改完毕。
cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "resolvectl status" to see details about the actual nameservers.
nameserver 8.8.8.8
nameserver 1.1.1.1
search local
临时修改DNS
如果想测试一下的话,可以手动修改我们之前查看的/etc/resolv.conf文件。这种方法一旦重启或者网络服务发生变化就会被重新覆盖,仅在排查问题和测试的时候使用。
sudo nano /etc/resolv.conf
清空或者修改成你想要的DNS
nameserver 8.8.8.8
nameserver 1.1.1.1
文章封面(AI):