在 Linux 系统中,包管理器(Package Manager)和 systemctl 是两大基石工具:
包管理器 负责软件的安装、更新、卸载与依赖管理;
systemctl 则用于控制系统服务(如 Web 服务器、数据库等)的启停与自启行为。
掌握这两类工具,是高效运维 Linux 系统的关键。
包管理器 是一种自动化工具,用于管理 Linux 系统中的软件包(Software Packages)。每个软件包通常包含:
可执行程序
配置文件
库文件(libraries)
文档与元数据
核心功能:
优势:避免“依赖地狱”(Dependency Hell),确保系统一致性与安全性。
不同发行版使用不同的包管理生态:
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提示:
apt是apt-get的现代化封装,交互更友好,推荐日常使用。
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的软链接。
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 kernelAUR 辅助工具(如
yay)可安装社区包:yay -S visual-studio-code-bin
openSUSE
常用命令:
# 安装
sudo zypper install mariadb
# 更新
sudo zypper update
# 卸载
sudo zypper remove gimp
# 搜索
zypper search docker
# 列出仓库
zypper repos优势:事务安全(失败时自动回滚),适合生产环境。
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)。
systemctl 与 systemdLinux 的初始化系统(init system)和服务管理器
替代旧式 SysV init 和 Upstart
负责:启动进程、管理系统服务、日志、挂载点等
并行启动 → 加快开机速度
依赖管理 → 服务按序启动
统一日志 → journalctl
资源控制 → 通过 cgroups 限制 CPU/ 内存
systemctl 常用命令# 启动服务
sudo systemctl start nginx
# 停止服务
sudo systemctl stop nginx
# 重启服务
sudo systemctl restart nginx
# 重载配置(不中断服务)
sudo systemctl reload nginx# 设置开机自启
sudo systemctl enable nginx
# 禁用开机自启
sudo systemctl disable nginx
# 查看是否已启用
systemctl is-enabled nginx# 查看详细状态(含日志片段)
systemctl status nginx
# 检查是否正在运行
systemctl is-active nginx# 重启系统
sudo systemctl reboot
# 关机
sudo systemctl poweroff
# 进入救援模式
sudo systemctl rescue# 列出所有已加载的服务单元
systemctl list-units --type=service
# 列出所有已启用的服务
systemctl list-unit-files --type=service | grep enabled为什么 apt upgrade 和 apt full-upgrade(或 dist-upgrade)有区别?什么情况下应使用后者?
如果一个服务 systemctl status 显示 “active (exited)”,这通常意味着什么?常见于哪些类型的服务?
在没有网络的情况下,如何使用 dpkg 或 rpm 安装软件?需要注意哪些风险?
配置阿里云作为软件源
确认系统版本
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.listdeb 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