当运行apt-get update时,会出现“failed to fetch”错误。我该如何修复这个问题?

与上面的注释相反,我没有找到解决我的问题的答案。
我已经阅读了几乎十几个apt-get update的问题,大部分来自askubuntu.com,都是关于“failed to fetch”错误,并尝试了那里提供的解决方案。不幸的是,没有一个有效。我最近在我的笔记本电脑上安装了Ubuntu 12.04,与Windows 7双启动。当我尝试时
sudo apt-get update

在终端上,以下内容一直出现:
Err http://ph.archive.ubuntu.com precise InRelease
Err http://ph.archive.ubuntu.com precise-updates InRelease                     
Err http://ph.archive.ubuntu.com precise-backports InRelease                   
Err http://ph.archive.ubuntu.com precise Release.gpg                           
  Unable to connect to ph.archive.ubuntu.com:http: [IP: 91.189.92.177 80]
Err http://ph.archive.ubuntu.com precise-updates Release.gpg            
  Unable to connect to ph.archive.ubuntu.com:http: [IP: 91.189.92.177 80]
Err http://ph.archive.ubuntu.com precise-backports Release.gpg          
  Unable to connect to ph.archive.ubuntu.com:http: [IP: 91.189.92.177 80]
Err http://extras.ubuntu.com precise InRelease                          
Err http://extras.ubuntu.com precise Release.gpg                        
  Unable to connect to extras.ubuntu.com:http:
Err http://security.ubuntu.com precise-security InRelease
Err http://security.ubuntu.com precise-security Release.gpg
  Unable to connect to security.ubuntu.com:http: [IP: 91.189.92.190 80]
Reading package lists... Done
W: Failed to fetch http://ph.archive.ubuntu.com/ubuntu/dists/precise/InRelease  
W: Failed to fetch http://ph.archive.ubuntu.com/ubuntu/dists/precise-updates/InRelease  
W: Failed to fetch http://ph.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://extras.ubuntu.com/ubuntu/dists/precise/InRelease  
W: Failed to fetch http://ph.archive.ubuntu.com/ubuntu/dists/precise/Release.gpg      Unable to connect to ph.archive.ubuntu.com:http: [IP: 91.189.92.177 80]
W: Failed to fetch http://ph.archive.ubuntu.com/ubuntu/dists/precise-updates/Release.gpg  Unable to connect to ph.archive.ubuntu.com:http: [IP: 91.189.92.177 80]
W: Failed to fetch http://ph.archive.ubuntu.com/ubuntu/dists/precise-backports/Release.gpg  Unable to connect to ph.archive.ubuntu.com:http: [IP: 91.189.92.177 80]
W: Failed to fetch http://extras.ubuntu.com/ubuntu/dists/precise/Release.gpg  Unable to connect to extras.ubuntu.com:http:
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/precise-security/Release.gpg  Unable to connect to security.ubuntu.com:http: [IP: 91.189.92.190 80]
W: Some index files failed to download. They have been ignored, or old ones used instead.

注意:

这发生在我安装了Ubuntu 12.04之后立即发生。我对Linux/Ubuntu世界非常陌生,对这些东西一窍不通。

更新管理器(和软件中心)设置中的源列表很短。它只包含两行带有"Canonical"的内容,两行带有"Independent"的内容,以及另外两行。我认为这个列表很短是因为刚刚安装的Ubuntu。

与互联网的连接似乎很好,而且我的朋友的笔记本电脑也是在我之前几乎同时安装了Ubuntu 12.04,他的更新没有问题。我们使用相同的连接,所以我认为互联网连接问题不可能是错误的原因。

尝试的解决方案

here,我在/etc/resolvconf/resolv.conf.d周围进行了探索,并在/etc/resolvconf/resolv.conf.d/head中添加了以下内容:

nameserver 8.8.8.8
nameserver 8.8.4.4

错误仍然发生。

这里这里,我反复更改了更新管理器和软件源设置中要使用的镜像服务器。但是,错误仍然发生。

我还尝试编辑我的源列表,取消勾选其中带有“独立”字样的行(如建议所示)。根据这里的说明,我尝试删除源列表中的derb-src行。然而,仍然没有成功。

最后,这个网站建议运行以下命令:

echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null
or, for a permanent solution,
echo "nameserver 8.8.8.8" | sudo tee /etc/resolvconf/resolv.conf.d/base > /dev/null

遗憾的是,对我而言一无所获。在我参加的所有论坛中,与nameserver 8.8.8.8相关的答案似乎最常出现。请注意,我实际上并不理解这些解决方案,它们的含义或如何执行。我只是简单地按照它们的指示行事。
这是cat /etc/apt/sources.list的输出结果:
# deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release amd64 (20120425)]/ dists/precise/main/binary-i386/

# deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release amd64 (20120425)]/ dists/precise/restricted/binary-i386/
# deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release amd64 (20120425)]/ precise main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu precise main restricted
deb-src http://archive.ubuntu.com/ubuntu precise main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu precise-updates main restricted
deb-src http://archive.ubuntu.com/ubuntu precise-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu precise universe
deb-src http://archive.ubuntu.com/ubuntu precise universe
deb http://archive.ubuntu.com/ubuntu precise-updates universe
deb-src http://archive.ubuntu.com/ubuntu precise-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://archive.ubuntu.com/ubuntu precise multiverse
deb-src http://archive.ubuntu.com/ubuntu precise multiverse
deb http://archive.ubuntu.com/ubuntu precise-updates multiverse
deb-src http://archive.ubuntu.com/ubuntu precise-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu precise-security main restricted
deb-src http://archive.ubuntu.com/ubuntu precise-security main restricted
deb http://archive.ubuntu.com/ubuntu precise-security universe
deb-src http://archive.ubuntu.com/ubuntu precise-security universe
deb http://archive.ubuntu.com/ubuntu precise-security multiverse
deb-src http://archive.ubuntu.com/ubuntu precise-security multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu precise partner
# deb-src http://archive.canonical.com/ubuntu precise partner

## This software is not part of Ubuntu, but is offered by third-party
## developers who want to ship their latest software.
deb http://extras.ubuntu.com/ubuntu precise main
deb-src http://extras.ubuntu.com/ubuntu precise main

以下是用于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
nameserver 127.0.0.1
search nip.upd.edu.ph

据指出,答案在这里,但不幸的是那也没起作用。我尝试选择不同的服务器,但从来没有“选择服务器”的选项。寻找最佳服务器的结果是“没有合适的服务器”。我认为镜像服务器都没问题,而且似乎与互联网连接也没有问题。
使用ping -c3 archive.ubuntu.comping -c3 8.8.8.8都显示0%数据包丢失。对于8.8.8.8的ping有时会显示33%数据包丢失,但大多数情况下是0%。
输入nslookup google.com的结果是:
Server:     127.0.0.1
Address:    127.0.0.1#53

ps aux | grep dns的输出结果:

nobody    1761  0.0  0.0  33012  1284 ?        S    17:04   0:00 /usr/sbin/dnsmasq
 --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/var
/run/sendsigs.omit.d/network-manager.dnsmasq.pid --listen-address=127.0.0.1 --conf-
file=/var/run/nm-dns-dnsmasq.conf --cache-size=0 --proxy-dnssec
joa       2197  0.0  0.0  13576   928 pts/0    S+   17:10   0:00 grep --color=auto dns

1好的,我们就从头开始吧。首先,你能复制在终端输入sudo apt-get update后的精确输出吗?另外,请包括cat /etc/apt/sources.listcat /etc/resolv.conf的输出内容。还有你可以确认你能够正常浏览互联网上的网站吗? - Alaa Ali
@Alaa 回复你的询问,我已经编辑了问题并发布了你需要知道的一切。是的,我能够正常地浏览互联网上的网站。 - jowabels
0%数据包丢失?所以你可以进行ping和浏览,但无法在apt-get更新中解析...嗯。有没有一种只能与apt-get一起使用的DNS设置?无论如何,你能贴出 nslookup google.com 的前两行,并输出 ps aux | grep dns 的结果吗?此外,你是使用静态IP吗? - Alaa Ali
@Alaa,我已经编辑了问题并发布了输出结果。ps aux | grep dns 的输出应该是一行,但我将其编辑成了更易于查看的形式。起初,我会说我使用动态 IP,因为我没有为此付费,但我使用大学的网络连接,所以很难确定我使用的 IP 是静态还是动态的。 - jowabels
@Alaa 我刚刚验证了一下,我使用的是动态IP。 - jowabels
好的,试试我在回答中发布的内容。 - Alaa Ali
等一下!你刚刚编辑了你的帖子!你现在遇到的错误是无法连接到ph.archive.ubuntu.com:http: [IP: 91.189.92.177 80],而不是之前的无法解析,这意味着DNS查找是正常工作的!你还能ping通google.com吗?现在试试换一个不同的镜像。 - Alaa Ali
жҲ‘иҝҳзј–иҫ‘дәҶ/etc/resolv.confе’Ң/etc/apt/sources.listзҡ„иҫ“еҮәгҖӮжҳҜзҡ„пјҢжҲ‘д»Қ然еҸҜд»ҘpingйҖҡи°·жӯҢпјҢдё”дёўеҢ…зҺҮдёә0%пјҢдҪҶд»Қ然жүҫдёҚеҲ°еҗҲйҖӮзҡ„дёӢиҪҪжңҚеҠЎеҷЁгҖӮеҲҮжҚўеҲ°дё»жңҚеҠЎеҷЁд№ҹжІЎжңүеё®еҠ©гҖӮ - jowabels
我有一种感觉,这一切都是因为你在大学里面(对吧?)。他们有某种代理吗? - Alaa Ali
就是这样!非常感谢你的帮助。终于成功了。:) - jowabels
很高兴你解决了这个问题!只是别忘了,如果你连接到另一个网络,你需要删除/更改代理才能上网和更新。 - Alaa Ali
请你帮我检查一下这里的答案:http://askubuntu.com/questions/135932/apt-get-update-failure-to-fetch-cant-connect-to-any-sources - m.alaa8
软件和更新 >> 其他软件 >> 取消选择失败的那一项。再次执行sudo apt update,问题将会解决。 - austin
6个回答

编辑/etc/resolv.conf。在终端窗口中运行。
sudo gedit /etc/resolv.conf

添加这行
nameserver 8.8.8.8

保存。然后执行。
ping www.google.com

如果成功,则运行以下命令。
sudo apt-get --download-only --reinstall install resolvconf
sudo dpkg --purge --force-depends resolvconf
sudo apt-get install resolvconf

如果现在解决失败,请右键单击桌面顶部的网络指示器,在弹出菜单中选择“编辑连接”,选择您的连接,点击“编辑”|“IPv4设置”。将方法从“自动(DHCP)”更改为“仅自动(DHCP)地址”,并在“附加DNS服务器”字段中输入“8.8.8.8”。点击“保存...”。验证“/etc/resolv.conf”现在包含一行“nameserver 8.8.8.8”,并且您仍然可以“ping www.google.com”。

7@guntbert 嗯,你正在与resolvconf软件包的作者交谈。我认为他知道自己在说什么。;-) - Kevin Bowen
@jdthood 之前,我能够ping通8.8.8.8(如问题所述),但在添加了nameserver 8.8.8.8这行后,无法解析,并出现了以下信息:来自nat1.up.edu.ph (10.16.1.2) icmp_seq=1 目标主机不可达 - jowabels
@jdthood 我也编辑了我的连接,改为“仅自动(DHCP)地址”并添加了“8.8.8.8”。是的,/etc/resolv.conf现在包含了“nameserver 8.8.8.8”的行,但是“来自nat1.up.edu.ph(10.16.1.2)icmp_seq = 1目标主机不可达”仍然出现。我恢复了对原始配置所做的所有编辑,但不幸的是它没有解决ping问题。 - jowabels
@jdthood 不好意思,我觉得我在上一条评论中关于还原后错误仍然存在的信息是错误的。当我还原到/etc/resolv.conf和连接设置的原始配置时,“目标主机不可达”的错误停止了,我又可以用0%的数据包丢失率去ping通8.8.8.8了。看起来你需要重置你的连接以使修改生效。 - jowabels
1@maggotbrain 谢谢你再次帮我解围,让我不再尴尬 :-) - guntbert
1@guntbert 没问题,兄弟。我也经历过这样的情况。如果有任何 DNS/Resolveconf 问题,jdthood 是解决它们的专家! - Kevin Bowen
在我使用的Ubuntu 14.04系统中运行良好。谢谢 :D - underscore
3当我按照这些步骤操作后,运行第一个命令后出现了以下错误:Err http://us.archive.ubuntu.com/ubuntu/ wily/main resolvconf all 1.77ubuntu1 404 Not Found [IP: 91.189.91.23 80] E: Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/main/r/resolvconf/resolvconf_1.77ubuntu1_all.deb 404 Not Found [IP: 91.189.91.23 80] - MadPhysicist
1它在20.04运行成功了。如果有人有时间,能否简要描述一下发生了什么以及为什么它成功了呢?😄 - Maciek Woźniak

我曾经面临同样的问题,找到了最简单的解决办法,就是重置 /etc/apt/sources.list 的源。要做到这一点,请按照以下步骤操作:
  1. 获取您的Ubuntu版本,请在控制台中输入:

    lsb_release -r
    
  2. 访问http://repogen.simplylinux.ch/生成新的sources.list

  3. 选择您的国家版本
  4. 勾选前12个框:

    All Ubuntu Brances + Security & Updates

  5. 生成并复制您的新列表
  6. 备份旧文件为sources.list.old

    mv /etc/apt/sources.list /etc/apt/sources.list.old
    
  7. 您现在可以通过以下方式之一打开vi来保存新列表:

    vi /etc/apt/sources.list
    (粘贴并使用`:wq`保存)
    

    或者将“curl”命令(包括用于更新源列表的唯一URI)复制并粘贴到终端中,该命令在repogen输出页面的“Sources List”下方显示。

  8. 搞定了,重新运行apt-get update


2尽管作者的问题已经以不同的方式解决了,但我决定回答这个问题。他的问题是与损坏的source.list文件相关的许多问题的顶级结果(我遇到过3次),我觉得把我的解决方案留在这里会节省我很多时间。 - RienNeVaPlus
我下载了一个 Ubuntu 12 虚拟机,但没有有效的软件源(意大利的)。然后我做了这个操作,完美运行!谢谢! - Feida Kila
在将文本粘贴到vim之前,您需要在打开它后按下字母'i'来进入插入模式。 - s g
这是对我有效的方法。 - Rick
发布列表中没有14.10版本 :( - JoeTidee
在发布列表中也没有16.10(yakkety)。这很不幸。 - byxor

sudo apt-get update终于成功了!我刚意识到问题不在系统上,而是系统如何连接和从互联网检索数据的方式。我刚刚配置了我的网络设置,并将代理检测更改为手动,并填写了我使用的代理服务器的HTTPHTTPSFTPSocks Host字段。当我再次更新时,首先出现了这个错误输出:

E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/)

嗯,不完全像同一个,但类似于它(我从这里复制过来)。但是当我尝试再次更新时,它最终成功了(我之前发布错误输出而不是我看到的确切输出的原因),现在我可以选择一个不同的镜像服务器。如果网络代理配置真的是我的困扰原因,那么对于这样一个基本错误,我感觉有点愚蠢。

无论如何,谢谢大家的帮助!:)


做这个

sudo bash -c 'echo "nameserver 127.0.0.1" >> /etc/resolv.conf'

然后执行sudo apt-get update命令,看看是否有效。

不行,sudo apt-get update 仍然无法工作。我需要删除 nameserver 127.0.01 这一行吗?还是可以保留它? - jowabels
你在还原jdthood的答案后尝试过这样做吗?只需确保您可以ping通8.8.8.8,然后将nameserver 127.0.0.1这一行添加到/etc/resolv.conf文件中,它必须是唯一的行(当然是在注释之后)。 - Alaa Ali
是的,在还原后我做了这个操作,ping 8.8.8.8 没有任何问题(当然,在还原后)。基本上,nameserver 127.0.0.1 是唯一添加到 /etc/resolv.conf 的行。 - jowabels
我只是想知道,在添加之前,原始的/etc/resolv.conf文件已经包含了nameserver 127.0.0.1。当我按照你的回答操作后,编辑后的文件中现在有两行nameserver 127.0.0.1。这是有什么原因吗? - jowabels
我的回答中的命令会在文件中添加一行 nameserver 127.0.0.1,无论文件中原本有什么内容。原始文件(在全新安装后)很可能已经包含了那一行,尽管你在问题中提供的输出结果没有显示出来。但是,在你按照jdthood的回答执行最后的命令后,我认为文件被重置并添加了 nameserver 127.0.0.1 这一行。所以当你按照我的回答执行时,又添加了另一行。不管怎样,如果你有两行,可以安全地删除其中一行。 - Alaa Ali
终于更新成功了!我通过配置网络设置中的代理检测来实现这一点。抱歉,我觉得这是一个很基础的错误。不管怎样,非常感谢您的帮助。在按照您的建议操作后,我终于意识到问题出在连接和获取网络数据的方式上。谢谢! - jowabels
这应该是被选定的答案。它完美地起到了作用。 - Amin Ya

由于http://repogen.simplylinux.ch/没有提供超过18.04的选项,
按下WinKey/Super键并开始打字软件和更新 - 一个粉红色的图标会在你输入时出现;选择或点击它。
在/Ubuntu软件-选项卡中,选择主站点或其他一些从之前不同的地方(最好是本地)。
注意:您的问题可能也与/其他软件-选项卡中的内容有关。 快速修复:取消勾选它。
然后允许更新,或者从终端/ Shell / bash中执行sudo apt update & sudo apt upgrade
我意识到我没有看到预期的软件包更新!在最后,sudo apt updated & sudo apt upgrade 没有显示任何更新。
20.04,我遇到了一个关于Ubuntu瑞典镜像的问题,但是通过以下操作: 这里试图不涉及操作系统/版本...
$ source /etc/os-release
$ grep -I "$(echo $VERSION_CODENAME)" /etc/apt/sources.list  
我发现更通用的URL http://se.archive.ubuntu.com/ubuntu/sources.list中被注释掉了。
# deb cdrom:[Ubuntu 20.04.1 LTS _Focal Fossa_ - Release amd64 (20200731)]/ focal main restricted
deb http://ftp.lysator.liu.se/ubuntu/ focal restricted main
# deb-src http://se.archive.ubuntu.com/ubuntu/ focal main restricted
...
因此,我使用sudo nano /etc/apt/sources.list并将所有那些#行恢复为更通用的http://se.archive.ubuntu.com/ubuntu/
# deb cdrom:[Ubuntu 20.04.1 LTS _Focal Fossa_ - Release amd64 (20200731)]/ focal main restricted
# deb http://ftp.lysator.liu.se/ubuntu/ focal restricted main
deb-src http://se.archive.ubuntu.com/ubuntu/ focal main restricted
...

...一直到文件的内容结束。

这样做使得

sudo apt updated & sudo apt upgrade

恢复了预期的功能。

无法确定为什么会有特定于镜像且不起作用的URL。
我认为这是一个"损坏"的更新,
最好使用一个通用且可能由中央管理的更新。


我遇到了同样的错误,后来发现是因为我添加了armhf架构。
运行以下命令-
sudo dpkg --print-foreign-architectures
最好移除任何新的架构,因为并非所有的软件源都有针对它们的索引文件,会导致这个错误。
运行-
sudo dpkg --remove-architecture <architecture name>
然后再运行-
sudo apt-get update
这个方法解决了我的问题。
希望能对你有所帮助... :)