APT Hash sum mismatch错误的常见解决方法总结

来源:互联网 发布:商务中国域名转出 编辑:程序博客网 时间:2024/06/06 21:45

APT Hash sum mismatch错误的常见解决方法总结

LINUX报这个错误的时候,有很多的原因,通常是出现在使用apt-get update的时候, apt 的全称是Advanced Packaging Tool。在apt版本1.2.0之前,它是有BUG的,后续版本修正了,或许可以通过直接升级apt版本解决。问题原因五花八门,中外网友们的解决办法也是多种多样。

Hash sum mismatch这个错误是什么意思?

  apt repository metadata is organized in such a way that a top level file contains the checksums of other pieces of repository metadata. The “Hash sum mismatch” error indicates to the user that apt has run a checksum against the repository metadata it has downloaded and the checksum apt computed does not match the checksum listed in the top level file.Unfortunately, due to a bug in apt, metadata files compressed with lzma (.xz files) are occasionally downloaded (and in some cases) decompressed incorrectly resulting in a broken file.     As a result, the checksum of the broken file will be incorrect and cause apt to produce the “Hash sum mismatch” error.

这里写图片描述

Hash sum mismatch 为何产生?

There are at least 3 ways this can happen for most Ubuntu and Debian based systems today:Stale metadata cached between the client and server. This is unlikely in most cases and not possible if SSL is used.The metadata does not match because of a bug during the extraction of the metadata.The repository is being updated while an apt-get update is run, or apt has cached a stale Release file.

Users can avoid all 3 cases by: (如何避免?)

Using SSL.Disabling XZ compressed metadata, or ensuring a newer version of APT is used.Using the new Acquire-by-hash feature available in APT 1.2.0.

这里写图片描述

截图取自下文参考链接
参考链接4: Solution reference4
参考链接5: Solution reference5

怎么解决?总结能够google到的常见解决方法

本人在遇到这个错误的时候,查到了很多方法,这些方法的都是有网友验证过的,具体哪种方法适用于读者的问题的解决,可以多尝试。下面,我就按照方法被应用频率的高低来阐述。

(1)清cache缓存:

$ sudo apt-get clean  $ sudo apt-get update --fix-missing 

参考链接1:Solution reference1

(2)删除/var/lib/apt/lists/partial/中的下载文件:

$ sudo rm -R /var/lib/apt/lists/partial/*$ sudo apt-get update && sudo apt-get upgrade

参考链接2:Solution reference2

(3)更换apt更新源:

有时候,因为处于GFW内、网速下载更新等原因,我们是使用UBUNTU的时候,改为国内的更新源更加方便 。校园网的话,使用edu.cn的源,比如中科大,其他的话可以使用网易、阿里等。
替换前做个副本备份先:

$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

编辑源列表,加入国内源地址:

$ sudo gedit /etc/apt/sources.list
# 网易新开的更新源deb http://mirrors.163.com/ubuntu/ intrepid main restricted universe multiversedeb http://mirrors.163.com/ubuntu/ intrepid-security main restricted universe multiversedeb http://mirrors.163.com/ubuntu/ intrepid-updates main restricted universe multiversedeb http://mirrors.163.com/ubuntu/ intrepid-proposed main restricted universe multiversedeb http://mirrors.163.com/ubuntu/ intrepid-backports main restricted universe multiversedeb-src http://mirrors.163.com/ubuntu/ intrepid main restricted universe multiversedeb-src http://mirrors.163.com/ubuntu/ intrepid-security main restricted universe multiversedeb-src http://mirrors.163.com/ubuntu/ intrepid-updates main restricted universe multiversedeb-src http://mirrors.163.com/ubuntu/ intrepid-proposed main restricted universe multiversedeb-src http://mirrors.163.com/ubuntu/ intrepid-backports main restricted universe multiverse# 或者, 加入如下内容(中科大的):deb http://mirrors.ustc.edu.cn/ubuntu/ precise-updates main restricted deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-updates main restricteddeb http://mirrors.ustc.edu.cn/ubuntu/ precise universedeb-src http://mirrors.ustc.edu.cn/ubuntu/ precise universedeb http://mirrors.ustc.edu.cn/ubuntu/ precise-updates universedeb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-updates universedeb http://mirrors.ustc.edu.cn/ubuntu/ precise multiversedeb-src http://mirrors.ustc.edu.cn/ubuntu/ precise multiversedeb http://mirrors.ustc.edu.cn/ubuntu/ precise-updates multiversedeb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-updates multiversedeb http://mirrors.ustc.edu.cn/ubuntu/ precise-backports main restricted universe multiversedeb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-backports main restricted universe multiverse

再更新列表:

$ sudo apt-get update$ sudo apt-get upgrade

(4)指定apt下载文件的压缩格式或者设置Acquire-by-hash的值:

指定apt下载文件的压缩格式

$ sudo rm -rf /var/lib/apt/lists/partial$ sudo apt-get update -o Acquire::CompressionTypes::Order::=gz

对于第二条命令:也可以设置下载文件的压缩格式为bz2
如果不想每次的对apt的配置进行第二条命令那样设置的话,可以这么做。

1, 创建一个新文件, /etc/apt/apt.conf.d/99compression-workaround2,添加文本 Acquire::CompressionTypes::Order:: "gz";

现在,apt-get update 将会优先使用gzip 格式压缩的元数据。

这么设置的目的是因为apt本身的bug(bug报告),真正的解决办法还是利用apt的新特性进行设置Acquire-by-hash的值为‘Yes’,参考里面没有讲到如何enable或者take advantage of 这个Acquire-by-hash的feature, 如果有读者朋友找到在哪儿设置,麻烦留言~

详情参考
参考链接4: Solution reference4
参考链接5: Solution reference5
或者直接试着升级一下apt,如果apt的版本比较低的话。

(5)对代理服务器进行设置:

还有些开发者遇到的问题,是因为公司的Proxy的设置。
如果读者朋友使用了Proxy,那么可以尝试,在/etc/apt/apt.conf.d/ 目录下创建一个名称为99fixbadproxy 的文件

$ sudo gedit /etc/apt/apt.conf.d/99fixbadproxy

把以下文本复制到上述文件中,然后保存退出:

Acquire::http::Pipeline-Depth 0;Acquire::http::No-Cache true;Acquire::BrokenProxy    true;

运行更新命令

$ sudo apt-get update

参考链接6:Solution reference6

(6)检查下载源的服务器与你所处地区的时差,修正时间戳timestamp:

国外网站上的一个回答如下:

To figure out whether this issue is fixed, you can visit, for example, the page at https://apt.dockerproject.org/repo/dists/ubuntu-xenial/main/binary-amd64/ and check the timestamp for the InRelease file.Currently it still says 11:06 (UTC) which is the version of the file that has the wrong checksums. If it says a later time, then it has probably been fixed.Now the time is 13:25 (UTC) and we are still waiting

参考链接7:Solution reference7

N.B. 如果读者朋友是在docker中使用dockerfile进行build新镜像出错了的话,可以参考我的下一篇。

官网的那个docker-whale镜像build代码,我运行是有错误的,报的错误也是这个Hash sum mismatch。但是与我们上述的方法还是有些区别,毕竟上面的情况,都是网友在物理主机下运行apt-get update 得到的error,我们使用dockerfile进行build新镜像的时候,实际上是在容器里面,有一点不同。

1 0