搬瓦工 VPS Trivy 容器镜像安全扫描教程

Trivy 是由 Aqua Security 开源的全面安全扫描工具,可以扫描容器镜像、文件系统、Git 仓库和 Kubernetes 集群中的安全漏洞、错误配置和敏感信息泄露。它是目前最流行的容器安全扫描工具之一,扫描速度快且误报率低,被广泛集成到 CI/CD 流水线中。本文将介绍如何在搬瓦工 VPS 上使用 Trivy 保障容器安全。

一、安装 Trivy

# Ubuntu / Debian
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor -o /usr/share/keyrings/trivy.gpg
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb generic main" > /etc/apt/sources.list.d/trivy.list
apt update && apt install trivy -y

# CentOS / RHEL
cat > /etc/yum.repos.d/trivy.repo <<'EOF'
[trivy]
name=Trivy repository
baseurl=https://aquasecurity.github.io/trivy-repo/rpm/releases/$basearch/
gpgcheck=0
enabled=1
EOF
yum install trivy -y

# 验证
trivy version

二、扫描容器镜像

# 扫描公共镜像
trivy image nginx:latest

# 扫描并仅显示高危和严重漏洞
trivy image --severity HIGH,CRITICAL nginx:latest

# 扫描本地 Docker 镜像
trivy image --input myapp.tar

# 以 JSON 格式输出结果
trivy image --format json --output results.json nginx:latest

# 以表格格式输出
trivy image --format table nginx:latest

# 扫描时忽略未修复的漏洞
trivy image --ignore-unfixed nginx:latest

三、扫描文件系统

# 扫描项目目录中的依赖漏洞
trivy fs /path/to/project

# 扫描指定配置文件的安全问题
trivy config /path/to/terraform/
trivy config /path/to/kubernetes/

# 扫描 Dockerfile 最佳实践
trivy config --policy-namespaces builtin.dockerfile .

四、扫描 Git 仓库

# 扫描远程 Git 仓库
trivy repo https://github.com/user/project

# 扫描本地 Git 仓库
trivy repo /opt/myproject

五、密钥泄露检测

# 在镜像中查找泄露的密钥
trivy image --scanners secret nginx:latest

# 在文件系统中查找密钥
trivy fs --scanners secret /opt/project

# 在 Git 仓库中查找密钥
trivy repo --scanners secret https://github.com/user/project

六、自动化扫描脚本

cat > /opt/trivy-scan.sh <<'EOF'
#!/bin/bash
LOG_DIR="/var/log/trivy"
DATE=$(date +%Y%m%d)
mkdir -p $LOG_DIR

# 获取所有正在运行的容器镜像
IMAGES=$(docker ps --format '{{.Image}}' | sort -u)

for IMAGE in $IMAGES; do
    SAFE_NAME=$(echo $IMAGE | tr '/:' '_')
    echo "Scanning: $IMAGE"
    trivy image --severity HIGH,CRITICAL \
      --format json \
      --output "$LOG_DIR/${SAFE_NAME}-${DATE}.json" \
      "$IMAGE"
done

# 检查是否有严重漏洞
CRITICAL=$(find $LOG_DIR -name "*-${DATE}.json" -exec grep -l '"Severity": "CRITICAL"' {} \;)
if [ -n "$CRITICAL" ]; then
    echo "CRITICAL vulnerabilities found in: $CRITICAL" | \
    mail -s "Trivy Alert: Critical Vulnerabilities" admin@example.com
fi
EOF
chmod +x /opt/trivy-scan.sh
echo "0 6 * * * root /opt/trivy-scan.sh" >> /etc/crontab

七、CI/CD 集成

# Docker 构建后自动扫描
docker build -t myapp:latest .
trivy image --exit-code 1 --severity CRITICAL myapp:latest
# 如果发现严重漏洞,exit-code 1 会导致构建失败

八、忽略已知漏洞

# 创建忽略文件
cat > .trivyignore <<'EOF'
# 已评估可接受的漏洞
CVE-2023-12345
CVE-2023-67890
EOF

# 使用忽略文件扫描
trivy image --ignorefile .trivyignore nginx:latest

总结

Trivy 是搬瓦工 VPS 上 Docker 容器安全管理的必备工具。配合 Grype 进行交叉验证、Cosign 进行镜像签名,可以构建完整的容器供应链安全体系。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,通过 bwh81.net 进入官网购买。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。