适用:Debian 系(apt)与 RPM 系(dnf/yum) 耗时:约 30 分钟 前置:第一周 D3(会装软件)

D3 教你"怎么装"。这一篇讲**“为什么装不上”**——九成的安装失败,根子在软件源。


一、软件源是什么

你敲 apt install htop,系统去哪拿这个包?

答案:从"软件源"(repository)拿。 那是一台服务器,上面放着成千上万个预先编译好的软件包。

apt install htop
    ↓
读 /etc/apt/sources.list(+ sources.list.d/)
    ↓
按配置的地址去下载 htop + 依赖
    ↓
解包、安装、注册到系统

所以装不上,第一件事永远是:源对不对、通不通。


二、Debian 系:apt 的源在哪

看当前配置

cat /etc/apt/sources.list
ls /etc/apt/sources.list.d/

现代 Ubuntu 用的是新格式:

cat /etc/apt/sources.list.d/ubuntu.sources

老格式长这样

deb http://archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
deb http://archive.ubuntu.com/ubuntu/ jammy-security main restricted
deb-src http://archive.ubuntu.com/ubuntu/ jammy main universe

四个字段:

字段含义
deb二进制包(deb-src 是源码包)
http://...源地址
jammy发行版代号(必须和你的系统匹配!)
main universe组件(官方/社区)

新格式(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

查你的系统代号

lsb_release -cs
# 或
cat /etc/os-release | grep VERSION_CODENAME

Ubuntu 代号对照:

版本代号
20.04focal
22.04jammy
24.04noble
24.10oracular

Debian:

版本代号
11bullseye
12bookworm
13trixie

⚠️ 代号写错是最常见的错误。比如 22.04 写成 focal(那是 20.04),会装上一堆版本不匹配的包,系统可能直接坏掉。


三、换源:国内加速 or 内网源

为什么换

情况表现换什么
官方源在国外下载很慢、经常超时国内镜像(阿里/清华/中科大)
单位内网完全连不上外网内网源(问 IT 要)
源已停止维护404换到 old-releases 或升级系统

备份!备份!备份!

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak-$(date +%Y%m%d)

改源之前一定先备份。 改错了还能一键恢复。

换成国内镜像(Ubuntu 22.04 示例)

sudo nano /etc/apt/sources.list

全部内容替换成:

# 阿里云镜像(Ubuntu 22.04 jammy)
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

常用镜像地址:

镜像地址
阿里云https://mirrors.aliyun.com/ubuntu/
清华https://mirrors.tuna.tsinghua.edu.cn/ubuntu/
中科大https://mirrors.ustc.edu.cn/ubuntu/
华为云https://repo.huaweicloud.com/ubuntu/

一键换源(Ubuntu):

sudo sed -i 's|http://.*archive.ubuntu.com|https://mirrors.aliyun.com|g; s|http://security.ubuntu.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list
sudo apt update

⚠️ sed -i 改配置有风险。先备份,然后在 apt update 时看有没有报错。

麒麟 / UOS 换源

这两个系统默认就用国内源(一般是系统厂商自己的),通常不需要换。

真需要换时,配置文件位置:

# 银河麒麟 V10
cat /etc/apt/sources.list

# 统信 UOS
cat /etc/apt/sources.list
ls /etc/apt/sources.list.d/

⚠️ 重要提醒:麒麟/UOS 的源和 Ubuntu 不通用。 用 Ubuntu 的源装包会导致系统组件版本冲突。要换就换厂商提供的官方源,或者问 IT。

换完必须验证

sudo apt update

看输出:

命中:1 https://mirrors.aliyun.com/ubuntu jammy InRelease
命中:2 https://mirrors.aliyun.com/ubuntu jammy-updates InRelease
...
正在读取软件包列表... 完成
  • 全是 命中/获取 → ✅ 成功
  • 出现 404 Not Found → 代号或路径写错
  • 出现 Could not resolve → 域名解析不了(网络/DNS 问题)
  • 出现 NO_PUBKEY → 缺 GPG 密钥(见下一节)

四、GPG 密钥:签名验证

apt 会验证包的签名,防止被篡改。密钥不对就会报错。

报错长这样

W: GPG error: https://xxx InRelease: The following signatures couldn't be verified
   because the public key is not available: NO_PUBKEY 1234ABCD5678EFGH

修法

# 从密钥服务器拉取
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1234ABCD5678EFGH

# 或者(新版推荐)
curl -fsSL https://源地址/KEY.gpg | sudo gpg --dearmor -o /usr/share/keyrings/xxx.gpg

apt-key 在新版已弃用,但仍然能用。新写法是把密钥放 /usr/share/keyrings/ 并在源里用 signed-by= 指定。


五、RPM 系:dnf / yum 的源

看当前源

dnf repolist
cat /etc/yum.repos.d/*.repo

源文件格式

[baseos]
name=CentOS Stream $releasever - BaseOS
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-centosofficial
enabled=1

关键字段:baseurl(地址)、enabled(启用)、gpgcheck(签名验证)。

换源

先备份:

sudo mkdir -p /etc/yum.repos.d/backup
sudo mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/

写入新源(openEuler 示例):

sudo nano /etc/yum.repos.d/openeuler.repo
[openEuler-everything]
name=openEuler-everything
baseurl=https://repo.openeuler.org/openEuler-24.03-LTS/everything/$basearch/
enabled=1
gpgcheck=0

验证:

sudo dnf clean all
sudo dnf makecache
sudo dnf repolist

gpgcheck=0 是权宜之计——关了签名验证,安全性下降。理想情况应该配 gpgkey。

麒麟服务器版(RPM 系)

cat /etc/yum.repos.d/*.repo

麒麟服务器版的源是 kylin.repo,一般指向厂商仓库。不要用 CentOS 的源替换——包名和版本都不同。


六、诊断流程:装不上时按这个查

① sudo apt update 有没有报错?
    ├─ 有 404      → 代号/路径错,或源已停止维护
    ├─ 有 域名解析失败 → DNS 或网络
    ├─ 有 NO_PUBKEY → 缺密钥
    └─ 有 连接超时  → 源地址不通(防火墙/内网限制)
        ↓
② apt search 包名 —— 能找到吗?
    ├─ 找不到 → 这个名字不在任何已启用的源里
    └─ 找到  → 继续
        ↓
③ apt install 具体报什么?
    ├─ 依赖冲突   → apt --fix-broken install
    ├─ 磁盘满     → df -h
    └─ 锁被占用   → 等软件商店跑完

一条命令做全面体检

echo "=== 系统 ==="; cat /etc/os-release | head -3
echo "=== 代号 ==="; lsb_release -cs 2>/dev/null || grep VERSION_CODENAME /etc/os-release
echo "=== 源配置 ==="; cat /etc/apt/sources.list 2>/dev/null | grep -v '^#' | grep -v '^$'
ls /etc/apt/sources.list.d/ 2>/dev/null
echo "=== update 测试 ==="; sudo apt update 2>&1 | tail -15
echo "=== 磁盘 ==="; df -h /

把这段输出贴给 IT 或贴到搜索里,比说"装不上"有用一百倍。


七、离线环境怎么办

单位内网完全没网 → 三条路。

路 1:要内网源(最优)

问 IT 有没有内部 yum/apt 镜像。 有的话配上去就完事,和公网一样用。

这是最应该先问的。 很多单位其实有,只是没人告诉用户。

路 2:下载后离线安装

Debian 系:

# 在有网的同版本机器上下载(含依赖)
apt-get download $(apt-cache depends --recurse --no-recommends --no-suggests \
  --no-conflicts --no-breaks --no-replaces --no-enhances htop \
  | grep "^\w" | sort -u)

# 拷到目标机器
sudo dpkg -i *.deb

RPM 系:

# 只下载不装
sudo dnf download --resolve htop

# 拷过去
sudo dnf install ./*.rpm

路 3:用便携格式

格式特点
AppImage单文件,chmod +x 就能跑,不装进系统
静态编译二进制常见于 Go/Rust 工具,下载即用
tar.gz 解压版解压到目录,改 PATH 即可

离线环境里,AppImage 和静态二进制是最省事的。


八、动手:四个练习

练习 1:体检当前源

cat /etc/os-release | grep -E "^(NAME|VERSION)="
lsb_release -cs 2>/dev/null || grep VERSION_CODENAME /etc/os-release
cat /etc/apt/sources.list 2>/dev/null | grep -v '^#' | grep -v '^$' | head -5
sudo apt update 2>&1 | tail -8

练习 2:备份源配置

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak-$(date +%Y%m%d-%H%M%S)
ls -l /etc/apt/sources.list.bak-*

养成"改之前先备份"的习惯。

练习 3:测源的连通性(不改配置)

# 从源配置里提取域名
SRC=$(grep -m1 '^deb ' /etc/apt/sources.list | awk '{print $2}')
echo "源地址: $SRC"
HOST=$(echo "$SRC" | awk -F/ '{print $3}')
echo "域名: $HOST"
ping -c 2 "$HOST"

ping 不通说明网络到源有问题——这时候改源配置是白改。

练习 4:搜一个包,看它来自哪个源

apt-cache policy htop

输出里会有 500 https://mirrors.aliyun.com/ubuntu jammy/main amd64 Packages——这就告诉你包来自哪个源。


九、验证:你过关了吗

  • 知道 apt 的源配置在哪(/etc/apt/sources.list + .d/)
  • 会用 lsb_release -cs 查系统代号
  • 知道代号写错会导致系统出问题
  • 会备份源配置,会换成国内镜像
  • 会 apt update 后看输出来判断成没成
  • 知道 NO_PUBKEY 是缺密钥
  • 知道麒麟/UOS 的源不能和 Ubuntu 混用
  • 会查包里来自哪个源(apt-cache policy)

十、不行怎么办

Q1:apt update 全部超时

ping mirrors.aliyun.com      # 能通吗
curl -I https://mirrors.aliyun.com/ubuntu/   # HTTP 能通吗
  • 都不通 → 网络/防火墙问题,见 E4
  • HTTP 通但 apt 不通 → 可能配了代理但没生效,查 /etc/apt/apt.conf.d/ 里的 proxy 配置

Q2:404 Not Found 源地址里的代号或路径写错了。核对:

lsb_release -cs
# 然后和 sources.list 里的代号对比

也可能是系统已经 EOL(停止维护),官方源把它移到了 old-releases.ubuntu.com。

Q3:Release file is not valid yet 或 expired 系统时间不对。 证书校验依赖时间:

date
sudo timedatectl set-ntp true      # 开自动对时
# 或手动
sudo date -s "2026-10-20 18:00:00"

Q4:换了源之后系统各种奇怪报错 恢复备份:

sudo cp /etc/apt/sources.list.bak-20261020-180000 /etc/apt/sources.list
sudo apt update

这就是为什么要备份。

Q5:E: 无法修正错误,因为您要求某些软件包保持现状 依赖卡死了。先看它建议什么,通常是:

sudo apt --fix-broken install
# 或者按提示
sudo apt install -f

Q6:内网源要认证 apt 支持 basic auth:

deb http://用户名:密码@内网源地址/ubuntu jammy main

但密码明文存在文件里不安全。 更好的做法是用 /etc/apt/auth.conf:

machine 内网源域名
login 用户名
password 密码
sudo chmod 600 /etc/apt/auth.conf

Q7:apt update 报 Skipping acquire of configured file ... doesn't support architecture 'i386' 这只是警告,不影响使用。想消除就在源行里加 [arch=amd64]:

deb [arch=amd64] https://mirrors.aliyun.com/ubuntu/ jammy main

十一、一句话总结

装不上先查源:地址对不对、代号对不对、通不通、密钥有没有。

改源前先备份,改完 apt update 看输出。

麒麟/UOS 不混用 Ubuntu 源——这条能救命。


← 上一篇 T5|Git 下一篇 → T7|备份:别等丢了才想起这事