Introduction
A process that needs one narrow privileged operation, such as binding to port 443 or opening a raw socket, does not need to run as root. Linux capabilities split root’s all-or-nothing privilege into 41 named flags (cap_last_cap reports 40 on modern kernels; that count has held since Linux 5.9), and setcap attaches a chosen subset to an executable so an unprivileged account can run it and still perform that specific operation. getcap reads back what was attached. The audit problem is that file capabilities are invisible to ls -l, invisible to find -perm, and stored in the security.capability extended attribute where ordinary file inspection never looks. A binary can carry CAP_SYS_ADMIN for years because someone copied a command off a forum in 2019 and nobody read the xattr since. Walk the tree with getcap -r, compare against a baseline you keep in version control, and re-run the check after every package upgrade, because the package manager replaces binaries and the new inode arrives with no capabilities at all.
How the kernel stores and evaluates capabilities
Each capability is a bit. The kernel tracks five sets per process: permitted, effective, inheritable, bounding, and ambient. Permitted is what the process is allowed to hold. Effective is what it can currently exercise. Inheritable survives across exec. Bounding is a hard ceiling that exec cannot exceed. Ambient is the one set that lets a non-privileged program carry a capability across exec without any file capability at all, and it was added in kernel 4.3.
File capabilities use only three of those flags. When you write cap_net_bind_service=ep, you set the permitted and effective bits on the file. At exec time the kernel combines the file’s permitted set with the process’s inheritable set and the bounding set to produce the new process’s permitted set, then uses the file’s effective bit to decide whether those permissions go active immediately or stay dormant until the program calls capset(). A file capability never crosses into a set the bounding mask excludes. If a parent process dropped CAP_NET_BIND_SERVICE from its bounding set, the same binary that works in your login shell will fail in that process’s child.
The internal representation is a vfs_cap_data struct: a first word encoding a magic number plus a revision flag, then the permitted and inheritable masks for bits 0-31, then the permitted and inheritable masks for bits 32-63. Revision 1 (original, 32-bit masks) and revision 2 (Linux 2.6.25, 64-bit masks) appear on older systems; revision 3 (Linux 4.14) supports namespaced file capabilities. Ambient capabilities are a process-level feature added in Linux 4.3 — they are not part of the file revision at all. Newer kernels understand all three revisions.
Reading the current state with getcap
The basic invocation is a path. On a Debian or Ubuntu box the utilities live in the libcap2-bin package:
sudo apt install libcap2-bingetcap /usr/bin/pingOutput on a stock Ubuntu 24.04 looks like:
/usr/bin/ping cap_net_raw=epRecurse over the whole filesystem with -r:
sudo getcap -r / 2>/dev/nullThe redirect matters. getcap will traverse /proc and /sys and spew Operation not supported or permission errors for virtual filesystems. Piping stderr to /dev/null leaves the real list. Add -v for verbose mode, which lists every path examined rather than only files that carry capabilities, useful when you are trying to prove a directory contains nothing:
sudo getcap -v -r /usr/local 2>/dev/nullFor a running process, getpcaps decodes the five Cap fields into names:
getpcaps 1getpcaps $$The raw values in /proc/self/status are hexadecimal bitmasks. You can decode one by hand with capsh:
grep Cap /proc/self/statuscapsh --decode=0000000000000400That returns 0x0000000000000400=cap_net_bind_service. The capsh --print command shows the current process’s full set; the kernel’s cap_last_cap, the highest capability number the kernel knows about, lives at /proc/sys/kernel/cap_last_cap.
Setting capabilities with setcap
The syntax takes a space-separated capability spec followed by the target file. Use +ep to add permitted and effective, -ep to remove, and = to reset the set explicitly:
sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/myappgetcap /usr/local/bin/myappThe result reads back as cap_net_bind_service=ep. To grant multiple capabilities, comma-separate them:
sudo setcap 'cap_net_bind_service,cap_net_raw=+ep' /usr/local/bin/myappTo strip everything, use -r:
sudo setcap -r /usr/local/bin/myappThe kernel requires the permitted bit whenever the effective bit is enabled. Enabling only the effective bit — cap_net_bind_service=+e alone — is accepted but produces an empty grant: getcap then reports just file =. Some guides tell you to use cap_net_bind_service=+eip, which is valid but unnecessary. Inheritable only matters if you plan to re-exec another binary that needs the same capability in its own inheritable set, and that pattern is rare outside of a custom daemon that drops privileges by hand.
Writing security.capability requires CAP_SETFCAP, so setcap has to run as root or through sudo. Set the capability on the real binary, not on a symlink to it.
Auditing the full filesystem
An audit is not one command. It is a baseline plus a diff. Start by capturing the current state to a file you can commit:
sudo getcap -r / 2>/dev/null | sort > /var/lib/capability-audit/current.txtOn a fresh server that file holds a small handful of lines. On a workstation with Docker, snaps, and a few manually installed tools it can hold thirty. Read every line and ask whether the capability matches the program’s function. A browser with cap_sys_admin=ep is a privilege escalation waiting for a bug. A container runtime shim with cap_sys_admin=ep is expected. The audit is about the gap.
To diff across time:
sudo getcap -r / 2>/dev/null | sort > /var/lib/capability-audit/$(date +%F).txtdiff /var/lib/capability-audit/2026-11-01.txt /var/lib/capability-audit/2026-12-05.txtAny line that appears outside of a package upgrade window deserves a ticket. Any line that disappears when a service starts failing deserves a check of the package manager logs.
The find command can also surface them, though the xattr filter is awkward:
sudo find / -xdev -type f -exec getfattr -d -m security.capability {} + 2>/dev/nullgetfattr from the attr package prints the raw base64 blob. It is useful when getcap skips a mount it cannot read and you need to prove a directory holds no capabilities.
What the capability xattr looks like on disk
Read the raw xattr to understand what you are actually setting:
sudo getfattr -d -m - /usr/local/bin/myappTypical output:
# file: usr/local/bin/myappsecurity.capability=0sAQAAAgAEAAAAAAAAAAAAAAAAAAA=The base64 decodes to the vfs_cap_data header: 01 00 00 02 is little-endian for 0x02000001, meaning revision 2 with the effective flag set; 00 04 00 00 is the permitted low word, 0x00000400 = CAP_NET_BIND_SERVICE; the remaining words are zero. There is no ambient field in the file — ambient is a process property, not stored in the xattr. On a filesystem that does not support security.capability, setcap fails with Operation not supported before any of this appears. ext4, xfs, btrfs, zfs with the right dataset property, and tmpfs all support it. NFS, CIFS, vfat, and most FUSE mounts do not. Overlayfs support depends on the backing filesystem and the container runtime’s mount options; Docker’s overlay2 driver supports it on recent versions, but some CI systems strip the xattr on layer export.
If you tar a file with capabilities, GNU tar needs --xattrs to carry them. GNU cp copies them only with --preserve=xattr (or -a); plain cp drops them. rsync needs -X/--xattrs. OpenSSH scp does not carry extended attributes at all, and git never stores them. A binary distributed via a tarball without xattr headers arrives with no capabilities even if the tarball’s README says to run setcap.
Why scripts and non-ELF files ignore file capabilities
You can run setcap cap_net_bind_service=+ep /usr/local/bin/deploy.sh and setcap will succeed. The xattr is written. getcap reports it. At exec time the kernel sees a #! interpreter line, hands the script path to /bin/bash, and never applies the capability. The shell that runs the script has whatever privileges the invoking user had. The fix is to attach the capability to the interpreter, which is a bad idea because it exposes every script that interpreter runs, or to replace the shell script with a compiled wrapper. capsh --shell=/bin/bash is a testing shortcut, not a production pattern.
The same rule applies to any file that is not ELF. Java .jar launchers, Python entry points installed as scripts, and Node.js .js files invoked directly all fall outside what file capabilities can affect. Bind ports through a reverse proxy such as nginx or HAProxy, or configure the daemon’s own capability handling through systemd (see below).
Mount options also disable capabilities. A filesystem mounted nosuid causes the kernel to ignore security.capability on exec. This is the default for /tmp on many hardened systems and for most container rootfs mounts unless the runtime overrides it. If a test binary works in /usr/local/bin and fails in /tmp, mount the directory without nosuid or move the binary.
Failure modes you will hit in practice
The errors are specific enough to diagnose once you know the shape.
Failed to set capabilities on file '/path/app' (Operation not supported) means the target filesystem does not support the security.capability xattr. Check with mount | grep <mountpoint>, look for nfs, cifs, vfat, or fuse in the type column, and move the binary to a filesystem that does.
Failed to set capabilities on file '/path/app' (Invalid argument) is a malformed capability spec — cap_net_bind_service=+ep itself is valid, but garbage inside the spec or an unquoted shell expansion can break it. Try running the same command with single quotes.
Failed to set capabilities on file '/path/app' (Operation not permitted) means you are not root, or you are root inside a container without CAP_SETFCAP in the bounding set. Check capsh --print | grep Bounding and confirm cap_setfcap appears. Docker’s default capability set includes it, but rootless Podman and most Kubernetes security policies remove it.
capng_apply: Operation not permitted comes from a program using libcap-ng at runtime when the file’s capabilities were dropped or never applied. The binary started, most likely because its effective bit was set, then tried to raise a capability outside its permitted set. Trace the path that calls capng_update.
bind: Permission denied on a process that used to bind port 80 is the symptom of a lost capability after upgrade. Re-run getcap, compare against the baseline, and if the file is empty, the package replaced it.
Package upgrades silently erase file capabilities
This is the most common cause of a working capability grant becoming a broken service overnight. When dpkg or rpm unpacks a new version of a package, it writes a new file to disk. The old inode, and its xattr, are replaced. The new file arrives with no security.capability at all unless the package explicitly sets it in a postinst script or through the rpm %caps directive.
Debian has no equivalent directive in the .deb format; packages such as iputils-ping re-apply capabilities in postinst — for example setcap cap_net_raw+ep /usr/bin/ping. Third-party binaries installed with setcap manually are not covered. A cron job that runs apt upgrade overnight will remove the grant, and the service will fail at next restart with a permission error that has no obvious source.
RHEL and Fedora have a cleaner mechanism. RPM’s %files list can carry %caps(cap_net_bind_service=ep), and rpm applies the xattr at install time and re-applies it on upgrade (your RPM build needs libcap support). rpm --setcaps <package> re-applies capabilities from package metadata if they were lost. If you maintain packages, use that directive. If you install binaries by hand, write a small systemd unit drop-in that re-applies the capability on every boot through ExecStartPre=/usr/sbin/setcap ..., or ship the grant as part of a configuration management role so it reappears after upgrades.
For anything installed via a .deb or .rpm that lacks the directive, verify post-upgrade:
dpkg -S /usr/local/bin/myapp # is it even tracked?getcap /usr/local/bin/myapp # is the grant still there?If the path is not tracked by a package, nothing will re-apply it. That is your prompt to automate.
Comparing distro defaults and package names
The getcap and setcap binaries ship in different packages depending on the distribution. On Debian and Ubuntu the package is libcap2-bin; on current merged-usr releases the tools live in /usr/sbin (which /sbin symlinks to). On RHEL 8, RHEL 9, and Fedora the package is libcap and the tools live in /usr/sbin/. On Arch Linux install libcap; on Alpine install libcap and call /usr/sbin/setcap directly — its PATH sometimes omits /sbin.
The kernel’s default bounding set differs too. Debian and Ubuntu remove nothing by default; systemd services can ship with their own CapabilityBoundingSet= that trims capabilities per service. Arch keeps everything. Detection is straightforward:
capsh --print | grep Boundinggrep CapBnd /proc/1/statusCross-check that the bounding set still contains whatever capability your file-based grant needs. A hardened image that drops CAP_NET_RAW from the bounding set will make ping fail even when getcap /usr/bin/ping reports the expected grant.
The kernel version also matters. Ambient capabilities require kernel 4.3 or later, and a few newer capabilities such as CAP_BPF and CAP_PERFMON arrived in 5.8, with CAP_CHECKPOINT_RESTORE following in 5.9. An older libcap refuses a capability name it does not know, so setcap fails rather than granting a bit the kernel cannot hold. Check cat /proc/sys/kernel/cap_last_cap before relying on a grant.
Running daemons with capabilities via systemd
The setcap approach works for one-off binaries, but for anything managed by systemd you should hand the capability to the unit rather than the file. This avoids the package upgrade problem entirely and keeps the grant in a configuration file you can version.
[Service]ExecStart=/usr/local/bin/myappAmbientCapabilities=CAP_NET_BIND_SERVICECapabilityBoundingSet=CAP_NET_BIND_SERVICEUser=myappNoNewPrivileges=trueCapabilityBoundingSet restricts what the process can ever hold, and AmbientCapabilities raises the listed capabilities into the process’s ambient set so they survive the exec into the binary without any file capability. Combined with User=myapp, this gives you a durable grant with no setcap call and no root-owned binary. Reload and restart:
sudo systemctl daemon-reloadsudo systemctl restart myappsudo systemctl show myapp | grep CapThe CapabilityBoundingSet= line is the security boundary. Leave it out and the process still gets a wide default set. Setting it to a single capability plus NoNewPrivileges=true is stricter than any file-based capability grant, because the process cannot escalate to anything outside the mask even if it is compromised.
A repeatable audit script
The audit you can trust is one that runs itself. Save this to /usr/local/sbin/cap-audit and schedule it weekly:
#!/usr/bin/env bashset -euo pipefail
BASELINE=/var/lib/capability-audit/expected.txt[ -f "$BASELINE" ] || { echo "missing baseline: $BASELINE" >&2; exit 1; }CURRENT=$(mktemp)trap 'rm -f "$CURRENT"' EXIT
/usr/sbin/getcap -r / 2>/dev/null | \ grep -v -E '^/proc|^/sys|^/run' | \ sort > "$CURRENT"
if ! diff -q "$BASELINE" "$CURRENT" >/dev/null; then echo "Capability drift detected on $(hostname) at $(date -Is)" | \ mail -s "cap-audit: drift" root diff -u "$BASELINE" "$CURRENT" | mail -s "cap-audit: diff" rootfiSeed the baseline once on a known-good system, then review every diff manually before accepting it. On a system with frequent package upgrades, diff noise is real, so pipe the output through a filter that ignores known packages or scope the audit to /usr/local and /opt where manual grants live. Both approaches are valid; the point is that an unexplained change gets a human review. The script uses mail — mailutils on Debian/Ubuntu, s-nail on RHEL — so point it at your alerting channel if neither is installed.
What to do with the audit results
Three outcomes from the baseline diff. The first is an unexplained new grant on a file you did not install. Treat it as a possible compromise and check ls -l --time-style=full-iso, then dpkg -S or rpm -qf for the owning package. The second is a missing grant on a binary that used to work. That usually means an upgrade replaced it and the postinst did not re-apply the caps. The third is an expected grant that has grown wider than it was, for example a file that used to have cap_net_bind_service=ep and now shows cap_net_bind_service,cap_net_raw=ep. That is either a deliberate change or tampering.
For every grant that survives review, ask two questions. Does the file need this capability to do its documented job, and does the file’s owner and mode make it safe against tampering by a non-root user? A capability grant on a world-writable file is a privilege escalation primitive. Check permissions with ls -l and verify the parent directory is not writable by anyone the running user can influence.
Set a policy: every capability file must be tied to a package or a systemd unit that re-applies it, and every grant without that tie should be replaced with an AmbientCapabilities= line. That policy alone eliminates most of the drift.
Limits of file capabilities
File capabilities are not a sandbox. They restrict what a binary gains at exec, not what it can do once running. A process with CAP_NET_BIND_SERVICE that later calls into a library with a deserialization bug can be exploited within the scope of that capability. A process with CAP_SYS_ADMIN, even one intended only to mount a tmpfs, has enough reach to escape most container boundaries. Use the narrowest possible set and combine it with seccomp, AppArmor or SELinux, and NoNewPrivileges=true.
They are also invisible to most security scanning tools. lynis, tiger, and OpenSCAP have partial coverage at best, and none of them audit security.capability by default. The audit script above covers the gap that those tools leave. Run it after every upgrade window, review the diff, and keep the baseline in version control next to the rest of your host configuration.