更换 Debian 10 软件源为清华源后执行 `apt update` 出现“签名无效”或“NO_PUBKEY”错误,导致更新失败。常见原因是源地址配置不正确或系统缺少清华镜像的 GPG 密钥。此外,若未同步更新 `sources.list` 中的发行版代号(如误用 bullseye 替代 buster),也会引发无法获取仓库元数据的问题。需检查源地址格式、密钥环状态及发行版本匹配性。
1条回答 默认 最新
Jiangzhoujiao 2025-09-24 01:35关注1. 问题背景与现象描述
在 Debian 10(代号 buster)系统中,为提升软件包下载速度,运维人员常将默认的官方 APT 源更换为国内镜像源,如清华大学开源软件镜像站(https://mirrors.tuna.tsinghua.edu.cn)。然而,在执行
apt update时,常出现如下错误:W: GPG error: http://mirrors.tuna.tsinghua.edu.cn/debian buster InRelease: The following signatures were invalid: EXPKEYSIG ... E: The repository 'http://mirrors.tuna.tsinghua.edu.cn/debian buster InRelease' is not signed. W: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/debian/dists/buster/InRelease 404 Not Found [IP: 101.6.15.130 80] E: Some index files failed to download. They have been ignored, or old ones used instead.上述现象可归因于三大类问题:源地址配置错误、GPG 密钥缺失或过期、发行版代号不匹配。
2. 常见错误类型分类
- NO_PUBKEY 错误:APT 无法验证仓库签名,提示缺少公钥 ID。
- 签名无效(The following signatures were invalid):密钥存在但已过期或被撤销。
- 404 Not Found / InRelease 无法获取:
sources.list中的发行版名称错误,如误将buster写成bullseye或bookworm。 - 混合源协议冲突:HTTP 与 HTTPS 混用可能导致元数据校验失败。
3. 根本原因分析流程图
graph TD A[执行 apt update 失败] --> B{检查错误类型} B --> C[是否提示 NO_PUBKEY?] B --> D[是否提示 404 或 InRelease 找不到?] B --> E[是否提示签名无效?] C -->|是| F[缺少 GPG 公钥] D -->|是| G[sources.list 发行版代号错误] E -->|是| H[密钥过期或仓库变更] F --> I[导入清华源 GPG 密钥] G --> J[修正为 Debian 10 正确代号 buster] H --> K[更新密钥环或重新配置源]4. 解决方案分步实施
4.1 验证当前源配置
首先检查
/etc/apt/sources.list文件内容:# Debian 10 (buster) 清华源正确配置示例 deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free deb https://mirrors.tuna.tsinghua.edu.cn/debian-security/ buster/updates main contrib non-free deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security/ buster/updates main contrib non-free deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free关键点:
- 使用 https 协议以增强安全性;
- 所有条目中的发行版代号必须为 buster,不可使用
stable或其他版本代号; - 安全更新源应指向
buster/updates而非security的错误路径。
4.2 修复 GPG 密钥缺失问题
若错误信息包含
NO_PUBKEY ABCD1234,需导入对应密钥:# 方法一:通过 gpg 和 apt-key(适用于传统方式) gpg --keyserver keyserver.ubuntu.com --recv-keys ABCD1234 gpg --export --armor ABCD1234 | sudo apt-key add - # 方法二:推荐使用 modern 方式(避免 apt-key deprecation) sudo mkdir -p /etc/apt/keyrings curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/debian/archive-keyring.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/debian-archive-keyring.gpg5. 完整配置验证表格
检查项 正确值 常见错误 修复命令/建议 Debian 版本代号 buster bullseye, bookworm, stable sed -i 's/bullseye/buster/g' /etc/apt/sources.list 主源 URL https://mirrors.tuna.tsinghua.edu.cn/debian/ http://debian.org, 错误子路径 手动编辑 sources.list GPG 密钥路径 /etc/apt/keyrings/debian-archive-keyring.gpg 未导入或路径错误 使用 curl + gpg --dearmor 导入 协议类型 HTTPS HTTP 明文传输 替换 http 为 https security 源路径 buster/updates stretch-security, 不带 updates 修正为 buster/updates 6. 自动化检测脚本示例
可用于批量服务器巡检:
#!/bin/bash # check_debian_source.sh RELEASE=$(lsb_release -cs) if [ "$RELEASE" != "buster" ]; then echo "错误:当前系统发行版为 $RELEASE,非 Debian 10 (buster)" exit 1 fi if ! grep -q "tuna.tsinghua.edu.cn" /etc/apt/sources.list; then echo "警告:未检测到清华源配置" fi if ! apt update 2>&1 | grep -q "NO_PUBKEY"; then echo "GPG 密钥正常" else echo "检测到 NO_PUBKEY 错误,建议导入密钥" fi本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报