ubuntu执行apt-get update报错

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
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse
# deb-src 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-src 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-src 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
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse

修改镜像源之后需要重新更新,执行以下语句:

1
2
sudo apt-get update
sudo apt-get upgrade

一般来说,到这里就应该解决了无法更新软件包的问题。

后续如果执行 apt-get update 仍然报错,问题就在于 DNS 没有配置好。

这时候就要修改 DNS 的配置,在网上的一些文章说是要修改 /etc/resolv.conf 文件,但这个文件是自动生成的,在修改后配置会被重置,不能这里修改 DNS 的配置文件,/etc/resolv.conf 文件里的注释也说了不要修改该文件:

1
2
3
$ cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
# ...... 省略其他内容

正确的修改 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
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
## 将DNS配置为以下值
DNS=8.8.8.8 114.114.114.114
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#DNSOverTLS=no
#Cache=no-negative
#DNSStubListener=yes
#ReadEtcHosts=yes

修改后使用以下命令来让配置生效:

1
systemctl restart systemd-resolved.service

此时再次执行 apt-get update 语句就可以正常更新软件包。


ubuntu执行apt-get update报错
https://sunshinechen.cn/2023/10/13/ubuntu执行apt-get update报错/
作者
sunshine
发布于
2023年10月13日
许可协议