Linux软件包管理

作者:

在

背景

linus喜欢使用的linux发行版:fedora

【官方双语】真·Linus来了! – 为Linux之父Linus Torvalds装机 / 两莱一机#linus谈科技】 https://www.bilibili.com/video/BV1ueS4BUEEn/?share_source=copy_web&vd_source=9d01159cb1b6afa8b969782aea8cb4c8

软件包

源码包(Source Package)
├── 人类可读的源代码
├── 需要编译工具转换
├── 优点:可定制、性能优化
└── 缺点:编译、依赖复杂

二进制包(Binary Package)
├── 已经编译好的机器码(指令集和操作系统)
├── 直接安装即可使用
├── 优点:安装快、依赖自动解决(yum)
└── 缺点:版本固定、灵活性较低

生活中的类比:

点外卖:商家做好送上门 → 二进制包(直接安装使用)

自己做饭:买菜→洗切→烹饪 → 源码包(需要编译)

rockylinux中包格式

RPM(Red Hat Package Manager)
├── 文件名格式:软件名-版本号-发布次数.架构.rpm
├── 示例:nginx-1.20.1-10.el9.x86_64.rpm
│          ├── nginx      → 软件名称
│          ├── 1.20.1     → 主版本号.次版本号.修订号
│          ├── 10.el9     → 第10次发布,适配Rocky9
│          └── x86_64     → 64位系统
└── 特点:红帽系专用(RHEL/CentOS/Rocky/Alma)

rocky中的管理工具变化

RPM时代(90年代)
└── rpm -ivh package.rpm
    └── 问题:不会自动解决依赖,被称为"依赖地狱"

YUM时代(2003-2020)
└── yum install package
    └── 改进:自动解决依赖关系
    └── 基于Python2开发

DNF时代(2020-至今)
└── dnf install package
    └── 改进:性能更好、依赖解析更快
    └── 基于Python3开发

rocky9.7中所使用的yum其实就是dnf,所以大家到底使用dnf还是yum,根据自己喜好来

[root@localhost ~]# which yum
/usr/bin/yum
[root@localhost ~]# ls -al /usr/bin/yum
lrwxrwxrwx. 1 root root 5 11月  3  2025 /usr/bin/yum -> dnf-3
[root@localhost ~]# which dnf
/usr/bin/dnf
[root@localhost ~]# ls -al /usr/bin/dnf
lrwxrwxrwx. 1 root root 5 11月  3  2025 /usr/bin/dnf -> dnf-3

image-20260724175923374

命令实战

# 查看版本,dnf是前台工具,软件安装还是依靠rpm命令
[root@localhost ~]# dnf --version
4.14.0
  Installed: dnf-0:4.14.0-31.el9.noarch at Wed 17 Jun 2026 07:39:51 AM GMT
  Built    : Rocky Linux Build System (Peridot) <releng@rockylinux.org> at Mon 03 Nov 2025 04:06:26 AM GMT

  Installed: rpm-0:4.16.1.3-39.el9.x86_64 at Wed 17 Jun 2026 07:39:39 AM GMT
  Built    : Rocky Linux Build System (Peridot) <releng@rockylinux.org> at Fri 31 Oct 2025 01:43:53 AM GMT

查找软件包

# 搜索想要安装的工具包,一般是自己想要安装一个工具包,但是不确定repo仓库中有没有,可以先搜索一下
[root@localhost ~]# dnf search nginx
Rocky Linux 9 - BaseOS                                                                                              6.5 MB/s |  11 MB     00:01
Rocky Linux 9 - AppStream                                                                                           6.5 MB/s |  15 MB     00:02
Rocky Linux 9 - Extras                                                                                              7.1 kB/s |  17 kB     00:02
=========================================================== Name Exactly Matched: nginx ============================================================
nginx.x86_64 : A high performance web server and reverse proxy server
========================================================== Name & Summary Matched: nginx ===========================================================
nginx-all-modules.noarch : A meta package that installs all available Nginx modules
nginx-core.x86_64 : nginx minimal core
nginx-filesystem.noarch : The basic directory layout for the Nginx server
nginx-mod-http-image-filter.x86_64 : Nginx HTTP image filter module
nginx-mod-http-perl.x86_64 : Nginx HTTP perl module
nginx-mod-http-xslt-filter.x86_64 : Nginx XSLT module
nginx-mod-mail.x86_64 : Nginx mail modules
nginx-mod-stream.x86_64 : Nginx stream modules
pcp-pmda-nginx.x86_64 : Performance Co-Pilot (PCP) metrics for the Nginx Webserver

# 查看软件包具体信息,重点是版本信息
[root@localhost ~]# dnf info nginx
Last metadata expiration check: 0:00:37 ago on Mon 20 Jul 2026 03:58:37 PM CST.
Available Packages
Name         : nginx
Epoch        : 2
Version      : 1.20.1
Release      : 28.el9_8.2.rocky.0.1
Architecture : x86_64
Size         : 37 k
Source       : nginx-1.20.1-28.el9_8.2.rocky.0.1.src.rpm
Repository   : appstream
Summary      : A high performance web server and reverse proxy server
URL          : https://nginx.org
License      : BSD
Description  : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
             : IMAP protocols, with a strong focus on high concurrency, performance and low
             : memory usage.

# 通过配置文件路径反查软件包名称 
[root@localhost ~]# dnf provides /etc/ssh/sshd_config
Last metadata expiration check: 0:03:58 ago on Mon 20 Jul 2026 03:58:37 PM CST.
openssh-server-8.7p1-46.el9.rocky.0.1.x86_64 : An open source SSH server daemon
Repo        : @System
Matched from:
Filename    : /etc/ssh/sshd_config

openssh-server-9.9p1-7.el9_8.rocky.0.1.x86_64 : An open source SSH server daemon
Repo        : baseos
Matched from:
Filename    : /etc/ssh/sshd_config

查看已经安装的包

# 查看所有可用包(数量巨大,建议配合grep)
dnf list | head -20          # 只看前20行
dnf list | grep mysql     # 只看MySQL相关的包

# 查看已经安装的包
dnf list installed | head -20

安装软件包

# 安装nginx(-y表示自动确认所有提示)
dnf install -y nginx

# 安装过程中的输出解读:
# Dependencies resolved.  → 依赖关系已解决
# Package    Architecture  Version  Repository  Size
# Installing:  → 开始安装
# Complete!  → 安装完成
# 重点留意依赖部分
[root@localhost ~]# dnf install nginx
Last metadata expiration check: 0:11:42 ago on Mon 20 Jul 2026 03:58:37 PM CST.
Dependencies resolved.
====================================================================================================================================================
 Package                              Architecture              Version                                          Repository                    Size
====================================================================================================================================================
Installing:
 nginx                                x86_64                    2:1.20.1-28.el9_8.2.rocky.0.1                    appstream                     37 k
Installing dependencies:
 nginx-core                           x86_64                    2:1.20.1-28.el9_8.2.rocky.0.1                    appstream                    568 k
 nginx-filesystem                     noarch                    2:1.20.1-28.el9_8.2.rocky.0.1                    appstream                     11 k
 rocky-logos-httpd                    noarch                    90.17-1.el9                                      appstream                     24 k

Transaction Summary
====================================================================================================================================================
Install  4 Packages

Total download size: 639 k
Installed size: 1.8 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): nginx-filesystem-1.20.1-28.el9_8.2.rocky.0.1.noarch.rpm                                                       31 kB/s |  11 kB     00:00
(2/4): nginx-1.20.1-28.el9_8.2.rocky.0.1.x86_64.rpm                                                                  81 kB/s |  37 kB     00:00
(3/4): rocky-logos-httpd-90.17-1.el9.noarch.rpm                                                                     139 kB/s |  24 kB     00:00

(4/4): nginx-core-1.20.1-28.el9_8.2.rocky.0.1.x86_64.rpm                                                            670 kB/s | 568 kB     00:00

----------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                               378 kB/s | 639 kB     00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                            1/1
  Running scriptlet: nginx-filesystem-2:1.20.1-28.el9_8.2.rocky.0.1.noarch                                                                      1/4
  Installing       : nginx-filesystem-2:1.20.1-28.el9_8.2.rocky.0.1.noarch                                                                      1/4
  Installing       : nginx-core-2:1.20.1-28.el9_8.2.rocky.0.1.x86_64                                                                            2/4
  Installing       : rocky-logos-httpd-90.17-1.el9.noarch                                                                                       3/4
  Installing       : nginx-2:1.20.1-28.el9_8.2.rocky.0.1.x86_64                                                                                 4/4
  Running scriptlet: nginx-2:1.20.1-28.el9_8.2.rocky.0.1.x86_64                                                                                 4/4
  Verifying        : nginx-2:1.20.1-28.el9_8.2.rocky.0.1.x86_64                                                                                 1/4
  Verifying        : nginx-core-2:1.20.1-28.el9_8.2.rocky.0.1.x86_64                                                                            2/4
  Verifying        : nginx-filesystem-2:1.20.1-28.el9_8.2.rocky.0.1.noarch                                                                      3/4
  Verifying        : rocky-logos-httpd-90.17-1.el9.noarch                                                                                       4/4

Installed:
  nginx-2:1.20.1-28.el9_8.2.rocky.0.1.x86_64 nginx-core-2:1.20.1-28.el9_8.2.rocky.0.1.x86_64 nginx-filesystem-2:1.20.1-28.el9_8.2.rocky.0.1.noarch
  rocky-logos-httpd-90.17-1.el9.noarch

Complete!

安装多个软件和重新软件

# 同时安装多个包,空格分隔
dnf install -y vim wget curl tree

# 重新安装软件
dnf reinstall nginx

==离线安装==

# 在线情况下先下载所有软件包及其依赖
# 必须增加--resolve参数,否则,仅下载nginx主包,依赖包不会下载,断网环境还是无法安装
dnf download nginx --resolve --destdir=./nginx

# 断网环境下安装,可使用nmcli conn down ens160断网
dnf install -y ./nginx/*

几乎用不到,不用看,遇到了再说,一般都联网

更新软件包

# 更新单个软件到最新版本
dnf update nginx

# 更新所有软件到最新版本(生产环境慎用,容易裂开)
dnf update

image-20260724200717357


卸载软件包

# 卸载nginx(会同时删除依赖它的包,仅针对通过依赖自动安装的,手动安装的不算依赖)
dnf remove nginx   
# 模拟卸载(看会删什么,但实际不执行)
dnf remove --assumeno nginx
[root@localhost ~]# dnf remove nginx --assumeno
Dependencies resolved.
====================================================================================================================================================
 Package                             Architecture              Version                                          Repository                     Size
====================================================================================================================================================
Removing:
 nginx                               x86_64                    2:1.20.1-28.el9_8.2.rocky.0.1                    @appstream                    147 k
Removing unused dependencies:
 nginx-core                          x86_64                    2:1.20.1-28.el9_8.2.rocky.0.1                    @appstream                    1.6 M
 nginx-filesystem                    noarch                    2:1.20.1-28.el9_8.2.rocky.0.1                    @appstream                    141

Transaction Summary
====================================================================================================================================================
Remove  3 Packages

Freed space: 1.7 M
Operation aborted.

# 卸载后清理不再需要的依赖(生产环境慎用,容易裂开)
dnf autoremove 

讲解: image-20260724201438498

历史记录

[root@localhost ~]# dnf history

ID     | Command line                                                                                  | Date and time    | Action(s)      | Altered

----------------------------------------------------------------------------------------------------------------------------------------------------
    12 | install nginx                                                                                 | 2026-07-20 16:43 | Install        |    3
    11 | remove nginx-core nginx-filesystem                                                            | 2026-07-20 16:42 | Removed        |    2
    10 | remove git                                                                                    | 2026-07-20 16:41 | Removed        |    2
     9 | remove nginx                                                                                  | 2026-07-20 16:41 | Removed        |    1
     8 | install ./git/git-2.52.0-1.el9.x86_64.rpm ./git/git-core-2.52.0-1.el9.x86_64.rpm ./git/git-co | 2026-07-20 16:28 | Install        |   65
     7 | install -y ./nginx/nginx-1.20.1-28.el9_8.2.rocky.0.1.x86_64.rpm ./nginx/nginx-core-1.20.1-28. | 2026-07-20 16:23 | Install        |    3

# 查看某次操作的详细信息
dnf history info 12

# 撤销某次操操作
dnf history undo 12

group

[root@localhost ~]# dnf grouplist
Last metadata expiration check: 0:06:14 ago on Mon 20 Jul 2026 05:09:17 PM CST.
Available Environment Groups:
   Server with GUI
   Server
   Workstation
   KDE Plasma Workspaces
   Custom Operating System
   Virtualization Host
Installed Environment Groups:
   Minimal Install
Available Groups:
   Fedora Packager
   VideoLAN Client
   Xfce
   Legacy UNIX Compatibility
   Console Internet Tools
   Container Management
   Development Tools
   .NET Development
   Graphical Administration Tools
   Headless Management
   Network Servers
   RPM Development Tools
   Scientific Support
   Security Tools
   Smart Card Support
   System Tools


dnf groupinstall "Development Tools"  # --assumeno 可以使用此参数看看能安装哪些

==注意==:用不到不用记,平时用服务器,一定是用到什么装什么软件,不用装很多

==总结==:

image-20260724210652717

image-20260724210802972

==注意==:这是全部开发工具


yum源管理

软件仓库(Repository)
├── 本质:一个存放了大量RPM包和元数据的网络服务器
├── dnf的工作流程:
│   └── 1. 读取 /etc/yum.repos.d/ 下的 .repo 配置文件
│   └── 2. 连接仓库服务器,下载包,元数据(有哪些包、版本、依赖)
│   └── 3. 本地缓存元数据到 /var/cache/dnf/
│   └── 4. 根据用户命令查找、解析依赖、下载安装
└── 默认仓库


    ├── BaseOS → 核心系统包(内核、glibc等)
    ├── AppStream → 应用软件包(nginx、httpd等)
    └── Extras → 额外包(非核心但有用)

image-20260724220852265

image-20260724220941393

查看当前仓库信息

# 查看已启用的仓库列表
dnf repolist

# 输出示例:
# repo id            repo name
# appstream          Rocky Linux 9 - AppStream
# baseos             Rocky Linux 9 - BaseOS
# extras             Rocky Linux 9 - Extras

# 查看仓库配置文件位置
ls -l /etc/yum.repos.d/
-rw-r--r--. 1 root root 1627 Jul 20 10:00 rocky.repo
-rw-r--r--. 1 root root  636 Jul 20 10:00 rocky-addons.repo

# 查看某个仓库文件的内容
cat /etc/yum.repos.d/rocky.repo

*.repo文件格式**

[baseos]                          # 仓库ID(唯一标识)
name=Rocky Linux 9 - BaseOS      # 仓库名称(给人看的)
baseurl=http://mirror.rocky...   # 软件包的实际下载地址
mirrorlist=https://mirrors...    # 镜像列表(与baseurl二选一)
enabled=1                         # 1=启用,0=禁用
gpgcheck=1                        # 1=校验签名,0=不校验
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-9  # 签名公钥

image-20260724222315665

image-20260724222701804

==实现==:

image-20260724224719869

换源——让速度更快

# 创建备份目录
mkdir -p /root/repo_backup

# 备份所有repo文件
cp /etc/yum.repos.d/*.repo /root/repo_backup/

# 使用sed命令批量替换
sed -e 's|^mirrorlist=|#mirrorlist=|g' 
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.ustc.edu.cn/rocky|g' 
    -i.bak /etc/yum.repos.d/rocky*.repo

# 重建缓存
dnf makecache

==注意==:具体可以用中科大的镜像源Rockylinux的软件库,这样下载速度就快点,一般换不换都差不多其实,可以看科大镜像源的网站看方法


image-20260726231802914

image-20260726231818337

实现:

image-20260724232049725

image-20260724232800190

image-20260724232821163

添加epel

官方软件源里的包数量相对有限,版本也偏旧,主要包含经过严格测试的核心软件,EPEL 的出现就是为了填补这个空白。它由 Fedora 社区维护,提供的软件包大多来自 Fedora 仓库,质量有保障,并且与 RHEL 系统兼容,不会与官方源里的包发生冲突或覆盖。这让系统管理员既能获得系统的稳定性,又能用 yum 或 dnf 命令方便地安装 Nginx、htop、ImageMagick 等常用工具

# 安装epel-release包(自动配置好EPEL仓库)
dnf install -y epel-release

# 查看添加后的仓库
dnf repolist

# 现在可以安装EPEL专属的软件了
dnf install -y htop

htop #更帅的TUI

image-20260724233702663

htop更好用

image-20260724233917216

编译安装

思考题:生产环境中,运维接到需求”必须在服务器上部署Nginx 1.25.x版本,且必须开启HTTP/3支持”。但官方仓库只有1.20版本,没有HTTP/3功能。怎么办?Support for QUIC and HTTP/3

或者仓库中仅有pthon3.14.5,我想用3.14.6,甚至我想用prerelease的版本,怎么办?

img

解决方案:

​ 1. 等,等6-12个月,等着新的版本发布后,使用dnf update xxx

​ 2. 找这个软件官方提供的repo,比如nginx的nginx: Linux packages

​ 3. 找源代码,自己编译一份二进制可执行文件

什么时候必须用源码编译?

场景说明示例
需要新版特性官方仓库版本太老需要python预发布,但仓库只有最新稳定版
需要定制功能官方包默认不开启某些特性需要开启 –with-http_v3_module
安全审计要求需要用特定补丁版本安全团队指定CVE补丁版本

编译环境准备

编译环境准备,其实完全可以粗略的理解为,此源代码开发环境的准备,所有的源代码运行环境和依赖都要准备好,才能进入编译环节。

工具作用
gccC语言编译器
g++C++语言编译器
make自动化构建工具

以上c语言编译经常遇到的,了解即可。

一般c项目的编译流程

源码编译"三步走"战略:

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│ ./configure │ ──▶ │    make     │ ──▶│ make install│
└─────────────┘     └─────────────┘     └─────────────┘
      ▲                   ▲                   ▲
      │                   │                   │
   检查环境(配置功能)      编译代码            安装到系统
    生成Makefile         生成可执行文件          指定路径放置

image-20260725085625609

==总结==:

image-20260725090145440

image-20260725114332588

编译代码雨

cmatrix-2.0.tar.gz

GitHub – abishekvashok/cmatrix: Terminal based “The Matrix” like implementation · GitHub

mkdir cmatrix
tar xvf cmatrix-2.0.tar.gz -C ./cmatrix
cd cmatrix/cmatrix-2.0
dnf install gcc make autoconf ncurses-devel automake
autoreconf -i
./configure  # 检查环境并生成Makefile供make使用
make -j 4 #使用4个核心并行编译
# 如果正常,此时将获得cmatrix二进制文件,./cmatrix便可以运行了
make clean # 可以清除make产生的中间文件

image-20260726212806196

image-20260726212828644

image-20260726212852299


编译小火车

sl-5.02.tar.gz

GitHub – mtoyoda/sl: SL(1): Cure your bad habit of mistyping · GitHub

# 进入源代码目录,直接make即可
[root@localhost sl-5.02]# ls
LICENSE  Makefile  README.ja.md  README.md   sl.1  sl.1.ja  sl.c  sl.h

[root@localhost sl-5.02]# make
gcc -O -o sl sl.c -lncurses

[root@localhost sl-5.02]# ./sl

编译python(拓展部分,学有余力可尝试)

Download Python | Python.org 从此网站下载python源代码

sudo dnf install 
      gcc gcc-c++ gdb lzma glibc-devel libstdc++-devel openssl-devel 
      readline-devel zlib-devel libzstd-devel libffi-devel bzip2-devel 
      xz-devel sqlite sqlite-devel sqlite-libs libuuid-devel gdbm-libs 
      perf expat expat-devel mpdecimal python3-pip

cd /root/Python-3.15.0b4
./configure # 不加参数,默认配置就行
make -j 8   # 8核心情况下,大概1分钟左右
ls -al python  # 便可以获得python二进制
[root@localhost Python-3.15.0b4]# ./python  # 一个repo中不存在的python我们就得到了
Python 3.15.0b4 (main, Jul 20 2026, 19:21:56) [GCC 11.5.0 20240719 (Red Hat 11.5.0-14)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

二进制文件与软件包管理

image-20260726204108920

image-20260726204124080

image-20260726204141064

==稳定与研发新特性==:

image-20260726234921757

image-20260726234935967