APT Hash sum mismatch錯誤的常見解決方法總結

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 multiverse
deb http://mirrors.163.com/ubuntu/ intrepid-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ intrepid-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ intrepid-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ intrepid-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ intrepid main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ intrepid-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ intrepid-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ intrepid-proposed main restricted universe multiverse
deb-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 restricted
deb http://mirrors.ustc.edu.cn/ubuntu/ precise universe
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise universe
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-updates universe
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-updates universe
deb http://mirrors.ustc.edu.cn/ubuntu/ precise multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-updates multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-updates multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-backports main restricted universe multiverse
deb-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-workaround
2,添加文本
 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新鏡像的時候,實際上是在容器裏面,有一點不同。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章