linux 中查看服务依赖关系主要用 systemctl list-dependencies 命令:正向查依赖用 systemctl list-dependencies 服务名,反向查谁依赖它加 –reverse,树状展示加 –tree;依赖源自。service 文件中的 wants=、requires=、after= 等字段,可用 systemctl cat 查看;验证依赖是否满足需结合 systemctl verify 和 systemctl status。

Linux 中查看服务依赖关系,主要通过 systemd 管理的服务(现代主流发行版默认)来实现,核心命令是 systemctl 的依赖分析子命令。
查看某个服务依赖哪些其他服务(正向依赖)
即“启动 A 之前必须先启动哪些服务”。使用:
-
systemctl list-dependencies <strong> 服务名 </strong>—— 显示所有层级的依赖(包括间接依赖) -
systemctl list-dependencies --type=after <strong> 服务名 </strong>—— 只看启动顺序上的“在……之后”,更贴近实际依赖逻辑 -
systemctl list-dependencies --reverse <strong> 服务名 </strong>—— 查看谁依赖它(反向),常用于判断停用影响范围
查看服务单元文件中声明的依赖项
依赖关系最终来自服务单元(.service)文件中的配置字段。可直接检查:
-
systemctl cat <strong> 服务名 </strong>—— 查看完整 unit 文件内容 - 重点关注:
Wants=、Requires=(强依赖,失败则本服务不启动)、After=/Before=(启动顺序)、BindsTo=(绑定依赖,被依赖服务停止时本服务也自动停止)
图形化或树状方式查看依赖结构
增强可读性,适合复杂服务:
-
systemctl list-dependencies --all --tree <strong> 服务名 </strong>—— 以缩进树形展示全部依赖链(含 wants/requires/after 等) - 例如:
systemctl list-dependencies --all --tree nginx.service能清晰看到 network.target、system.slice 等逐层关系
验证依赖是否满足、服务能否正常启动
仅看声明不够,还需确认运行时状态:
-
systemctl verify <strong> 服务名 </strong>—— 检查 unit 文件语法及基础依赖完整性(如引用的 target 是否存在) -
systemctl status <strong> 服务名 </strong>—— 查看 Loaded 行是否提示“not-found”或“masked”,Active 行是否为“inactive (dead)”并提示 failed due to dependency - 若依赖服务未启用:
systemctl is-enabled <strong> 依赖服务名 </strong>和systemctl is-active <strong> 依赖服务名 </strong>可分别确认是否开机自启和当前是否运行






























