当前位置:   article > 正文

Ubuntu 18.04系统中执行 sudo apt-get update 报错【总结版】【命令行解决】_aptupdate报错怎么解决

aptupdate报错怎么解决


前言

在执行 sudo apt-get install 安装新软件包或升级现有软件包之前,通常会建议先运行 sudo apt-get update,以确保你安装的是最新版本。
但是往往敲完sudo apt-get update命令,会出现超时一段时间后报错结束的问题。


1、命令简单描述

apt-get

Debian 和 Ubuntu 系统上用于包管理的一个工具,而其他 Linux 发行版可能使用不同的包管理工具(如 yum、zypper等)。

update

update 是同步 /etc/apt/sources.list/etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。

upgrade

upgrade 是升级已安装的所有软件包,升级之后的版本就是本地索引里的。 因此,在执行 upgrade 之前一定要执行 update,这样才能是最新的。

#新添加begin
nameserver 8.8.8.8
nameserver 8.8.4.4
#新添加end
nameserver 127.0.0.53
options edns0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Esc取消编辑模式,:wq!保存编辑退出,执行cat命令查看是否编辑成功

sudo cat /etc/resolv.conf 
  • 1

之后执行sudo apt-get update就正常了。

2、本地问题log

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

3、原因分析

报错提示:

W: Some index files failed to download. They have been ignored, or old ones used instead.
W: 一些索引文件下载失败。它们要么被忽略,要么使用旧的索引文件。
  • 1
  • 2

可以看出是网络原因
主要是底下这两种原因导致的:
3.1、系统网络问题;【本问题解决方案】
3.2、ubuntu18.04 自带的源/etc/apt/sources.list有问题;

4、解决方法

4.1、系统网络问题【本问题解决方案】

解决方案:检查DNS配置
(1)Ubuntu 18.04 查看DNS命令:

sudo cat /etc/resolv.conf
  • 1
root@ubuntu:~# cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

(2)添加Google 提供的公共 DNS 服务器的 IP 地址 8.8.8.8。
两种方式添加:
第一种:直接在 /etc/resolv.conf 文件后面追加命令:

echo "nameserver 8.8.8.8" >> /etc/resolv.conf
  • 1

第二种:修改文件 sudo vi /etc/resolv.conf,修改后的文件如下:

root@ubuntu:~# sudo vi /etc/resolv.conf 
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0
nameserver 8.8.8.8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

(4)尝试 apt update 一下,速度快多了!

sudo apt-get update
  • 1

4.2、ubuntu18.04 自带的源/etc/apt/sources.list有问题

4.2.1、解决方案:替换源/etc/apt/sources.list

(1)备份源列表
Ubuntu配置的默认源并不是国内的服务器,下载更新软件都比较慢。

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
  • 1

(2)打开 /etc/apt/sources.list 文件修改

sudo vim /etc/apt/sources.list
  • 1

(3)在上面打开的文件开头添加(阿里源)就行

# 阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

# ubuntu
deb http://cn.archive.ubuntu.com/ubuntu bionic-security main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu bionic-security main restricted
deb http://cn.archive.ubuntu.com/ubuntu bionic-security universe
# deb-src http://cn.archive.ubuntu.com/ubuntu bionic-security universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-security multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu bionic-security multiverse
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

附记:(清华源

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

(4)同样的尝试更新一下,速度快多了!

sudo apt-get update
  • 1
4.2.2、如果替换源/etc/apt/sources.list后还是不行

把它们加入到了系统的源列表中后,sudo apt-get update还是不行。但是,所有停止维护的版本都可以使用 old 源。所以在原列表中把原来的地址改为带有 old 源的就可以了。
直接在前面加old-即可。
例如:

deb http://cn.archive.ubuntu.com/ubuntu bionic-security main restricted
改为
deb http://old-cn.archive.ubuntu.com/ubuntu bionic-security main restricted
  • 1
  • 2
  • 3

5、apt-get update成功log

root@ubuntu:~# apt-get update
Hit:1 http://mirrors.ustc.edu.cn/ubuntu-ports bionic InRelease
Hit:2 http://mirrors.ustc.edu.cn/ubuntu-ports bionic-updates InRelease
Hit:3 http://mirrors.ustc.edu.cn/ubuntu-ports bionic-backports InRelease
Hit:4 http://mirrors.ustc.edu.cn/ubuntu-ports bionic-security InRelease
Reading package lists… Done
root@ubuntu:~# apt-get upgrade
Reading package lists… Done
Building dependency tree
Reading state information… Done
Calculating upgrade… Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.


谢谢关注点赞哈!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/387330
推荐阅读
相关标签
  

闽ICP备14008679号