Every few months someone asks me for a file integrity tool and reaches for something with a web console and a license server. FreeBSD has shipped one in the base system for decades. It is called mtree, it is a man page and a text file, and it does not phone anybody.
Record, then verify
mtree -c -k type,uid,gid,mode,size,sha256digest -p /usr/local/etc > /root/usr-local-etc.spec
-c writes a specification from the tree under -p. -k picks which attributes get recorded. Here: file type, owner, group, mode, size and a SHA-256 of the contents. The spec is plain text, one entry per file, readable with less.
Later, check the tree against it:
mtree -f /root/usr-local-etc.spec -p /usr/local/etc
Silence means the tree matches. Anything else prints a line per difference. Read mtree(8) on your release for the exact wording, but you will see three shapes: a file in the spec that is gone (missing), a file on disk that the spec does not know about (extra), and a file whose recorded keyword no longer matches, with the expected and found values side by side. The man page says it exits non-zero on mismatch, so it drops straight into a script.
Flags that matter
-estops complaining about extra files. Use it when you only care that the files you listed are intact, not that nobody dropped a new one beside them.-xdoes not descend into other mount points. Without it, pointing mtree at/will wander into every filesystem under it, and you will wait.-Kadds keywords to the default set rather than replacing it, which is the lazy way to add a hash. I prefer-kwith an explicit list so the spec says what I meant.-Uupdates the tree to match the spec’s modes and ownership instead of just reporting. Powerful, and exactly the thing you do not want to run as root against a tree you have not looked at first. Report first. Always.
Where this earns its keep
- Config drift. Spec
/etcand/usr/local/etcright after a clean install. A month on, the verify run is a list of everything anyone has touched. It is the same question zfs diff answers, but it works on UFS, on a tarball unpacked on a Linux box (mtree is packaged for it), and across machines. - Fleet comparison. One golden spec, N hosts, one loop. A host with a different
sshd_confighash is the one you go look at. - Post-restore check. Restored a backup? Verify it against the spec you took before the failure. “The restore completed” and “the restore is right” are different claims.
The part people get wrong
A spec stored on the machine it describes proves nothing. Anyone who can modify /usr/local/bin/sudo can modify /root/usr-local-etc.spec in the same breath. Copy the spec off the box, or at minimum print its own hash somewhere else, and verify with a copy you fetched fresh. Same logic as the trust store you forgot a service reads: the check is only as honest as where it lives.
Other limits, because there are always some:
- Recording the
timekeyword on a directory like/var/loggives you a failure every run. Spec things that are supposed to be static. - Hashing a large tree reads every byte. Do it off-peak, and not on the same disks your database is hammering. If
gstatis already red, see what gstat tells you about a busy disk. - It tells you that something changed, not who or why. That is a different tool and a different afternoon.
It will not replace a proper audit trail. It will, however, tell you in a few seconds whether the box you are about to blame is the box you set up. More in the FreeBSD and OpenBSD hub.
