服务最小化核查怎么做
最小安装、最小服务是主机加固的基础要求:装的包越少、启用的服务越少,暴露面与补丁负担越小。核查分三步:先盘点所有 enabled 的服务单元,再对照最小化清单圈出可停项,最后在确认依赖不影响业务的前提下停用并取消自启。注意 disable 与 stop 是两件事,验证必须用 is-enabled 与 is-active 分别确认,只做一半会导致重启后又全部回来。
基本信息
| 适用平台 | systemd 系全部发行版;桌面版与服务器版默认服务差异很大 |
|---|---|
| 盘点 | systemctl list-unit-files --type=service --state=enabled |
| 依赖检查 | systemctl list-dependencies --reverse 服务名 |
| 停用 | systemctl disable --now 服务名 |
| 验证 | systemctl is-enabled 与 is-active 两个都查 |
典型使用场景
- 交付前加固:新装系统按清单收敛服务,再进入生产
- 等保核查:输出启用的服务清单作为最小化证据
- 磁盘与内存优化:停掉不用的守护进程,降低资源占用
- 攻击面收敛:关闭 cups、avahi、bluetooth 等桌面相关服务
- 故障排查:服务异常时确认是否有不该启用项在抢占资源
盘点与依赖确认
盘点与依赖确认
systemctl list-unit-files --type=service --state=enabled | head -60 # 已启用清单
systemctl list-unit-files --type=service | grep -E 'cups|avahi|bluetooth|rpcbind|nfs'
systemctl --failed # 有失败单元先记录
systemctl list-dependencies --reverse rpcbind | head # 看谁依赖它
systemctl list-units --type=service --state=running | head -40 # 正在运行的清单
systemctl is-enabled cups avahi-daemon bluetooth 2>/dev/null停用与验证
停用与验证
systemctl disable --now cups # 取消自启并立即停止
systemctl disable --now bluetooth
systemctl disable --now rpcbind
systemctl is-enabled cups; systemctl is-active cups # 两项都确认
systemctl daemon-reload # 单元有改动时重载
systemctl list-unit-files --state=enabled | wc -l # 复查启用数量是否下降常见可停服务
| 服务 | 用途 | 停用风险 |
|---|---|---|
| cups | 打印服务 | 服务器不需要打印,桌面需保留 |
| avahi-daemon | 局域网设备发现 | 多数环境不需要,注意影响 mDNS |
| bluetooth | 蓝牙 | 桌面需按需保留 |
| rpcbind | RPC 端口映射 | NFS 客户端/服务端依赖,停前必查 |
| nfs-server | NFS 服务端 | 对外提供共享时不能停 |
| postfix | 邮件服务 | 仅作本地通知时可改为按需启动 |
桌面版与服务器版的默认服务不同:先按环境定位(办公终端还是服务器)确定清单,再逐机核对执行。
停用前先 list-dependencies --reverse 查依赖方;例如停 rpcbind 会让 NFS 挂载失败,故障表现与防火墙问题相似,容易查错方向。