Linux capabilities
How Linux splits root into 41 separate privileges, how to grant one with setcap or AmbientCapabilities=, and how to read a process's capability sets.
Special permission bits and umaskProcesses through /procSandboxing services with systemd
Linux capabilities
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Instead of one all-powerful root, Linux divides root's privileges into capabilities, such as `CAP_NET_BIND_SERVICE` for ports below 1024. Current kernels define 41, numbered 0 to 40.
- A process has several capability sets. The kernel checks the effective set. Permitted is the ceiling. Bounding limits what can ever be gained. Ambient carries capabilities across `execve` for non-root programs.
- `setcap cap_net_bind_service=+ep /path/to/binary` gives a file a capability, the way setuid gives it all of root. `getcap` shows it.
- For a systemd service, `User=app` plus `AmbientCapabilities=CAP_NET_BIND_SERVICE` lets an unprivileged process bind port 443 without touching the binary.
- `CAP_SYS_ADMIN` is so broad that the man page calls it overloaded. Granting it is close to granting root.
- `/proc/<pid>/status` shows each set as a hex bitmask. `capsh --decode` turns the mask into names.
Root is too big a key
A web server needs to listen on port 443. On Linux, ports below 1024 are privileged, so traditionally the server started as root, bound the port, and then dropped to an ordinary user. ping needs raw sockets, so it was setuid root. Every such program ran, for at least a moment, with the power to read every file, load kernel modules and reboot the machine, when it needed exactly one thing.
The capabilities man page (man-pages 6.19) describes the fix: Linux “divides the privileges traditionally associated with superuser into distinct units, known as capabilities”, which can be turned on and off separately. Think of a building where the master key has been replaced by a ring of separate keys. The cleaner gets the storeroom key and nothing else.
A few capabilities worth knowing
The kernel header include/uapi/linux/capability.h in the current mainline tree (7.4-rc4) defines capabilities 0 to 40, CAP_CHECKPOINT_RESTORE being the last. Some of them, with the man page’s wording:
| Capability | Allows |
|---|---|
CAP_NET_BIND_SERVICE |
binding to ports below 1024 |
CAP_NET_RAW |
raw and packet sockets, as ping and tcpdump need |
CAP_DAC_OVERRIDE |
bypassing file read, write and execute permission checks |
CAP_CHOWN |
arbitrary changes to file owners and groups |
CAP_SYS_ADMIN |
a huge grab bag of administration |
The man page says CAP_SYS_ADMIN “is overloaded” and advises: “Don’t choose CAP_SYS_ADMIN if you can possibly avoid it!” Treat a process holding it as root.
The sets
Each thread carries several capability sets. In the man page’s terms:
- Effective: what the kernel actually checks when the process tries something.
- Permitted: the ceiling. A process can raise into effective only what is permitted.
- Inheritable: what may be kept across
execvein combination with file capabilities. - Bounding: the absolute limit on what can be gained at
execve. Dropping a capability from the bounding set is permanent for that process and its children. - Ambient (since Linux 4.3): capabilities kept across
execveof an ordinary, non-setuid program. This is what makes “a normal user with one capability” practical.
The man page also notes that when a process with non-zero user IDs calls execve, its permitted and effective sets are cleared unless something, such as file or ambient capabilities, puts them back.
Reading a process’s capabilities
The /proc lesson showed /proc/<pid>/status. It also lists the sets as hex bitmasks:
grep Cap /proc/1234/status # CapInh, CapPrm, CapEff, CapBnd, CapAmb
capsh --decode=0000000000000400
getpcaps 1234
Each bit is one capability number. I checked the arithmetic in Python rather than on a Linux box:
$ python3 -c "print(hex(1<<10)); print(hex((1<<41)-1))"
0x400
0x1ffffffffff
So CapEff: 0000000000000400 is bit 10, CAP_NET_BIND_SERVICE alone, and a full root process on a kernel with 41 capabilities shows 000001ffffffffff. /proc/sys/kernel/cap_last_cap gives the highest number the running kernel knows.
Granting one capability
On the file. File capabilities are stored in an extended attribute, much like the setuid bit from special permission bits but far narrower:
sudo setcap cap_net_bind_service=+ep /usr/local/bin/web
getcap /usr/local/bin/web
getcap -r /usr/bin 2>/dev/null # audit what has capabilities
+ep means add to permitted and set the effective bit, so the capability is live as soon as the program starts. Two catches. Replacing the binary, as a package upgrade does, drops the attribute. And a file capability is still a privilege gain at execve, so it is ignored under no-new-privileges.
On the service. For a systemd service, give the capability to the process instead of the file:
[Service]
User=web
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
NoNewPrivileges=yes
systemd.exec says ambient capabilities “are useful if you want to execute a process as a non-privileged user” and that systemd keeps them across the user switch. The bounding set line makes sure nothing else can ever be gained. This fits the rest of sandboxing with systemd, and it survives package upgrades.
For ports specifically there is a third route. The kernel’s ip-sysctl document defines net.ipv4.ip_unprivileged_port_start, per network namespace. Lowering it lets every process bind low ports, which is fine inside a container and blunt on a shared host.
Containers use this too
A container’s “root” is usually a root user whose bounding set has been cut down to a dozen or so capabilities. That is one reason --privileged, which restores them all, is dangerous. How containers are built is covered in what a container really is.
Only the Python arithmetic above was run. The setcap, getcap and /proc commands need Linux and are written from capabilities(7), systemd.exec(5) and the kernel header, checked 2026-11-28.
About the video
The embed is all of “Master Linux Capabilities in 10 Minutes FAST!” by The Linux Lighthouse, March 2025, about 10 minutes, demonstrated on openSUSE. Its description lists running a web server on a low port without root as a worked example, with getcap and setcap, which is the core of this lesson. It does not, going by the description, cover the ambient set or the systemd route. The four-minute Hack the Clown video in deeper is a sharp summary of the sets and of decoding masks. I judged the embed from its description, and English from its English caption track.
Where this fits
Capabilities, SELinux and AppArmor limit what a process can do. The audit system records what processes actually did: auditing with auditd.
Check yourself
Your Go web server must listen on port 443 but should not run as root. What are two ways to allow it?
Give the binary the file capability with `setcap cap_net_bind_service=+ep`, or run it under systemd with `User=` and `AmbientCapabilities=CAP_NET_BIND_SERVICE`. The systemd route survives package upgrades that replace the binary.
Why is a binary with `cap_net_raw+ep` safer than the same binary with the setuid bit?
A setuid-root binary runs with all of root's powers, so any bug in it is a full compromise. With one capability, a bug gives the attacker only raw sockets.
A unit has `NoNewPrivileges=yes` and runs a binary that has file capabilities set. Will the capabilities apply?
No. No-new-privileges stops privilege gain through `execve`, which includes file capabilities as well as setuid. Use `AmbientCapabilities=` in the unit instead.
`grep CapEff /proc/1234/status` shows `0000000000000400`. Which capability is that?
Bit 10, `CAP_NET_BIND_SERVICE`, and nothing else. `0x400` is 1 shifted left 10 places.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.