Debian 更换国内源详细教程

在国内使用 Debian 系统时,默认的官方源由于服务器在国外,下载和更新软件包的速度通常较慢。为了提升下载速度,建议将软件源更换为国内的镜像源(如阿里云、清华大学、中科大等)。

以下是详细的操作步骤。

1. 确认 Debian 版本

不同版本的 Debian 对应的代号不同,配置源文件时需要使用正确的代号。首先查看当前系统的版本代号:

打开终端,输入以下命令:

lsb_release -a

或者查看版本文件:

cat /etc/os-release

常见的版本代号如下:

  • Debian 12 (Bookworm): 代号为 bookworm (当前稳定版)
  • Debian 11 (Bullseye): 代号为 bullseye
  • Debian 10 (Buster): 代号为 buster

请记住你的版本代号,后面配置时会用到。

2. 备份原有源文件

在修改配置文件之前,强烈建议先备份原有的源列表文件,以防配置出错后无法恢复。

在终端执行以下命令:

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

3. 修改源文件

使用文本编辑器(如 nanovim)打开源文件:

sudo nano /etc/apt/sources.list

打开后,你会看到原有的官方源地址。建议删除所有内容或注释掉(在行首加 #,然后复制以下适合你版本的镜像源内容粘贴进去。

推荐国内镜像源列表

以下配置默认包含主包、更新包和安全更新包。请根据你的 Debian 版本选择对应的配置。

注意:以下示例均以 Debian 12 (Bookworm) 为例。如果你使用的是 Debian 11,请将文中的 bookworm 替换为 bullseye

方案一:阿里云源(推荐,速度快且稳定)

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

方案二:清华大学源(教育网推荐)

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware

方案三:中科大源(USTC)

deb https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb-src https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb-src https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.ustc.edu.cn/debian-security/ bookworm-security main contrib non-free non-free-firmware
deb-src https://mirrors.ustc.edu.cn/debian-security/ bookworm-security main contrib non-free non-free-firmware

关于 non-free-firmware 的说明: 从 Debian 12 (Bookworm) 开始,官方新增了 non-free-firmware 组件用于存放非自由固件包(如显卡驱动、WiFi驱动)。如果你使用的是 Debian 11 或更早版本,请删除配置中的 non-free-firmware 字段,否则会报错。

4. 保存并退出

如果你使用的是 nano 编辑器:

  1. Ctrl + O 保存修改。
  2. Enter 确认文件名。
  3. Ctrl + X 退出编辑器。

如果你使用的是 vim 编辑器:

  1. Esc 退出编辑模式。
  2. 输入 :wq 并按 Enter 保存退出。

5. 更新软件列表

修改完源文件后,需要更新软件包列表以使更改生效。执行以下命令:

sudo apt update

如果输出结果中没有报错(Error),且显示“正在读取状态信息... 完成”,则说明源配置成功。

6. 升级软件(可选)

如果你想将系统中的软件包升级到最新版本,可以执行:

sudo apt upgrade

常见问题

Q: 执行 apt update 时提示“无法验证签名”怎么办? A: 这通常是因为长时间未更新或系统时间不对。尝试安装 debian-archive-keyring

sudo apt install debian-archive-keyring

Q: 我该如何恢复原来的源? A: 执行以下命令恢复备份:

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