一个计算机技术爱好者与学习者

0%

好好学Linux:Ubuntu/Debian更换软件安装源

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. 备选国内源

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
2
3
4
5
Types: deb
URIs: https://mirrors.ustc.edu.cn/debian
Suites: bookworm
Components: main contrib
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

在切换软件源时,需要根据实际情况选择对应的格式进行修改。

关于 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
##測試版源
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse
# 源碼
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
##測試版源
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse
# Canonical 合作夥伴和附加
# deb http://archive.canonical.com/ubuntu/ trusty partner
# deb http://extras.ubuntu.com/ubuntu/ trusty main

ubuntu14的别名是trusty,所以souces.list中出现了很多trusty,其他版本的ubuntu就不是trusty了。

3、更新安装源

1
sudo apt-get update

如果报错Error connecting: Could not connect: Connection refused
解决方法参考:

4、更新软件

1
sudo apt-get upgrade

PS:其他版本的Ubuntu修改方法类似,主要是大版本别名的差别。

4.2. Ubuntu24.04

1
2
3
sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak
sudo sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/ubuntu.sources
sudo apt update

可能会收到GPG错误,显示没有公钥,或者公钥不可用。这是因为APT管理系统通过GPG密钥验证软件包的安全性。如果遇到这样的问题,须要导入对应软件源的公钥。

5. Debian更换软件安装源

5.1. Debian buster

1
2
3
4
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo sed -i 's#http://deb.debian.org#http://mirrors.tuna.tsinghua.edu.cn#g' /etc/apt/sources.list
sudo sed -i 's#http://security.debian.org/debian-security#http://mirrors.tuna.tsinghua.edu.cn/debian-security#g' /etc/apt/sources.list
sudo apt-get update

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/FirmwareDebian 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
2
gpg --keyserver  keyserver.ubuntu.com --recv-keys 0E98404D386FA1D9
gpg --export --armor 0E98404D386FA1D9 | sudo apt-key add -

6.2. 缺少gnupg

但是,执行apt-keygpg命令,都需要依赖 gnupg

1
apt install gnupg

而想要安装gpupg需要先执行 apt update,死循环了。。。

解决办法:

1
apt update --allow-unauthenticated --allow-insecure-repositories

6.3. Problem executing scripts APT

可能还会报错:

1
2
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'
E: Sub-process returned an error code

解决办法:

1
sed -i -e 's/^APT/# APT/' -e 's/^DPkg/# DPkg/' /etc/apt/apt.conf.d/docker-clean

6.4. 安装软件报错

重新更新软件安装源,安装gnupg

1
2
apt update --allow-unauthenticated --allow-insecure-repositories
apt install --allow-unauthenticated -y gnupg

报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
dpkg-deb (subprocess): decompressing archive '/tmp/apt-dpkg-install-0nckSy/24-libsasl2-modules_2.1.28+dfsg-10_amd64.deb' (size=66596) member 'control.tar': lzma error: Cannot allocate memory
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
dpkg-deb: error: tar subprocess returned error exit status 2
dpkg: error processing archive /tmp/apt-dpkg-install-0nckSy/24-libsasl2-modules_2.1.28+dfsg-10_amd64.deb (--unpack):
dpkg-deb --control subprocess returned error exit status 2
Errors were encountered while processing:
/tmp/apt-dpkg-install-0nckSy/00-readline-common_8.2-1.3_all.deb
/tmp/apt-dpkg-install-0nckSy/01-libassuan0_2.5.5-5_amd64.deb
/tmp/apt-dpkg-install-0nckSy/02-libreadline8_8.2-1.3_amd64.deb
/tmp/apt-dpkg-install-0nckSy/03-gpgconf_2.2.40-1.1_amd64.deb
/tmp/apt-dpkg-install-0nckSy/04-libksba8_1.6.3-2_amd64.deb
/tmp/apt-dpkg-install-0nckSy/05-libsasl2-modules-db_2.1.28+dfsg-10_amd64.deb
/tmp/apt-dpkg-install-0nckSy/06-libsasl2-2_2.1.28+dfsg-10_amd64.deb
/tmp/apt-dpkg-install-0nckSy/07-libldap-2.5-0_2.5.13+dfsg-5_amd64.deb
/tmp/apt-dpkg-install-0nckSy/08-libnpth0_1.6-3_amd64.deb
/tmp/apt-dpkg-install-0nckSy/09-dirmngr_2.2.40-1.1_amd64.deb
/tmp/apt-dpkg-install-0nckSy/10-gnupg-l10n_2.2.40-1.1_all.deb
/tmp/apt-dpkg-install-0nckSy/11-gnupg-utils_2.2.40-1.1_amd64.deb
/tmp/apt-dpkg-install-0nckSy/12-libsqlite3-0_3.40.1-2_amd64.deb
/tmp/apt-dpkg-install-0nckSy/13-gpg_2.2.40-1.1_amd64.deb
/tmp/apt-dpkg-install-0nckSy/14-libncursesw6_6.4-4_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

终极解决办法:升级Docker!升级Docker之后,GPG错误消失,后续所有问题也都不存在了。

7. 扩展:Ubuntu/Debian配置时间同步

1
2
3
4
5
6
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
sudo echo "Asia/Shanghai" > /etc/timezone
date
sudo apt install ntpdate
sudo ntpdate time.windows.com
date

如果是在容器中操作ntpdate time.windows.com,可能会报错ntpdate[542]: step-systime: Operation not permitted
解决办法:启动容器时直接挂载宿主机的/etc/localtime

1
2
3
docker run --name test -d \
-v /etc/localtime:/etc/localtime \
python:3.7.10-slim-buster sleep 7200