Debian 设置软件源最常见也最推荐的方法是修改 /etc/apt/sources.list 文件。下面按步骤说明具体操作。

1. 编辑 /etc/apt/sources.list

使用文本编辑器以 root 权限打开 /etc/apt/sources.list 文件。常用的编辑器有 nano、vim、gedit 等。例如,使用 nano:

   sudo nano /etc/apt/sources.list

2. 添加或修改软件源

/etc/apt/sources.list 文件中的每一行代表一个软件源。其基本格式是:

   deb [选项] 软件源URL 发行版 组件1 组件2 ...
   deb-src 软件源URL 发行版 组件1 组件2 ...
  • deb:表示这是一个二进制软件包的软件源。
  • deb-src:表示这是一个源代码软件包的软件源(通常不需要)。
  • [选项]:可选参数,例如 arch=amd64,arm64 用于指定架构;也可以包含 signed-by=/usr/share/keyrings/debian.gpg 用于指定 GPG 密钥文件。
  • 软件源URL:软件源的网址。
  • 发行版:Debian 发行版的代号。例如:bookworm(Debian 12)、bullseye(Debian 11)、buster(Debian 10)。也可以使用 stable、oldstable、testing、unstable,但通常不推荐,因为它们会随着时间变化。
  • 组件:软件源的组件,通常有 main、contrib、non-free。
    • main:包含符合 Debian 自由软件指南(DFSG)的软件包。
    • contrib:包含符合 DFSG 的软件包,但依赖于 non-free 中的软件包。
    • non-free:包含不符合 DFSG 的软件包。

示例:Debian 12(Bookworm)的官方软件源

   deb http://deb.debian.org/debian bookworm main contrib non-free
   deb-src http://deb.debian.org/debian bookworm main contrib non-free

   deb http://security.debian.org/debian-security bookworm-security main contrib non-free
   deb-src http://security.debian.org/debian-security bookworm-security main contrib non-free

   deb http://deb.debian.org/debian bookworm-updates main contrib non-free
   deb-src http://deb.debian.org/debian bookworm-updates main contrib non-free

国内镜像源推荐

选择一个即可,不要全部添加。

清华大学(TUNA)

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

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

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

阿里云

       deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free
       deb-src http://mirrors.aliyun.com/debian/ bookworm main contrib non-free

       deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib/updates main
       deb-src http://mirrors.aliyun.com/debian-security bookworm-security main contrib/updates main

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

中科大

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

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

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

注意事项:删除或注释掉(在行首添加 #)不需要的软件源;选择一个离你地理位置较近的镜像源,可以提高下载速度;确保选择的软件源与你的 Debian 版本匹配。

3. 更新软件包列表

保存并关闭 /etc/apt/sources.list 文件后,运行以下命令更新软件包列表:

   sudo apt update

这个命令会从你配置的软件源下载软件包列表。

4. 可选:添加软件源的 GPG 密钥

某些第三方软件源可能需要你手动添加 GPG 密钥才能验证软件包的完整性。通常,软件源的提供者会提供添加密钥的说明。常见的做法是使用 wget 下载密钥,然后使用 apt-key add 添加密钥;或者,使用 gpg --dearmor | sudo tee /usr/share/keyrings/example.gpg > /dev/null 和 deb [signed-by=/usr/share/keyrings/example.gpg] ...。

例如:

   wget -qO - https://example.com/example.gpg | sudo apt-key add -

或者,更现代的方法:

   wget -qO - https://example.com/example.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/example.gpg > /dev/null

然后在 sources.list 中添加:

   deb [signed-by=/usr/share/keyrings/example.gpg] http://example.com/repo stable main

5. 升级软件包

更新软件包列表后,你可以升级已安装的软件包:

   sudo apt upgrade

或者,完全升级系统(包括删除旧软件包):

   sudo apt full-upgrade

其他注意事项

  • sources.list.d 目录:/etc/apt/sources.list.d/ 目录可以用来存放单独的软件源配置文件。每个文件以 .list 结尾。这使得管理软件源更加方便,特别是添加第三方软件源时。
  • apt-add-repository 命令:某些软件源提供 apt-add-repository 命令来自动添加软件源和密钥。但并非所有软件源都支持此命令,并且在 Debian 上可能需要安装 software-properties-common 包。
  • 错误处理:如果在更新软件包列表时遇到错误,请检查 /etc/apt/sources.list 文件中的软件源 URL 是否正确,以及是否缺少 GPG 密钥。
  • 不要混合使用不同 Debian 版本的软件源。这样做可能会导致系统不稳定。

小结

编辑 /etc/apt/sources.list 是 Debian 设置软件源的主要方法。选择合适的镜像源、更新软件包列表,并根据需要添加 GPG 密钥,就可以方便地安装和更新软件包。

— 感谢阅读 —

一起交流

分享你的思考,让讨论更进一步。