1. 前言
在使用基于Debian的操作系统,如Ubuntu,我们经常需要安装和更新软件。这些操作通常是通过APT(Advanced Package Tool)软件包管理器完成的,它利用了一系列定义在系统中的软件源(repositories)来安装和管理软件。
Ubuntu默认的软件安装源是 http://archive.ubuntu.com
,在国内访问速度慢。
Debian(Ubuntu是Debian的衍生品)的默认软件安装源是 http://deb.debian.org
,在国内访问速度慢。
如果换成国内的软件安装源,能够节省很多时间,因此安装Ubuntu/Debian系统后建议先更换软件安装源。
本文中,我们会学习如何在Ubuntu/Debian系统中更换软件安装源。
参考文档:
2. 备选国内源
- Ubuntu中国:http://cn.archive.ubuntu.com
- 中科大:https://mirrors.ustc.edu.cn
- 清华:https://mirrors.tuna.tsinghua.edu.cn
- 阿里:https://mirrors.aliyun.com
- 腾讯:https://mirrors.cloud.tencent.com
- 网易:https://mirrors.163.com
3. 什么是 DEB822 (.sources) 文件格式?
自新版本的 Debian 与 Ubuntu 起,例如:
- Debian 12 的容器镜像
- Ubuntu 24.04
默认预装的系统中 APT 的系统源配置文件不再是传统的 /etc/apt/sources.list。传统格式(又被称为 One-Line-Style 格式)类似如下:
1 | deb http://mirrors.ustc.edu.cn/debian/ bookworm main contrib |
新的 DEB822 格式自 APT 1.1(2015 年发布)起支持,后缀为 .sources,存储在 /etc/apt/sources.list.d/ 目录下,格式类似如下:
1 | Types: deb |
在切换软件源时,需要根据实际情况选择对应的格式进行修改。
关于 DEB822 格式的设计考虑,可以参考官方文档(英文)。
4. Ubuntu更换软件安装源
4.1. Ubuntu14.04
系统版本Ubuntu14.04(大版本别名trusty),计划换成Ubuntu中国的安装源。
1、备份现有的软件源列表
1 | sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup |
2、修改软件安装源配置
1 | sudo sed -i 's#http://archive.ubuntu.com#http://cn.archive.ubuntu.com#g' /etc/apt/sources.list |
也可以手动修改配置:
1 | vim /etc/apt/sources.list |
修改后的 sources.list 的内容为:
1 | deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse |
ubuntu14的别名是trusty,所以souces.list中出现了很多trusty,其他版本的ubuntu就不是trusty了。
3、更新安装源
1 | sudo apt-get update |
如果报错Error connecting: Could not connect: Connection refused
解决方法参考:
- “Unable to connect” error with apt-get?
- apt-get update Error connecting Could not connect Connection refused
4、更新软件
1 | sudo apt-get upgrade |
PS:其他版本的Ubuntu修改方法类似,主要是大版本别名的差别。
4.2. Ubuntu24.04
1 | sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak |
可能会收到GPG错误,显示没有公钥,或者公钥不可用。这是因为APT管理系统通过GPG密钥验证软件包的安全性。如果遇到这样的问题,须要导入对应软件源的公钥。
5. Debian更换软件安装源
5.1. Debian buster
1 | sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak |
5.2. Debian bookworm
1 | sudo sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources |
从 Debian 12 (bookworm) 开始,仓库添加了非自由固件组件 non-free-firmware。如果正在使用 bookworm, testing 或 sid,并且需要使用非自由固件,则在编辑配置时需要添加 non-free-firmware
。
详情参考 https://wiki.debian.org/Firmware 与 Debian bug #1030189。
1 | sudo sed -i 's/Components: main/Components: main contrib non-free non-free-firmware/g' /etc/apt/sources.list.d/debian.sources |
可能会收到GPG错误,显示没有公钥,或者公钥不可用。这是因为APT管理系统通过GPG密钥验证软件包的安全性。如果遇到这样的问题,须要导入对应软件源的公钥。
6. GPG错误解决办法
6.1. public key is not available
基于Docker运行新版本的Ubuntu和Debian,修改软件安装源之后,执行apt update
,可能会报错:
1 | The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 0E98404D386FA1D9 |
解决办法:
1 | apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0E98404D386FA1D9 |
或者:
1 | gpg --keyserver keyserver.ubuntu.com --recv-keys 0E98404D386FA1D9 |
6.2. 缺少gnupg
但是,执行apt-key
和gpg
命令,都需要依赖 gnupg
1 | apt install gnupg |
而想要安装gpupg需要先执行 apt update
,死循环了。。。
解决办法:
1 | apt update --allow-unauthenticated --allow-insecure-repositories |
6.3. Problem executing scripts APT
可能还会报错:
1 | E: Problem executing scripts APT::Update::Post-Invoke 'rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true' |
解决办法:
1 | sed -i -e 's/^APT/# APT/' -e 's/^DPkg/# DPkg/' /etc/apt/apt.conf.d/docker-clean |
6.4. 安装软件报错
重新更新软件安装源,安装gnupg
1 | apt update --allow-unauthenticated --allow-insecure-repositories |
报错:
1 | ... |
终极解决办法:升级Docker!升级Docker之后,GPG错误消失,后续所有问题也都不存在了。
7. 扩展:Ubuntu/Debian配置时间同步
1 | sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime |
如果是在容器中操作ntpdate time.windows.com
,可能会报错ntpdate[542]: step-systime: Operation not permitted
解决办法:启动容器时直接挂载宿主机的/etc/localtime
1 | docker run --name test -d \ |