ubuntu 版本: 20.04
在一台新的 ubuntu 系统上执行 apt-get update 语句更新软件包失败,出现以下错误:
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 Err http://security.ubuntu.com precise-security InRelease Err http://security.ubuntu.com precise-security Release.gpg Temporary failure resolving 'security.ubuntu.com' Err http://cn.archive.ubuntu.com precise InRelease Err http://cn.archive.ubuntu.com precise-updates InRelease Err http://cn.archive.ubuntu.com precise-backports InRelease Err http://cn.archive.ubuntu.com precise Release.gpg Temporary failure resolving 'cn.archive.ubuntu.com' Err http://cn.archive.ubuntu.com precise-updates Release.gpg Temporary failure resolving 'cn.archive.ubuntu.com' Err http://cn.archive.ubuntu.com precise-backports Release.gpg Temporary failure resolving 'cn.archive.ubuntu.com' Reading package lists... Done W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise/InRelease W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-updates/InRelease W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-backports/InRelease W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/precise-security/InRelease W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/precise-security/Release.gpg Temporary failure resolving 'security.ubuntu.com' W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise/Release.gpg Temporary failure resolving 'cn.archive.ubuntu.com'
出现以上错误时代表镜像源出了问题,一般都会推荐使用国内的镜像源,比如 163 或者阿里云或者清华大学的镜像服务器 (强烈建议使用清华镜像)
将下列文本添加到/etc/apt/sources.list 文件里
1 2 3 4 5 6 7 8 9 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse
修改镜像源之后需要重新更新,执行以下语句:
1 2 sudo apt-get updatesudo apt-get upgrade
一般来说,到这里就应该解决了无法更新软件包的问题。
后续如果执行 apt-get update 仍然报错,问题就在于 DNS 没有配置好。
这时候就要修改 DNS 的配置,在网上的一些文章说是要修改 /etc/resolv.conf 文件,但这个文件是自动生成的,在修改后配置会被重置,不能这里修改 DNS 的配置文件,/etc/resolv.conf 文件里的注释也说了不要修改该文件:
1 2 3 $ cat /etc/resolv.conf
正确的修改 DNS 配置的地方在于 /etc/systemd/resolved ,将 Resolve 的 DNS 配置项修改,如果有多个值则用空格间隔开
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 cat /etc/systemd/resolved.conf [Resolve] DNS=8.8.8.8 114.114.114.114
修改后使用以下命令来让配置生效:
1 systemctl restart systemd-resolved.service
此时再次执行 apt-get update 语句就可以正常更新软件包。