DEBUG.WIKI
ZgoCloud:多地区部署,按需选节点;日本、美国、香港、德国
V.PS:让项目靠近用户,亚太、欧洲、北美 KVM VPS
晚安云;香港 CN2 ¥15/月

解决 Lynis 报错 Security hardening level below threshold 安全评分加固修复

排查 Lynis 报告中出现大量 systemd service security warnings(如 ProtectSystem=no、NoNewPrivileges=no),分析 服务沙盒化与安全加固配置。

问题现象

运行 Lynis 扫描后,控制台警告显示多个常驻服务安全性得分为危险等级:

Warnings (2):
  ! Service my-app.service has a security score of 9.4/10 (unsafe) [BOOT-5264]
      - No sandboxing mechanisms enabled

触发根因

  1. 服务单元文件(.service)未开启 systemd 提供的现代内核沙盒防护机制;
  2. 服务默认以全局超级权限运行且对根文件系统和设备节点拥有完全读写特权。

解决步骤

使用 systemd 提供的分析工具审查具体脆弱项

运行深度诊断:

systemd-analyze security my-app.service

在服务单元中加入标准沙盒化保护指令

编辑 /etc/systemd/system/my-app.service 的 [Service] 块:

[Service]
User=appuser
Group=appuser
# 禁止提权与防护敏感内核路径
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=read-only
ProtectKernelTunables=true
ProtectControlGroups=true
PrivateTmp=true
ReadWritePaths=/var/log/my-app

重新加载并重启服务:

sudo systemctl daemon-reload
sudo systemctl restart my-app

验证确认

重新执行 Lynis 扫描:

该服务安全风险评分从 9.4 骤降至 1.8(OK),整机合规性评级显著提升。