Skip to content

常用包管理器对照 ​

同一件事在 RHEL 系和 Debian 系命令不同。这一节是速查表,外加离线安装和版本锁定这两个生产常用技能。

一、命令对照 ​

操作dnf / yum(RHEL 系)apt(Debian 系)
刷新索引dnf makecacheapt update
安装dnf install -y nginxapt install -y nginx
卸载(保留配置)dnf remove nginxapt remove nginx
卸载(含配置)—apt purge nginx
升级全部dnf updateapt upgrade
升级(处理依赖变更)—apt full-upgrade
搜索dnf search nginxapt search nginx
包信息dnf info nginxapt show nginx
已安装列表dnf list installeddpkg -l
文件属于哪个包rpm -qf /usr/sbin/nginxdpkg -S /usr/sbin/nginx
包包含哪些文件rpm -ql nginxdpkg -L nginx
清缓存dnf clean allapt clean
自动清理无用依赖dnf autoremoveapt autoremove

二、仓库配置 ​

RHEL 系 ​

bash
ls /etc/yum.repos.d/
text
rocky.repo  epel.repo  nginx.repo
ini
# /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
bash
dnf makecache
dnf repolist
text
repo id              repo name                        status
appstream            Rocky Linux 9 - AppStream        8,412
baseos               Rocky Linux 9 - BaseOS           4,921
epel                 Extra Packages for Enterprise    18,204
nginx-stable         nginx stable repo                42

Debian 系 ​

ini
# /etc/apt/sources.list.d/docker.list
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] \
    https://download.docker.com/linux/ubuntu jammy stable
bash
apt update
apt-cache policy docker-ce
text
docker-ce:
  Installed: (none)
  Candidate: 5:27.3.1-1~ubuntu.22.04~jammy

apt-cache policy 能看到候选版本和它来自哪个源,排查"为什么装的版本不对"很有用。

三、版本锁定 ​

生产上禁止"一升级就变了行为"。

RHEL 系 ​

bash
dnf install -y 'dnf-command(versionlock)'
dnf versionlock add nginx
dnf versionlock list
text
nginx-1:1.27.2-1.el9.*
bash
dnf versionlock delete nginx
dnf versionlock clear

Debian 系 ​

bash
apt-mark hold nginx
apt-mark showhold
text
nginx
bash
apt-mark unhold nginx

四、回滚 ​

RHEL 系用 dnf history ​

bash
dnf history
text
ID     | Command line             | Date and time    | Action(s)      | Altered
-----  | ------------------------ | ---------------- | -------------- | -------
    12 | install nginx            | 2026-10-27 23:40 | Install        |    5
    11 | update                   | 2026-10-27 22:10 | Upgrade        |   42 EE
bash
dnf history undo 11

undo 回滚指定事务,rollback 回滚到某次之后的状态。

生产上 undo 也可能出事

dnf history undo 11 会尝试把所有包降回旧版本,但不保证能拿到旧版本(上游可能已删除),也不处理配置文件变更。回滚前先在测试机演练。

五、离线安装 ​

下载依赖 ​

bash
# RHEL 系
dnf install --downloadonly --downloaddir=/tmp/rpms nginx

# Debian 系
apt-get install --download-only nginx
ls /var/cache/apt/archives/*.deb

在目标机安装 ​

bash
# RHEL 系
rpm -ivh /tmp/rpms/*.rpm

# Debian 系
dpkg -i /tmp/debs/*.deb
apt install -f          # 补依赖

rpm -ivh 的顺序问题

rpm 不解决依赖顺序,/tmp/rpms/*.rpm 里的包如果有相互依赖,可能报缺少依赖。用 dnf install /tmp/rpms/*.rpm(dnf 5+)或先 rpm -ivh --nodeps 再补——后者容易装出坏状态,慎用。

本地仓库 ​

bash
# 建索引
dnf install -y createrepo_c
createrepo_c /data/repo
ini
# /etc/yum.repos.d/local.repo
[local]
name=local repo
baseurl=file:///data/repo
gpgcheck=0
enabled=1

六、校验与排查 ​

bash
# 包是否被篡改(RHEL 系)
rpm -V nginx

# 找某个命令来自哪个包
command -v nginx
rpm -qf "$(command -v nginx)"

# 依赖关系
dnf repoquery --requires nginx
dnf repoquery --whatrequires openssl-libs

# Debian 系
dpkg -l | grep nginx
apt-cache depends nginx
apt-cache rdepends nginx
text
S.5....T.  c /etc/nginx/nginx.conf

rpm -V 的标记位:S 大小变、M 权限变、5 校验和变、T 时间变。带 c 的是配置文件(改了正常),不带 c 的变了就要警惕。

七、常见问题 ​

GPG 校验失败 ​

text
Public key for nginx-1.27.2-1.el9.x86_64.rpm is not installed
bash
rpm --import https://nginx.org/keys/nginx_signing.key
# 或
dnf install -y epel-release

源冲突 ​

text
package nginx-1.27.2-1.el9.x86_64 conflicts with nginx from nginx-stable

两个源提供同一个包。用 priority(dnf 插件)或 exclude 解决:

ini
[baseos]
exclude=nginx*

依赖地狱 ​

text
Error: Problem: cannot install the best candidate for the job

先看完整输出里的 "problem" 段,通常是某个包被锁定了版本:

bash
dnf versionlock list
dnf distro-sync

distro-sync 把已装包同步到仓库里的版本,能解决一部分版本错位。

八、踩过的坑 ​

现象根因处置
装了但命令找不到包名 ≠ 命令名dnf provides '*/nginx'
版本比预期旧发行版源版本旧加官方源
GPG 校验失败公钥没导入rpm --import
升级后服务起不来配置格式变了锁版本,先看 changelog
离线安装缺依赖只下了主包--downloadonly 会带依赖
apt update 报 GPG error密钥过期/换地址更新 keyring
dnf update 把内核升了默认包含 kernel--exclude=kernel*
磁盘被缓存占满旧包没清dnf clean all / apt clean