Debian12更换国内apt源完整命令步骤 Linux

前言

Debian12(Bookworm)默认使用国外官方源,国内服务器或桌面访问经常很慢、超时,apt update 拉取索引耗时长甚至失败。Debian 的源配置和 Ubuntu 略有区别,仓库名是 bookworm、bookworm-updates、bookworm-security,写错版本代号会直接报错。本文给出备份、替换为阿里云源、更新索引、验证可用性的完整命令,适合新装机和存量服务器。

操作步骤

1. 备份原源配置

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

2. 查看系统版本代号

cat /etc/debian_version
lsb_release -cs

Debian12 代号为 bookworm,换源时必须对应,不能写成 bullseye(11)。

3. 写入阿里云 bookworm 源

sudo vi /etc/apt/sources.list

清空原有内容,写入:

deb https://mirrors.aliyun.com/debian/ bookworm main non-free-firmware contrib non-free
deb https://mirrors.aliyun.com/debian/ bookworm-updates main non-free-firmware contrib non-free
deb https://mirrors.aliyun.com/debian-security/ bookworm-security main non-free-firmware contrib non-free

4. 更新索引并升级

sudo apt update
sudo apt upgrade -y

5. 验证源是否生效

sudo apt-cache policy | head -20

输出里仓库地址应显示为 mirrors.aliyun.com。

6. 测试安装常用工具

sudo apt install -y curl vim net-tools

能正常下载即说明换源成功。

常见坑

  1. 版本代号写错,bookworm 写成 bookworm-updates 或沿用旧代号,报 404。
  2. 只换了主仓库,漏写 bookworm-security,安全更新拉不到。
  3. Debian12 默认包含 non-free-firmware 组件,漏写会导致部分驱动包找不到。
  4. 云服务器本身有内网镜像,公网阿里源不是最优,可查云厂商内网源。
  5. 改完没有 apt update,旧索引仍指向国外源。
  6. 同时存在 sources.list.d 下的第三方源文件,个别源仍超时,需单独处理。

总结

Debian12 换源核心是备份 sources.list、把仓库代号准确写成 bookworm、主仓库与安全仓库一起替换。换源后先 apt update 看速度和报错,再安装工具验证。长期使用建议固定一个稳定国内镜像,避免频繁切换造成缓存不一致。

遇到问题加QQ23979811 协助处理


emer 发布于  2026-11-14 07:08 
'); })();