在 Ubuntu 系统中执行 sudo apt update 时,经常会遇到 Hash Sum mismatch(哈希值不匹配)错误,导致软件源索引下载失败。本文以 Ubuntu 22.04(jammy)为例,分享解决方案,核心是切换稳定的清华镜像源彻底解决该问题。
一、问题现象
执行 apt update 时出现如下报错,核心特征是 Hash Sum mismatch,提示下载的文件哈希值与服务器声明的不一致:
E: Failed to fetch
http://mirrors.aliyun.com/ubuntu/dists/jammy/universe/source/by-hash/SHA256/8f73d18065a4f05ee7362c50553ea4b26ca2b4b3786472676b6b1ee58b4cff72
Hash Sum mismatch Hashes of expected file:
- SHA256:8f73d18065a4f05ee7362c50553ea4b26ca2b4b3786472676b6b1ee58b4cff72
Hashes of received file:
- SHA256:5d071b2b37ce6c225da79f73684b8c56ddff86539e89f2c519208bcc2b9f1e59
E: Some index files failed to download. They have been ignored, or old ones used instead.
二、问题原因
- 第三方镜像源(如阿里云)临时同步异常,导致文件哈希值不一致;
- 本地 APT 缓存文件损坏 / 过期,与服务器文件校验不通过;
- 网络传输不稳定(如 IPv6 干扰),导致文件下载不完整。
三、解决方案(切换清华镜像源)
步骤 1:备份原有源列表(重要)
避免修改出错后无法恢复,先备份系统默认的 sources.list 文件:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
步骤 2:编辑源列表文件
使用 nano 编辑器打开源配置文件(也可使用 vim):
sudo nano /etc/apt/sources.list
步骤 3:替换为清华镜像源
清空文件原有内容,粘贴以下适配 Ubuntu 22.04(jammy)的清华源配置(其他版本可替换 jammy 为对应版本代号,如 20.04 为 focal):
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

