wiki/运维/Linux/笔记/22.软件管理.md
2026-04-08 19:40:00 +08:00

150 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 1、软件管理概览
不同 Linux 发行版使用的包格式和包管理工具不同:
- **Red Hat 系**`rpm` 包,常用 `yum` / `dnf`
- **Debian 系**`deb` 包,常用 `dpkg` / `apt`
日常运维中,更多时候直接使用**高级包管理器**,例如 `dnf``apt`,而不是手动操作底层包文件。
## 2、RPM 系软件管理
### 2.1 `rpm`
`rpm` 更偏底层,适合:
- 查询包信息
- 查看文件归属
- 手动安装本地 rpm 包
### 2.2 常见查询命令
```shell
rpm -qa
```
查看所有已安装软件包。
```shell
rpm -qf /usr/bin/ls
```
查看某个文件由哪个包安装生成。
```shell
rpm -ql bash
```
查看某个包安装后包含哪些文件。
## 3、`dnf` 包管理器
在新版本 Red Hat 系系统中,`dnf` 是常用包管理器。
### 3.1 常见配置文件
```shell
/etc/dnf/dnf.conf
/etc/yum.repos.d/*.repo
```
- `/etc/dnf/dnf.conf`:全局配置
- `/etc/yum.repos.d/*.repo`:仓库配置文件
### 3.2 常见配置项
- `gpgcheck=1`:安装前校验软件包签名
- `installonly_limit=3`:保留多个内核版本
- `clean_requirements_on_remove=True`:删除包时清理无用依赖
- `best=True`:优先选择最佳可用版本
### 3.3 仓库文件常见字段
```ini
[repo-name]
name=仓库名称
baseurl=仓库地址
enabled=1
gpgcheck=1
gpgkey=URL
```
## 4、`dnf` 常用命令
### 4.1 查看仓库
```shell
dnf repolist
dnf repolist all
dnf repolist -v
```
### 4.2 查看软件包
```shell
dnf list
dnf list installed
dnf list available
dnf list updates
dnf list nginx
```
### 4.3 安装和重装
```shell
dnf install nginx
dnf reinstall nginx
```
### 4.4 只下载不安装
```shell
dnf install nginx --downloadonly --downloaddir=/tmp/pkg
```
### 4.5 卸载软件
```shell
dnf remove nginx
```
### 4.6 查询与搜索
```shell
dnf info nginx
dnf search nginx
```
## 5、APT 包管理器
Debian / Ubuntu 系统中,常见包管理器是 `apt`
### 5.1 常见命令
- `apt install`:安装
- `apt remove`:卸载
- `apt purge`:卸载并删除配置文件
- `apt update`:刷新软件源索引
- `apt upgrade`:升级已安装软件包
- `apt autoremove`:删除不再需要的依赖
- `apt search`:搜索软件包
- `apt show`:查看软件包详情
- `apt list --installed`:查看已安装包
- `apt list --upgradable`:查看可升级包
## 6、常见运维建议
- Red Hat 系优先用 `dnf`Debian 系优先用 `apt`
- 修改仓库配置后,先更新索引再安装软件
- 卸载软件前,先确认是否存在依赖关系
- 线上环境尽量使用可信仓库并保留 GPG 校验
- 排障时可先判断:是仓库问题、依赖问题,还是网络问题
## 7、小结
- `rpm` / `deb` 是包格式,`dnf` / `apt` 是更常用的高级管理工具
- `rpm` 常用于查询和底层包操作
- `dnf` 是 Red Hat 系主流包管理器
- `apt` 是 Debian 系主流包管理器
- 软件管理重点不只是安装,还包括仓库、依赖、升级和清理