$RodHat_/search
Console Tips

pkg audit, pkg lock and pkg check, the three commands that keep a FreeBSD box honest

Published by

This article may contain Amazon affiliate links. I may earn a commission if you buy through them. Disclosure.

Terminal on a FreeBSD host showing pkg audit output listing a vulnerable package with a CVE reference and a URL
AI illustration: RodHat
On this page · 4 sections
  1. pkg audit: what on this box has a known hole
  2. pkg lock: pin a package through an upgrade
  3. pkg check: did something change a file I did not
  4. What I run

Every few months someone asks me how I know what is wrong with a FreeBSD box without a dashboard, an agent, and a vendor. The answer is three subcommands of pkg(8) that I type without thinking and that most people never open the man pages for.

pkg audit: what on this box has a known hole

pkg audit -F

-F fetches the current vulnerability database first, then checks every installed package against it. Without -F you audit against whatever database you downloaded last time, which on a neglected machine is a comforting lie. Each hit prints the package, the advisory text, and a URL. pkg-audit(8) also takes -r, which adds the packages that depend on a vulnerable one, so you can see the blast radius before you start upgrading.

Two things it will not do. It does not know about software you built outside pkg, and a clean report means “no known entry for this version in the database”, not “safe”. It is a lookup, not an audit in the compliance sense.

The part people miss: you probably already run it. The periodic security scripts include a pkg audit check, so the nightly mail may have been telling you for weeks. I wrote about that in periodic(8), the cron job you never read. Read the mail.

pkg lock: pin a package through an upgrade

You have a build of something that works, the new version changes behaviour, and you cannot test it until Thursday. Pin it:

pkg lock nginx
pkg lock -l
pkg unlock nginx

A locked package will not be upgraded, reinstalled, or deleted by pkg until you unlock it. pkg lock -l lists what is locked (check pkg-lock(8) on your release for the exact long options). Add -y in scripts to skip the confirmation prompt.

The cost is real. A lock is a promise to come back, and a locked package keeps showing up in pkg audit while you forget about it. Put the unlock date in your notes, or the lock becomes the incident. If you want to try an upgrade with an exit, take a boot environment first with bectl and skip the lock entirely.

pkg check: did something change a file I did not

pkg check -s -a

-s recomputes checksums of installed files and compares them with what the package database recorded. -a does it for every package. Output is a list of files whose checksum no longer matches. A modified file under /usr/local/bin that you did not touch is a conversation worth having, whether the cause is a bad disk, a careless root shell, or something worse.

Related flags in pkg-check(8): -d checks for missing dependencies, -B recalculates the shared library dependency information, and -r reinstalls packages with problems. Run pkg check -d -a after any upgrade that was interrupted. If you remember the dependency-hell mess a dying pkg upgrade leaves behind, this is the cheap diagnosis.

This is the package-level cousin of verifying the base system tree against a spec, which I covered in mtree(8). pkg check covers /usr/local; mtree covers whatever spec you wrote it.

What I run

On a server I care about, weekly: pkg audit -F, then pkg check -s -a, output to a file I diff against last week’s. It takes under a minute and has found a silently corrupted file once and a forgotten lock twice.

I resisted pkg for years because ports let me know exactly what I was running. I still build the odd thing from source. But pkg audit -r answering “what depends on the broken thing” in one line is faster than anything I did by hand. Fine. It’s good. More in the FreeBSD and OpenBSD hub.