中文

修复 Ubuntu 和 Debian VPS 上常见的 APT 错误

1 min
VPS/VDS 主机

迟早有一天,您 VPS Linux Infrawire 上的 apt update 会甩出一大片红色报错:缺少 GPG 密钥、软件源已不存在,或者锁一直不释放。每个错误都有明确的原因和简短的解决办法。

本指南按从影响最小到影响最大的顺序,介绍 Ubuntu 24.04 和 Debian 12 上最常见的问题。

📋 前提条件

  • 运行 Ubuntu 24.04 或 Debian 12 的 VPS
  • root SSH 访问权限(IP 和 root 密码可在客户区找到)
  • 基本的命令行知识

1. 先找到软件源

修复之前,先弄清楚软件源定义在哪里:

Bash
cat /etc/os-release cat /etc/apt/sources.list ls -l /etc/apt/sources.list.d/ apt update 2>&1 | grep -E '^(Err|W:|E:)'

在 Ubuntu 24.04 上,主要软件源位于 /etc/apt/sources.list.d/ubuntu.sources,采用 deb822 格式(.sources 文件)。Debian 12 仍在 /etc/apt/sources.list 中使用传统的单行格式,两种格式可以并存。

Bash
# deb822 条目示例 (Ubuntu 24.04) Types: deb URIs: http://archive.ubuntu.com/ubuntu/ Suites: noble noble-updates noble-backports Components: main restricted universe multiverse Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

2. 锁错误与 dpkg 中断

提示 Could not get lock /var/lib/dpkg/lock-frontend 表示另一个进程正在使用 apt,最常见的是刚开机时运行的 unattended-upgrades。操作前先检查:

Bash
ps aux | grep -E 'apt|dpkg|unattended' | grep -v grep lsof /var/lib/dpkg/lock-frontend

如果有进程在运行,请等待几分钟。只有确认没有任何进程占用时才删除锁文件(判断错误会有风险),然后修复中断的状态:

Bash
rm -f /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock /var/cache/apt/archives/lock dpkg --configure -a apt --fix-broken install

dpkg was interrupted, you must manually run 'dpkg --configure -a' 这条提示正是由第一条命令解决的。

3. NO_PUBKEY 与缺失的 GPG 密钥

错误 The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ABCD1234 表示某个第三方软件源没有受信任的密钥。请不要使用已被弃用的 apt-key:把密钥存放到 /etc/apt/keyrings/,并用 signed-by 引用。

Bash
install -d -m 0755 /etc/apt/keyrings curl -fsSL https://example.com/repo/key.gpg | gpg --dearmor -o /etc/apt/keyrings/example.gpg chmod 644 /etc/apt/keyrings/example.gpg echo 'deb [signed-by=/etc/apt/keyrings/example.gpg] https://example.com/repo stable main' > /etc/apt/sources.list.d/example.list apt update

请使用该软件官方文档给出的密钥 URL,若缺少 curl gnupg 请先安装。如果错误出在 Ubuntu 或 Debian 官方归档本身,请重新安装密钥包:apt install --reinstall ubuntu-keyring 或 debian-archive-keyring。

4. 失效的第三方软件源

PPA 或厂商软件源出现 404 Not Found 或 does not have a Release file,通常说明它已不再支持您的系统版本。建议禁用而不是删除:

Bash
# 传统 .list 文件:重命名 mv /etc/apt/sources.list.d/broken.list /etc/apt/sources.list.d/broken.list.disabled # deb822 .sources 文件:在该段内加入这一行 Enabled: no

然后重新运行 apt update。错误消失,已安装的软件包照常工作。

5. 时钟错误:Release file not valid yet

Release file for ... is not valid yet (invalid for another 3h 12min) 表示系统时钟比软件源的时间戳慢。检查并重新同步:

Bash
timedatectl timedatectl set-ntp true systemctl restart systemd-timesyncd

如果未安装 systemd-timesyncd(部分精简版 Debian 镜像),请改用 apt install -y chrony。

6. 已终止支持的版本

版本到达生命周期终点后,其软件包会被移走,apt update 返回 404 或 no longer has a Release file。对于非 LTS 的 Ubuntu 版本,把软件源指向旧版本归档:

Bash
sed -i 's|http://archive.ubuntu.com/ubuntu|http://old-releases.ubuntu.com/ubuntu|g; s|http://security.ubuntu.com/ubuntu|http://old-releases.ubuntu.com/ubuntu|g' /etc/apt/sources.list /etc/apt/sources.list.d/*.sources 2>/dev/null apt update

对于旧版 Debian,请使用 http://archive.debian.org/debian 并删除 security 行。请把 EOL 系统视为权宜之计:它不再获得安全修复,因此应计划升级到受支持的版本。

7. 验证

Bash
apt update apt upgrade --dry-run apt-get check

apt update 必须在没有 Err 或 E: 行的情况下完成,apt-get check 不应输出任何内容。

🛠️ 故障排查

每次尝试锁都会再次出现:很可能是 unattended-upgrades 正在运行。查看 systemctl status unattended-upgrades 并等待其结束。

Conflicting values set for option Signed-By:同一个软件源被用不同密钥声明了两次。用 grep -r example.com /etc/apt/ 查找,只保留一条。

apt --fix-broken install 在某个软件包脚本处失败:阅读最后几行错误,然后尝试 dpkg --remove --force-remove-reinstreq PACKAGE,再正常重新安装。


✅ 您的包管理器恢复健康:更新和安装不再报错!

及时了解最新的优惠、折扣和特别促销活动。

查看促销

我们使用Cookie

我们使用分析性Cookie来改善您在我们网站上的体验。数据不会出售给第三方。了解更多请查看我们的 隐私政策.