源本科技 | 码上会

Linux 软件包管理器

2025/12/24
90
0

在 Linux 系统中,包管理器(Package Manager)和 systemctl 是两大基石工具:

  • 包管理器 负责软件的安装、更新、卸载与依赖管理;

  • systemctl 则用于控制系统服务(如 Web 服务器、数据库等)的启停与自启行为。

掌握这两类工具,是高效运维 Linux 系统的关键。


什么是包管理器?

包管理器 是一种自动化工具,用于管理 Linux 系统中的软件包(Software Packages)。每个软件包通常包含:

  • 可执行程序

  • 配置文件

  • 库文件(libraries)

  • 文档与元数据

核心功能:

功能

说明

安装

从仓库或本地文件安装软件

依赖解析

自动安装所需依赖项

升级

更新到最新安全 / 功能版本

卸载

安全移除软件及相关文件

查询

查看已装 / 可装软件信息

优势:避免“依赖地狱”(Dependency Hell),确保系统一致性与安全性。


主流 Linux 包管理器

不同发行版使用不同的包管理生态:

发行版家族

默认包管理器

包格式

典型系统

Debian/Ubuntu

APT

.deb

Ubuntu, Debian, Linux Mint

Red Hat/Fedora

DNF(原 YUM)

.rpm

Fedora, RHEL, CentOS Stream

Arch Linux

Pacman

.pkg.tar.zst

Arch, Manjaro

openSUSE

Zypper

.rpm

openSUSE Leap/Tumbleweed

低层工具

dpkg / rpm

原始包

手动安装 .deb.rpm 文件


各包管理器命令

1. APT

Debian/Ubuntu 系

# 更新软件源列表(必须先执行!)
sudo apt update

# 安装软件
sudo apt install nginx

# 升级所有可更新软件
sudo apt upgrade

# 卸载软件(保留配置)
sudo apt remove firefox

# 完全清除(含配置)
sudo apt purge firefox

# 搜索软件包
apt search python3

# 查看已安装包信息
apt list --installed | grep docker

提示:aptapt-get 的现代化封装,交互更友好,推荐日常使用。


2. DNF

Fedora/RHEL 8+

注意:YUM 已被 DNF 取代(RHEL 8+、Fedora 22+)

# 安装
sudo dnf install httpd

# 更新所有包
sudo dnf upgrade

# 卸载
sudo dnf remove vim

# 搜索
dnf search git

# 列出已安装包
dnf list installed

兼容性:yum 命令在多数新系统中实际是 dnf 的软链接。


3. Pacman

Arch Linux 系

特点:

  • 极简设计

  • 滚动更新(始终最新)

  • 支持 AUR(Arch User Repository)社区仓库

常用命令:

# 同步并升级整个系统(关键命令!)
sudo pacman -Syu

# 安装
sudo pacman -S firefox

# 卸载
sudo pacman -Rsc libreoffice  # -s 删除依赖,-c 清除配置

# 搜索官方仓库
pacman -Ss keyword

# 查询已安装包
pacman -Q | grep kernel

AUR 辅助工具(如 yay)可安装社区包:

yay -S visual-studio-code-bin

4. Zypper

openSUSE

常用命令:

# 安装
sudo zypper install mariadb

# 更新
sudo zypper update

# 卸载
sudo zypper remove gimp

# 搜索
zypper search docker

# 列出仓库
zypper repos

优势:事务安全(失败时自动回滚),适合生产环境。


5. DPKG

Debian 底层工具(慎用)

dpkg 不处理依赖!通常配合 apt 使用。

常用命令:

# 安装本地 .deb 文件
sudo dpkg -i package.deb

# 若报依赖错误,修复:
sudo apt install -f

# 卸载(保留配置)
sudo dpkg -r package-name

# 完全清除
sudo dpkg -P package-name

# 列出所有已安装包
dpkg -l

场景:手动安装官方未提供的 .deb 软件(如 Chrome、VSCode)。


systemctlsystemd

什么是 systemd

  • Linux 的初始化系统(init system)和服务管理器

  • 替代旧式 SysV init 和 Upstart

  • 负责:启动进程、管理系统服务、日志、挂载点等

为什么重要

  • 并行启动 → 加快开机速度

  • 依赖管理 → 服务按序启动

  • 统一日志 → journalctl

  • 资源控制 → 通过 cgroups 限制 CPU/ 内存


systemctl 常用命令

1. 服务启停控制

# 启动服务
sudo systemctl start nginx

# 停止服务
sudo systemctl stop nginx

# 重启服务
sudo systemctl restart nginx

# 重载配置(不中断服务)
sudo systemctl reload nginx

2. 开机自启管理

# 设置开机自启
sudo systemctl enable nginx

# 禁用开机自启
sudo systemctl disable nginx

# 查看是否已启用
systemctl is-enabled nginx

3. 查看服务状态

# 查看详细状态(含日志片段)
systemctl status nginx

# 检查是否正在运行
systemctl is-active nginx

4. 系统级操作

# 重启系统
sudo systemctl reboot

# 关机
sudo systemctl poweroff

# 进入救援模式
sudo systemctl rescue

5. 查看所有服务

# 列出所有已加载的服务单元
systemctl list-units --type=service

# 列出所有已启用的服务
systemctl list-unit-files --type=service | grep enabled

重点总结

操作

APT (Debian)

DNF (RHEL)

Pacman (Arch)

Zypper (SUSE)

更新源

apt update

dnf check-update

pacman -Sy

zypper refresh

安装

apt install pkg

dnf install pkg

pacman -S pkg

zypper install pkg

升级

apt upgrade

dnf upgrade

pacman -Syu

zypper update

卸载

apt remove pkg

dnf remove pkg

pacman -R pkg

zypper remove pkg

systemctl 操作

命令

启动服务

systemctl start service

开机自启

systemctl enable service

查看状态

systemctl status service

重载配置

systemctl reload service


思考题

  1. 为什么 apt upgradeapt full-upgrade(或 dist-upgrade)有区别?什么情况下应使用后者?

  2. 如果一个服务 systemctl status 显示 “active (exited)”,这通常意味着什么?常见于哪些类型的服务?

  3. 在没有网络的情况下,如何使用 dpkgrpm 安装软件?需要注意哪些风险?

特别说明

配置阿里云作为软件源

  • 确认系统版本

cat /etc/os-release

# 或者直接获取当前系统代号
CODENAME=$(lsb_release -cs)
echo "当前系统代号: $CODENAME"
  • 替换为阿里云

sed -i "s|http://[a-z0-9\.]*\.archive\.ubuntu\.com|https://mirrors.aliyun.com|g" /etc/apt/sources.list
sed -i "s|http://security\.ubuntu\.com|https://mirrors.aliyun.com|g" /etc/apt/sources.list
  • 直接手动修改(上述命令不生效的情况下使用)

vi /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
  • 更新软件包索引

apt clean
apt update