How to Install and Use Trivy in Linux to Scan Container Images, Git Repositories, and Filesystems for Vulnerabilities

Modern DevSecOps pipelines demand continuous vulnerability detection across container base images, operating system packages, and open-source language dependencies (npm, PyPI, Go modules, Cargo). The open-source security scanner Trivy by Aqua Security provides a fast, all-in-one vulnerability and misconfiguration analysis tool. Designed for both developer terminals and automated CI/CD workflows, trivy detects Common Vulnerabilities and Exposures (CVEs), hardcoded secrets, and Infrastructure as Code (IaC) security flaws with zero configuration overhead.

Why Trivy Is the Leading Linux Security Scanner

Unlike monolithic scanners that require dedicated client-server architecture or external databases, trivy is a standalone binary. It downloads compact vulnerability metadata databases directly from GitHub Container Registry, performs local offline scanning, and delivers results in seconds. It scans OS packages (Alpine, Debian, Ubuntu, RedHat, Amazon Linux) and language-specific dependency locks (package-lock.json, Pipfile.lock, Cargo.lock, go.sum) seamlessly within a unified interface.

Installation Instructions

On Ubuntu and Debian

sudo apt-get install wget apt-transport-https gnupg lsb-release -y
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy -y

On Fedora, RHEL, and Rocky Linux

sudo rpm -ivh https://github.com/aquasecurity/trivy/releases/download/v0.56.2/trivy_0.56.2_Linux-64bit.rpm

On Arch Linux

sudo pacman -S trivy

Step-by-Step Practical Usage

Step 1: Scanning a Docker Container Image

To scan an official container image (e.g. python:3.9-slim) for known CVEs:

trivy image python:3.9-slim

Trivy checks OS layer packages and Python site-packages, rendering an organized vulnerability table with Severity, CVE ID, Package Name, Installed Version, and Fixed Version.

Step 2: Filtering by Severity (Critical and High Only)

To eliminate noise from low-severity warnings and focus remediation on actionable risks:

trivy image --severity HIGH,CRITICAL node:18-alpine

Step 3: Scanning a Local Project Directory for Secrets and Dependencies

To audit a local codebase for compromised dependencies, uncommitted AWS keys, or API tokens:

trivy fs /path/to/project

Step 4: Auditing Infrastructure as Code (IaC) Misconfigurations

Trivy natively scans Terraform configurations, Dockerfiles, and Kubernetes manifests for security flaws:

trivy config ./Dockerfile

Alerts highlight violations like running as root, missing health checks, or insecure port exposures.

Step 5: Integrating with CI/CD Pipelines (Exit on Failure)

To fail automated build pipelines when CRITICAL vulnerabilities exist:

trivy image --exit-code 1 --severity CRITICAL myapp:latest

Trivy Scan Target Categories Reference

Scan Command Target Artifact Detection Scope Typical Pipeline Stage
trivy image Container Images (OCI/Docker) OS packages + Application dependencies Post-build container verification / Registry audit
trivy fs Local Filesystem / Git Repos Lockfiles, hardcoded API secrets, licenses Pre-commit hooks and Pull Request validation
trivy repo Remote Git Repository URL Clones and scans without manual checkout Automated repository dependency monitoring
trivy config IaC Files (Terraform, Dockerfile) Security misconfigurations and compliance Pre-deployment infrastructure review
trivy sbom Software Bill of Materials (SPDX/CycloneDX) Vulnerabilities listed within SBOM documents Vendor software compliance verification

Best Practices and Performance Optimization

  • Vulnerability Database Caching: Trivy automatically caches its database in ~/.cache/trivy. In air-gapped or restricted CI environments, download the database archive ahead of time using trivy image --download-db-only.
  • Ignoring Unfixed CVEs: Many upstream packages have known CVEs without available patch versions. To hide vulnerabilities that lack fixes, append --ignore-unfixed.
  • Exporting CycloneDX SBOMs: Generate a formal Software Bill of Materials for your build by running: trivy image --format cyclonedx --output sbom.json myapp:latest.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.