一、仓库是什么

仓库的本质:一个带索引的文件服务器

不管是 yum 还是 apt,软件仓库的本质都一样:

  • 一堆 .rpm/.deb 包文件
  • 一份描述这些包的元数据索引

包管理器先下载元数据(几十KB),在本地搜索/解析依赖,然后只下载需要的包。

二、yum/dnf 仓库

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[nginx-stable]
name=nginx stable repo
baseurl=https://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=https://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
  • name:仓库的名字

  • baseurl:仓库的url,一个有 repodata 目录的 url,存储元信息,支持本地URL,形如 file:///mnt/repo

  • $releasever:发行版版本,如 9

  • $basearch:架构,如 x86_64

  • gpgcheck:是否检查 GPG 密钥

  • enabled:是否启用仓库

  • gpgkey:gpg密钥,可以是本地文件、也可以是一个 URL.

这些变量可以通过一行 python 脚本获取

1
python3 -c "import dnf; db=dnf.Base(); print(db.conf.substitutions)"

三、APT 仓库

apt 仓库两种配置格式:

  • 单行格式(One-line)— 传统,兼容性好
  • DEB822 格式(.sources)— 新格式,更清晰

3.1 单行格式

1
2
3
4
5
6
7
8
9
10
11
12
13
# /etc/apt/sources.list (ubuntu 22.04)
# 语法:deb [options] URI Suite Component...
# URL 支持本地URL,形如 `file:///mnt/repo`
deb http://ports.ubuntu.com/ubuntu-ports/ jammy main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ jammy-updates main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ jammy universe
deb http://ports.ubuntu.com/ubuntu-ports/ jammy-updates universe
deb http://ports.ubuntu.com/ubuntu-ports/ jammy multiverse
deb http://ports.ubuntu.com/ubuntu-ports/ jammy-updates multiverse
deb http://ports.ubuntu.com/ubuntu-ports/ jammy-backports main restricted universe multiverse
deb http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ jammy-security universe
deb http://ports.ubuntu.com/ubuntu-ports/ jammy-security multiverse
Suite 含义
focal 发布时的原始版本,基本不动
focal-updates 发布后的 bug 修复更新(稳定后推送)
focal-security 安全漏洞修复,优先级最高、最快推送
focal-backports 从更新版 Ubuntu 移植回来的新版本软件,可选
Component 维护者 License
main Canonical 官方维护 全开源
restricted Canonical 官方维护 部分闭源(如 GPU 驱动)
universe 社区维护 全开源,但官方不保证
multiverse 社区维护 有版权限制的软件

3.2 DEB822 格式

DEB822 格式 更加清晰,变成了键值对的形式.

1
2
3
4
5
6
7
8
9
10
11
# /etc/apt/sources.list.d/ubuntu.sources (ubuntu 24.04)
Types: deb
URIs: http://ports.ubuntu.com/ubuntu-ports/
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Types: deb
URIs: http://ports.ubuntu.com/ubuntu-ports/
Suites: noble-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

四、update 和 upgrade

这两个命令在 APTYUM 有区别.

  1. YUM

几乎一致,只有在极少数情况下行为才不一致

1
2
yum update
yum upgrade
  1. APT

update = 更新索引,upgrade = 升级软件包

1
2
apt update
apt upgrade

五、常用命令

5.1 yum / dnf 常用命令

5.1.1 安装 / 卸载

1
2
3
4
5
yum install pkg              # 安装
yum install pkg-1.2.3 # 安装指定版本
yum reinstall pkg # 重新安装
yum remove pkg # 卸载(同时移除孤儿依赖)
yum autoremove # 清理不再需要的依赖包

5.1.2 更新

1
2
3
4
yum makecache                # 只刷新元数据索引,不升级包
yum update # 升级所有已安装的包
yum update pkg # 只升级某个包
yum check-update # 查看哪些包有可用更新(不安装)

5.1.3 搜索 / 查询

1
2
3
4
5
6
7
yum search keyword           # 按关键词搜索包名和描述
yum info pkg # 查看包详情(版本、大小、描述)
yum list installed # 列出所有已安装的包
yum list installed pkg # 查某个包是否已安装
yum --showduplicates list pkg # 显示该包所有可用版本
dnf repoquery --showduplicates pkg # 同上(dnf 写法)
yum provides /usr/bin/curl # 查某个文件/命令属于哪个包

5.1.4 仓库管理

1
2
3
4
5
6
yum repolist                 # 列出所有已启用的仓库
yum repolist all # 列出所有仓库(含禁用的)
dnf config-manager --add-repo URL # 添加仓库
dnf config-manager --enable repo-id # 启用仓库
dnf config-manager --disable repo-id # 禁用仓库(文件还在)
rm /etc/yum.repos.d/name.repo # 删除仓库

5.1.5 指定仓库安装

1
2
3
4
5
# 临时启用某仓库安装(不改配置文件)
yum --enablerepo=repo-id install pkg

# 只用指定仓库安装
yum --disablerepo='*' --enablerepo=repo-id install pkg

5.2 apt 常用命令

5.2.1 安装 / 卸载

1
2
3
4
5
6
apt install pkg              # 安装
apt install pkg=1.2.3 # 安装指定版本
apt install --reinstall pkg # 重新安装
apt remove pkg # 卸载(保留配置文件)
apt purge pkg # 卸载 + 删除配置文件
apt autoremove # 清理不再需要的依赖包

5.2.2 更新

1
2
3
4
apt update                   # 只刷新元数据索引,不升级包
apt upgrade # 升级所有包(保守,不删包)
apt full-upgrade # 升级所有包(可删旧包/装新依赖)
apt install --only-upgrade pkg # 只升级某个已安装的包

5.2.3 搜索 / 查询

1
2
3
4
5
6
7
8
apt search keyword           # 按关键词搜索
apt show pkg # 查看包详情
apt list --installed # 列出所有已安装的包
apt list --installed pkg # 查某个包是否已安装
apt-cache madison pkg # 显示该包所有可用版本及来源仓库
apt-cache policy pkg # 查看版本优先级和来源仓库
dpkg -L pkg # 列出已安装包的所有文件
dpkg -S /usr/bin/curl # 查某个文件属于哪个包

5.2.4 仓库管理

1
2
3
4
add-apt-repository ppa:user/name           # 添加 PPA 仓库
add-apt-repository --remove ppa:user/name # 删除 PPA 仓库
ls /etc/apt/sources.list.d/ # 查看所有第三方仓库
rm /etc/apt/sources.list.d/name.list # 删除仓库

5.2.5 禁用仓库

1
2
# 单行格式(.list):手动注释掉那一行
# DEB822 格式(.sources):添加 Enabled: no