直接用oscap xccdf eval命令可完成标准合规扫描,但必须指定正确策略文件路径和完整profile ID(如xccdf_org.ssgproject.content_profile_cis),否则结果无效;需先安装openscap-scanner与scap-security-guide包,并验证/usr/share/xml/scap/ssg/content/下存在对应ssg-rhel8-ds.xml等文件。

直接用 oscap xccdf eval 命令即可完成标准合规扫描,但必须指定正确的策略文件路径和 profile ID,否则结果无效或报错。
确认系统已安装 OpenSCAP 及 SSG 内容
没装齐组件会导致 oscap 找不到策略文件或报 File not found 错误:
- RHEL/CentOS 8+:运行
sudo dnf install openscap-scanner scap-security-guide - Ubuntu/Debian:运行
sudo apt install openscap-utils scap-security-guide - 验证安装:执行
oscap --version和ls /usr/share/xml/scap/ssg/content/ | grep rhel(如ssg-rhel8-ds.xml) - 若目录为空,说明
scap-security-guide包未正确安装或内容未解压到位
选对策略文件与 profile ID 是关键
策略文件(如 ssg-rhel8-ds.xml)本身不直接执行扫描,必须配合 --profile 参数指定具体基线,否则默认不触发任何检查:
- 常见 profile ID 示例:
xccdf_org.ssgproject.content_profile_cis、xccdf_org.ssgproject.content_profile_stig、xccdf_org.ssgproject.content_profile_pci-dss - 查可用 profile:运行
oscap info /usr/share/xml/scap/ssg/content/ssg-rhel8-ds.xml | grep -A5 "Profiles" - 查某 profile 的详情(含适用范围):运行
oscap info --profile cis /usr/share/xml/scap/ssg/content/ssg-rhel8-ds.xml - 错误示例:写成
--profile cis(缺完整命名空间)会静默跳过所有规则
执行扫描并生成可读报告
基础命令结构固定,但参数顺序和必要项容易遗漏:
- 生成 HTML 报告(推荐初用):
sudo oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_cis --report report.html /usr/share/xml/scap/ssg/content/ssg-rhel8-ds.xml - 只输出失败项(适合快速定位):
sudo oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_cis --results results.xml --oval-results /usr/share/xml/scap/ssg/content/ssg-rhel8-ds.xml 2>/dev/null | grep -E "(fail|error)" - 加
--fetch-remote-resources时需确保网络可达,否则 OVAL 检查会超时失败 - 不加
sudo会导致部分系统状态读取失败(如/etc/shadow权限检查),结果中大量error而非fail
注意扫描结果里“fail”和“error”的区别
HTML 报告中红标项不全是配置问题,混着两类根本不同性质的结果:
-
fail:系统实际状态不符合策略要求(例如密码重试次数设为 1,但 CIS 要求 ≥3)——这是真问题,需修复 -
error:扫描器无法获取信息(如目标文件被 SELinux 拒绝读取、OVAL 定义依赖的 RPM 未安装、--fetch-remote-resources失败)——不是配置错,是扫描条件不足 - 修复
error常需手动干预:调整 SELinux 策略、安装缺失包(如rpm -q audit)、或改用本地 OVAL 文件避免网络依赖 - 别把
error当低风险忽略——某些 error 会掩盖真正fail的检查项
真正难的不是跑通命令,而是看懂 report.html 里每条 fail 对应的 Remediation 是否适用于你的环境。比如自动给出的 sed 命令可能覆盖已有 PAM 配置,或修改 /etc/security/limits.conf 会干扰容器运行时——这些必须人工核对后再执行。


















