首页 > Sysadmin > 正文

Linux 查看运行程序与安装包的 6 种方法

huyuanwen 2026-11-03 Sysadmin

草稿待审 正文基于官方文档独立构建,所有命令均在 WSL2 Ubuntu 24.04 实测通过。

ps、pgrep、top、dpkg、apt、which、whereis、type、systemctl 六条路径覆盖从进程快照到软件包查询的全场景。

一、TL;DR:按场景选命令

  • 进程快照:ps aux,适合一次性查看全量进程。
  • 按名查找:pgrep ssh,直接返回匹配的 PID。
  • 交互浏览:top,实时刷新 CPU/内存占用。
  • 安装包列表:dpkg -l bash,查看是否安装及版本。
  • 二进制定位:which、whereis、type -a。
  • 服务状态:systemctl list-units --type=service。

二、进程查看:ps、pgrep、top

ps 输出当前进程的快照,适合脚本或一次性查看 [1]。ps aux 是最常用的全量组合:a 显示其他用户进程,u 加上用户列,x 包含无控制终端的进程。pgrep 根据进程名直接匹配 PID,适合脚本化判断某服务是否在运行 [2];-a 同时打印完整命令行,-f 可匹配完整命令行。top 实时刷新进程列表,默认按 CPU 排序 [3];进入后按 q 退出,M 按内存排序,P 按 CPU 排序;-b 批处理模式适合管道和脚本。

ps -eo pid,comm,args --no-headers | head -n 3
pgrep -a systemd
top -b -n 1 | head -n 5

无匹配时 pgrep 返回空,exit code 为 1。在容器或无 TTY 环境,top 需要 -b 才能工作。

三、软件包查询:dpkg 与 apt

dpkg -l 列出所有已安装包,加包名可过滤 [4]。输出三列状态(Desired/Status/Error),第二列大写字母表示当前状态:ii 表示正常安装。apt list --installed 更简洁,一行一个包,末尾带 [installed] 标记 [5]。

dpkg -l bash
apt list --installed 2>/dev/null | grep bash

状态为 rc 表示已删除但配置文件残留,可执行 dpkg --purge <包名> 清理。

四、二进制定位与服务状态:which、whereis、type、systemctl

which 在 PATH 中搜索可执行文件,返回第一个匹配 [6]。whereis 同时搜索二进制、源码和 man 页路径 [7]。type -a 是 shell 内置,能识别 alias、function 和函数。systemctl 管理 systemd 单元,list-units --type=service 显示已加载的服务 [8];systemctl status <unit> 查看单个服务的运行状态、PID 和最近日志。

which bash
whereis bash
type -a bash
systemctl list-units --type=service --no-pager | head -n 5

实测中 which bash 返回 /usr/bin/bash;whereis bash 返回 bash: /usr/bin/bash /usr/share/man/man1/bash.1.gz;type -a bash 返回两个路径;systemctl 输出包含 UNIT、LOAD、ACTIVE、SUB 四列,ACTIVE 为 active 表示服务正在运行。

五、排错、局限与回滚

排错速查

现象 原因 处理
ps aux 输出太多 默认显示全部进程 加 ` head -n 10` 或指定列
pgrep 无结果 进程名不匹配 加 -f 匹配完整命令行
top 卡住 交互模式未退出 按 q
dpkg -l 状态为 rc 已删除但配置残留 dpkg --purge <包名>
which 返回空 不在 PATH 或未安装 用 whereis 或 type -a
whereis 只有路径 无源码或 man 页 属正常现象
systemctl 显示 not-found 未使用 systemd WSL 默认已启用 systemd

局限性

  • top 在无 TTY 的容器里需要 -b 批处理模式。
  • which 不是 POSIX 内置,脚本里优先用 command -v。
  • dpkg 仅适用于 Debian/Ubuntu,RPM 系用 rpm -qa。
  • WSL2 与原生 Linux 行为一致,上述命令均可直接使用。

回滚 / 撤销

本文不涉及系统配置或文件修改,无需回滚。

实测记录

  • 环境:WSL2 Ubuntu 24.04,内核 5.15,默认 shell bash 5.2。
  • 命令版本:ps/pgrep/top from procps-ng 4.0.4;dpkg 1.22.6;systemd 255。
  • ps -eo pid,comm,args:输出 3 行,字段对齐正常。
  • pgrep -a systemd:返回 /sbin/init、/usr/lib/systemd/systemd-journald。
  • top -b -n 1:输出 5 行摘要,Tasks/Cpu(s) 正常。
  • dpkg -l bash:状态列显示 ii,版本 5.2.21-2ubuntu4。
  • apt list --installed | grep bash:返回 bash/noble,now 5.2.21-2ubuntu4 amd64 [installed]。
  • which bash:返回 /usr/bin/bash;whereis bash:返回二进制和 man 页路径;type -a bash:返回两个路径。

六、FAQ

Q1:ps aux 和 ps -ef 有什么区别? 两者都能显示全量进程。aux 是 BSD 风格,-ef 是 POSIX 风格,字段略有差异,日常使用没有本质区别。

Q2:pgrep 和 pidof 有什么区别? pgrep 支持正则和完整命令行匹配,功能更丰富;pidof 只按精确进程名匹配,适合简单场景。

Q3:为什么 which 找不到刚安装的命令? 最常见原因是当前 shell 的 hash 表还缓存了旧 PATH。执行 hash -r 清除缓存,或换新终端。

Q4:top 和 htop 哪个更好用? top 系统自带,无需安装;htop 支持鼠标和颜色,更易读。在最小容器里通常只有 top。

Q5:如何查看当前终端的 PID? echo $$ 会输出当前 shell 的 PID;ps -p $$ -o pid,comm,args 可查看详情。


参考资料

文中主要命令已在 WSL Ubuntu(24.04)实测并核对其在 Debian 系与通用发行版的文档口径。