抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

受博友圈夏目同学所托,帮其新购置的云服务器搭建网站运行环境。几个 apt 的问题,想都没想就答应了,却未料到一开始就出现问题,Ubuntu 的 DNS 不可用,今天说下如何解决这个问题。

问题

接手新系统后,习惯性第一时间更新软件源,却发现执行失败了。看返回的错误提示,判断应该是域名解析的问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@dusays:~# apt update
Err:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:2 http://security.ubuntu.com/ubuntu bionic-security InRelease
Temporary failure resolving 'security.ubuntu.com'
Err:3 http://archive.canonical.com/ubuntu bionic InRelease
Temporary failure resolving 'archive.canonical.com'
Err:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Temporary failure resolving 'archive.ubuntu.com'
Reading package lists... Done
Building dependency tree... Done
All packages are up to date.
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease Temporary failure resolving 'security.ubuntu.com'
W: Failed to fetch http://archive.canonical.com/ubuntu/dists/bionic/InRelease Temporary failure resolving 'archive.canonical.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.

测了一下网络的连通性,没有任何问题,可以与 DNS 服务器正常通信:

1
2
3
4
5
6
7
8
9
10
11
root@dusays:~# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=1.53 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=1.52 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=118 time=1.56 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=118 time=1.57 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=118 time=1.55 ms
^C
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 1.522/1.551/1.575/0.040 ms

又使用杜老师的博客域名做了下测试,果然又报错了,几乎可以确定是 DNS 问题:

1
2
root@dusays:~# ping dusays.com
ping: dusays.com: Temporary failure in name resolution

查看一下通用 DNS 服务器配置文件,发现该文件已被 systemd-resolved 服务替代,看来需要激活 systemd-resolved 才可以:

1
2
3
4
5
root@dusays:~# 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 "systemd-resolve --status" to see details about the actual nameservers.

解决

添一条设置项到/etc/systemd/resolved.conf「该主机在国外,如果是国内的主机,建议不要用8.8.8.8,以免因为网络不稳导致解析失败」默认该项会被注释,直接执行下面命令即可:

1
root@dusays:~# echo "DNS=8.8.8.8" >> /etc/systemd/resolved.conf

因修改了配置文件,需要重启服务,却发现提示该服务被锁:

1
2
root@dusays:~# systemctl restart systemd-resolved.service
Failed to restart unit: Unit file /etc/systemd/system/systemd-resolved.service is masked.

使用下面命令即可解锁服务:

1
2
root@dusays:~# systemctl unmask systemd-resolved.service
Removed /etc/systemd/system/systemd-resolved.service.

查看一下服务状态,目前该服务已经被解锁,并且服务没在运行,且未设置开机启动:

1
2
3
4
5
6
7
8
root@dusays:~# systemctl status systemd-resolved
● systemd-resolved.service - Network Name Resolution
Loaded: loaded (/lib/systemd/system/systemd-resolved.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:systemd-resolved.service(8)
https://www.freedesktop.org/wiki/Software/systemd/resolved
https://www.freedesktop.org/wiki/Software/systemd/writing-network-configuration-managers
https://www.freedesktop.org/wiki/Software/systemd/writing-resolver-clients

使用下面命令启动服务:

1
root@dusays:~# systemctl start systemd-resolved

使用下面命令设置开机启动:

1
2
3
root@dusays:~# systemctl enable systemd-resolved.service
Created symlink /etc/systemd/system/dbus-org.freedesktop.resolve1.service → /lib/systemd/system/systemd-resolved.service.
Created symlink /etc/systemd/system/multi-user.target.wants/systemd-resolved.service → /lib/systemd/system/systemd-resolved.service.

再次通过杜老师说域名测试一下,发现解析没问题了:

1
2
3
4
5
6
7
8
9
root@dusays:~# ping dusays.com
PING dusays.com (76.76.21.21) 56(84) bytes of data.
64 bytes from 76.76.21.21 (76.76.21.21): icmp_seq=1 ttl=120 time=1.91 ms
64 bytes from 76.76.21.21 (76.76.21.21): icmp_seq=2 ttl=120 time=1.91 ms
64 bytes from 76.76.21.21 (76.76.21.21): icmp_seq=3 ttl=120 time=1.89 ms
^C
--- dusays.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 1.896/1.907/1.914/0.007 ms

重新执行下软件源更新命令,执行成功,问题解决:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
root@dusays:~# apt update
Get:1 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:3 http://archive.canonical.com/ubuntu bionic InRelease [10.2 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1019 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic/main Translation-en [516 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [9184 B]
Get:8 http://archive.ubuntu.com/ubuntu bionic/restricted Translation-en [3584 B]
Get:9 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [8570 kB]
Get:10 http://archive.canonical.com/ubuntu bionic/partner amd64 Packages [1592 B]
Get:11 http://archive.ubuntu.com/ubuntu bionic/universe Translation-en [4941 kB]
Get:12 http://archive.canonical.com/ubuntu bionic/partner Translation-en [1004 B]
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2237 kB]
Get:14 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [1892 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/main Translation-en [436 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [483 kB]
Get:17 http://archive.ubuntu.com/ubuntu bionic-updates/restricted Translation-en [65.6 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1749 kB]
Get:19 http://archive.ubuntu.com/ubuntu bionic-updates/universe Translation-en [375 kB]
Get:20 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [344 kB]
Get:21 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [459 kB]
Get:22 http://security.ubuntu.com/ubuntu bionic-security/restricted Translation-en [61.7 kB]
Get:23 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1138 kB]
Get:24 http://security.ubuntu.com/ubuntu bionic-security/universe Translation-en [259 kB]
Get:25 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [20.9 kB]
Get:26 http://security.ubuntu.com/ubuntu bionic-security/multiverse Translation-en [4732 B]
Fetched 25.0 MB in 5s (5376 kB/s)
Reading package lists... Done
Building dependency tree... Done
15 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@dusays:~#

评论