Compare images and filter by severity and package type
In the previous steps, we performed basic image comparisons. However, the output can sometimes be very detailed, especially for images with many vulnerabilities. trivy
allows you to filter the results based on severity level and package type, which helps you focus on the most critical issues or specific types of vulnerabilities.
Let's compare ubuntu:20.04
and ubuntu:22.04
again, but this time we will filter the results to only show vulnerabilities with a severity of HIGH
or CRITICAL
. This is useful when you want to prioritize fixing the most severe vulnerabilities.
We use the --severity
flag to specify the minimum severity level to report. You can provide a comma-separated list of severity levels.
trivy image --diff-image ubuntu:20.04 ubuntu:22.04 --severity HIGH,CRITICAL
This command will compare the two ubuntu
images and only display vulnerabilities that are classified as HIGH
or CRITICAL
in either image. You will notice that the output is much shorter than the default comparison, as it excludes vulnerabilities with lower severity levels (UNKNOWN, LOW, MEDIUM).
Besides filtering by severity, you can also filter by the type of package where the vulnerability was found. trivy
can scan for vulnerabilities in operating system packages (like those installed via apt
, yum
, etc.) and application dependencies (like libraries used by your code).
Let's compare the images again, but this time we will only look for vulnerabilities in operating system packages. We use the --vuln-type
flag for this.
trivy image --diff-image ubuntu:20.04 ubuntu:22.04 --vuln-type os
This command will compare the two images and only report vulnerabilities found in the operating system packages. This is helpful if you are primarily concerned with the security of the base operating system layer of your image.
You can also combine these filters. For example, to see only HIGH
or CRITICAL
vulnerabilities in operating system packages:
trivy image --diff-image ubuntu:20.04 ubuntu:22.04 --severity HIGH,CRITICAL --vuln-type os
This command provides a more focused view of the most severe vulnerabilities within the operating system layer of your images. Filtering helps you manage the output and prioritize your security efforts effectively.