How to Audit File Capabilities Using getcap in Linux

Introduction to Linux Capabilities

Traditionally, granting an executable elevated privileges required assigning the SUID bit, exposing the entire operating system to potential root-level compromise. Linux capabilities solve this security dilemma by partitioning monolithic superuser authority into discrete, granular permissions. This walkthrough explores the getcap utility, demonstrating how to audit existing binary privileges, evaluate assigned attributes, and implement best practices for least-privilege system hardening.

Installing libcap Utilities and Verifying getcap

The getcap utility ships as part of the libcap2-bin package on Debian and Ubuntu distributions. Update your local package repositories and install the toolkit with the following command:

Updating apt package indices and installing libcap2-bin

Confirm that the binary is accessible in your environment’s $PATH by checking its installation path with which getcap:

Locating the getcap binary path

System administrators rely on getcap to inspect the extended attributes (security.capability) associated with executable binaries. Running getcap with verbose output (-v) against /bin/ping reveals that it carries cap_net_raw=ep. This specific privilege permits unprivileged users to open raw ICMP sockets without demanding full root access.

⚙️ [Essential Command Flags]

-v : Enables verbose mode, displaying every scanned file even if no capabilities are assigned.
-r : Recursively traverses directories to catalog capability sets across the filesystem tree.
-n : Outputs security user IDs (UIDs) as numeric values rather than attempting namespace resolution.

Inspecting capabilities on ping with verbose output

Recursively Auditing System Binaries for File Capabilities

To audit all binaries under /usr/bin simultaneously, execute sudo getcap -r /usr/bin 2>/dev/null. Redirecting standard error (2>/dev/null) suppresses unreadable path warnings, allowing a clean inventory. In standard Ubuntu environments, /usr/bin/ping surfaces immediately with cap_net_raw=ep, granting it the necessary network privileges.

Running a recursive getcap scan across usr bin

Next, audit /bin with sudo getcap -r /bin 2>/dev/null. When the terminal returns without output, it indicates that no executable files within this directory carry extended capability attributes.

Scanning the bin directory with getcap

Hands-On Exercise: Assigning and Verifying Capabilities with setcap

Create an isolated workspace so you can test capabilities safely without impacting active system files. Set up a temporary practice directory using mkdir -p ~/cap_practice && cd ~/cap_practice:

Creating and entering the practice workspace

Copy the system ping binary into your local directory via cp /bin/ping ./my_ping to serve as our sandbox target:

Copying ping binary into the local folder

Inspect the newly created copy using getcap ./my_ping. Notice that standard file copying omits extended attributes by default, meaning our cloned binary currently possesses zero assigned capabilities and outputs nothing:

Checking capability attributes on cloned binary

Now, attach raw network socket capabilities to this copy by invoking sudo setcap cap_net_raw+ep ./my_ping. The +ep flag assigns cap_net_raw to both the effective (e) and permitted (p) sets:

Assigning cap_net_raw to practice binary using setcap

Validate that the attribute was successfully registered by running getcap ./my_ping. The terminal displays ./my_ping cap_net_raw=ep, verifying that raw packet manipulation is now granted directly to the binary:

Verifying effective capabilities on practice binary

Revoking Capabilities, Final Verification, and Cleanup

When a binary no longer requires specialized privileges, strip its assigned capabilities using sudo setcap -r ./my_ping. Passing the -r (remove) flag clears all capability entries from the file’s extended metadata:

Removing file capabilities with setcap -r

Confirm the teardown by querying the file once more with getcap ./my_ping. With the capabilities successfully eradicated, the command produces no output, proving the binary has reverted to an unprivileged state:

Confirming capability removal on local binary

Finally, purge the practice directory and its contents from your user account by running cd ~ && rm -rf ~/cap_practice:

Deleting the practice workspace directory

Understanding how to audit and manage granular file capabilities using getcap is an indispensable skill for hardening Linux environments. By replacing overly broad SUID root permissions with focused, fine-grained capabilities, you can significantly diminish the attack surface of your servers while keeping essential applications fully functional.