Linux系统 更新软件源

目前主流的Linux系统分Redhat系和Debian系,其中Debian系有Ubuntu、Kali、Debian等,Redhat系有CentOS、RHEL、OpenSUSE等。以下主要分析Ubuntu和Centos系统为例。

Ubuntu系统

Ubuntu在国内应用比较广泛,并且官网还一直提供免费的维护服务,因此Ubuntu的官方源可以直接访问下载软件,只不过可能访问速度比较慢,这里介绍如何将Ubuntu的官方源替换为国内镜像源。

源的构成

源列表语句构成:软件包格式 + 源地址 + 版本代号 + 组件分类

例如:deb http://mirrors.aliyun.com/ubuntu/ focal restricted

软件包构成

  • deb:表示这是一个二进制软件包的源,是.deb格式,适用于大多数用户。
  • deb-src:则表示是源代码包的源,适用于开发者或需要自己编译软件的人。

源地址

国内常用镜像还有清华、中科大、网易等。镜像源的URL地址,可以使用http、https或者ftp等协议。以下这个地址是阿里云的 Ubuntu 镜像站点。

示例:
http://mirrors.aliyun.com/ubuntu/

版本代号
ubuntu 每个 Ubuntu 版本都有一个代号,如 jammy 是 22.04,bionic 是 18.04。为了避免软件兼容差异性,最好是匹配对应的源。低版本系统使用高版本系统源,可以直接升级到高版本系统,但也存在着风险,会出现软件兼容性问题。

示例:
focal-updates

  • focal: 是 Ubuntu 20.04 LTS 的发行版代号(也称为“版本名称”),

  • -updates: 表示这是该版本的更新仓库,包含官方已经发布但尚未打入正式发行版本中的重要修复或增强更新。

组件分类

软件包的组件分类,Ubuntu 的软件分成多个组件,按开源/闭源以及许可方式分为四类:

  • main:完全开源并由 Ubuntu 官方支持。

  • restricted:非完全开源(例如某些驱动),但 Ubuntu 提供支持。

  • universe:社区维护,开源但不由 Ubuntu 官方提供技术支持。

  • multiverse:非自由软件或存在许可限制。

示例:
restricted

更新方法

ubuntu 源列表文件位置:/etc/apt/sources.list

1
2
3
4
5
6
7
8
9
10
11
12
13
# 进入源文件目录
cd /etc/apt/sources.list.d/

# 创建新的源文件或者在源文件末尾添加新的源
touch 01-source.list
或者
vim source.list

# 更新源列表
apt update

# 安装软件包
apt install <软件包名称>

源的架构

主流的架构有 x86_64 和 arm64、RISC-V这些架构。通常会在URL区分开,除x86_64架构,其他架构的在ubuntu-ports/目录中存放。
清华源网址。以下以清华镜像源参考,可直接复制到/etc/apt/sources.list中。
x86_64 架构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Ubuntu 22.04 x86_64
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse


Ubuntu 20.04 x86_64
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse

Ubuntu 18.04 x86_64
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse



arm64 架构
以下为中科大镜像源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Ubuntu 18.04 arm64 
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ bionic main restricted universe m ultiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ bionic main main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse


# Ubuntu 20.04 arm64
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ focal main main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse

# Ubuntu 22.04 arm64

# 默认注释了源码仓库,如有需要可自行取消注释
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse

CentOS 系统

CentOS 属于 RedHat 系的 Linux 发行版,广泛应用于服务器环境。CentOS 早期由社区维护,后来由 Red Hat 官方支持,但自 CentOS 8 之后逐步过渡为 CentOS Stream,因此建议新部署系统时使用 AlmaLinux 或 Rocky Linux 等兼容替代发行版。

软件源介绍

CentOS 使用 yum(CentOS 7)或 dnf(CentOS 8 及以后)包管理器,依赖 .repo 格式的源配置文件,位于:

1
/etc/yum.repos.d/

每个源文件以 .repo 结尾,定义了多个软件源仓库的地址。

常用国内镜像源

由于官方源服务器在国外,下载速度较慢,推荐使用国内镜像站,例如:

阿里云镜像
阿里源配置方法

以下源已经不支持 CentOS 7
清华大学镜像站

中科大镜像站

注意! 网上很多文章可能由于时效性,很多方法已经过时,最好直接访问镜像站获取最新信息

配置方法

使用curl或者wget命令下载并安装阿里云的yum源的repo文件,然后放入到/etc/yum.repos.d/目录下

  • CentOS 7 阿里云源配置 :

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 备份原始源
    sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

    # 下载阿里云的 CentOS-Base.repo 文件
    sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

    # 清除缓存并重新生成
    sudo yum clean all
    sudo yum makecache
    CentOS Stream(CentOS 8 之后)
    如果使用 CentOS Stream,可参考如下源替换:
  • CentOS Stream 8 阿里云镜像源

详细文件参考,文章内网址。

1
2
3
4
5
6
7
curl -o /etc/yum.repos.d/CentOS-Stream-AppStream.repo https://mirrors.aliyun.com/repo/Centos-8.repo

# 更新缓存
dnf clean all
dnf makecache
# 安装软件
yum install xxx

常见问题排查

1、 apt update 报 GPG 错误
GPG error: The following signatures couldn’t be verified because the public key is not available

1
2
3
4
5
6
sudo gpg --keyserver keyserver.ubuntu.com --recv BA034B64648B0073 //(这个公钥根据提示来写的)

sudo gpg --export --armor BA034B64648B0073 | sudo apt-key add -

sudo apt-get update

2、dpkg 锁被占用问题
Could not get lock /var/lib/dpkg/lock-frontend

1
2
3
4
5
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock*
sudo dpkg --configure -a
sudo apt update

3、镜像站点连接失败
检查网络和镜像源文件格式是否正确,根据提示修改。

4、无法安装特定软件包
软件不在当前启用的组件中,尝试加入 universe 或 multiverse;

对于 CentOS,可启用 epel:

1
2
# 安装 EPEL 扩展源,可以安装免费的软件包,例如minicom 等常见调试工具
sudo yum install epel-release