A web server needs port 80 and nothing else root can do. A monitoring agent needs raw sockets for ping and nothing else. The old answer was to make the binary setuid root and trust it to drop everything it did not need, which worked exactly as well as the program’s authors remembered to do it. The current answer is to give the file the one capability it needs — cap_net_bind_service, cap_net_raw — and nothing else, so that the program starts with a single extra permission rather than all of them. setcap writes that grant onto a file, getcap reads it back, and the two together are how a modern distribution ships ping without a setuid bit. They are also one of the easiest things on a system to lose by accident, which is what most of this page is about.
sudo setcap cap_net_bind_service=ep /usr/local/bin/myserver
getcap /usr/local/bin/myserver
getcap -r / 2>/dev/nullThe first grants one capability to one file; =ep means it goes into the program’s effective and permitted sets the moment it starts, which is what you want for a program that does not know about capabilities and simply expects the bind to work. The second shows what a file carries. The third walks the whole system and lists every file that carries anything — on a fresh install that is a handful of lines, and anything you do not recognise on it deserves a question. Measured, with a copy of the Python interpreter standing in for a server, run by an ordinary user:
$ cp /usr/bin/python3 ./py && sudo setcap cap_net_bind_service=ep ./py
$ getcap ./py
./py cap_net_bind_service=ep
$ su tester -c './py -c "import socket; s=socket.socket(); s.bind((\"127.0.0.1\",80)); print(\"bound :80 as\", __import__(\"os\").getuid())"'
bound :80 as 30033
$ sudo setcap -r ./py
$ su tester -c './py -c "import socket; socket.socket().bind((\"127.0.0.1\",80))"'
PermissionError: [Errno 13] Permission denied
$ getcap -r /usr 2>/dev/null
/usr/lib/x86_64-linux-gnu/gstreamer1.0/gstreamer-1.0/gst-ptp-helper cap_net_bind_service,cap_net_admin,cap_sys_nice=epWith the capability, uid 30033 binds a privileged port; setcap -r removes it and the same call is refused. The system-wide listing on this minimal machine finds one file; a desktop finds ping, mtr-packet, a few helpers, and whatever you added. Run it occasionally. It is the capability equivalent of find / -perm -4000, and a file on it that you did not put there is worth the same attention as an unexpected setuid binary.
The grant that disappears
A file capability is an extended attribute, security.capability, and the kernel clears it whenever the file is written to or changes owner — which is the right thing for security and a constant surprise in practice. A plain cp does not copy it, tar --xattrs does not archive it, and a deploy that overwrites the binary in place silently turns a working server into “permission denied” on its next restart. Measured:
$ getcap py
py cap_net_bind_service=ep
$ cp py py2; getcap py2
$ cp --preserve=xattr py py3; getcap py3
py3 cap_net_bind_service=ep
$ echo >> py4; getcap py4 # had the cap before the write
$ chown tester py4; getcap py4 # had it before the chown
$ tar --xattrs -cf t.tar py; tar --xattrs -xf t.tar -C out; getcap out/py
$ tar --xattrs --xattrs-include='*' -cf t.tar py; tar --xattrs --xattrs-include='*' -xf t.tar -C out2; getcap out2/py
out2/py cap_net_bind_service=ep
$ getfattr -d -m security.capability py
# file: py
security.capability=0sAQAAAgAEAAAAAAAAAAAAAAAAAAA=Every blank line is a lost capability: the plain copy, the file that was appended to, the file whose owner changed, and the archive made with --xattrs alone — GNU tar’s default include list leaves security.* out, so --xattrs-include='*' (or =security.capability) is needed on both the create and the extract. cp --preserve=xattr (or -a on recent coreutils) keeps it. The practical rule: a capability is set on a file as the last step of putting that file in place, every time — in the deploy script after the copy, in the package’s postinst, in the Ansible task that follows the one that copies the binary — and never assumed to survive from the previous version.
Options and syntax
| Form | Meaning |
|---|---|
setcap CAPS FILE | Replace the file’s capability set with CAPS; needs root (strictly, cap_setfcap) |
cap_net_bind_service=ep | The capability in the permitted (p) and effective (e) sets: granted at exec and already active. The form for programs that know nothing about capabilities |
cap_net_bind_service=p | Permitted only: the program must raise it into its effective set itself (cap_set_proc(), prctl); for programs written to use capabilities deliberately |
cap_a,cap_b=ep / cap_a=ep cap_b=p | Several at once; clauses are space-separated, capabilities in a clause comma-separated |
=ep +ep -ep | = sets exactly these flags; + adds flags to the capability, - removes; all=ep is every capability, which is root by another name |
setcap -r FILE | Remove the capability attribute entirely |
setcap -v CAPS FILE | Verify: exit 0 if the file carries exactly CAPS, 1 and a one-line diff otherwise — the form for config management and tests |
setcap -q / -n ROOTID | Quiet / namespaced capability for a user-namespace root (rare) |
getcap FILE… | Print each file’s capability; prints nothing for a file without one |
getcap -r DIR | Recurse; 2>/dev/null hides the “operation not supported” noise from /proc and friends |
getcap -v | Verbose: print every file, with or without a capability |
getpcaps PID | The capability sets of a running process (what the file grant turned into) |
capsh --print | The current process’s sets and bounding set; capsh --decode=HEX turns a /proc/PID/status mask into names |
capsh --drop=cap_x -- -c CMD | Run a command with capabilities removed from the bounding set; --caps= sets them precisely |
Why the grant goes on the binary, never the script
File capabilities, like the setuid bit, are honoured only for a file the kernel executes directly. A script starts with #!, so what the kernel executes is the interpreter, and the script’s own attributes are ignored. Measured, with a two-line Python script that binds port 80:
$ sudo setcap cap_net_bind_service=ep ./bind.py; getcap ./bind.py
./bind.py cap_net_bind_service=ep
$ su tester -c /srv/lab/bind.py
PermissionError: [Errno 13] Permission denied
$ sed -i 's#/usr/bin/python3#/srv/lab/py#' bind.py # interpreter copy that carries the cap
$ su tester -c /srv/lab/bind.py
bound :80
$ su tester -c '/srv/lab/py -c "import socket; socket.socket().bind((\"0.0.0.0\",443)); print(\"443 too\")"'
443 toosetcap on the script succeeds — it is only writing an attribute — and does nothing. Pointing the script at an interpreter that carries the capability makes it work, and the last line is the cost: every script, one-liner and REPL run through that interpreter copy can now bind any privileged port. The grant is on the binary, so it is for everyone who can run the binary, with any arguments. That is acceptable on a dedicated copy that only one service executes (and the copy must be exactly that: not /usr/bin/python3, which half the system runs), and it is the reason a capability on a general-purpose interpreter, shell or busybox is a privilege escalation waiting for a user. For a service, the cleaner answer is usually not a file capability at all but the unit file: AmbientCapabilities=CAP_NET_BIND_SERVICE with User= gives the capability to that one process, started by systemd, and to nothing else on disk.
Reading what a process ended up with
A file grant is a request; the sets a process actually holds are in /proc/PID/status as hex masks, and capsh --decode turns a mask into names. The sandbox this page was measured in, for instance, runs as root with one capability missing:
$ grep Cap /proc/self/status
CapInh: 0000000000000000
CapPrm: 000001fffeffffff
CapEff: 000001fffeffffff
CapBnd: 000001fffeffffff
CapAmb: 0000000000000000
$ getpcaps $$
497: =ep cap_sys_resource-ep
$ capsh --decode=0000000000000400
0x0000000000000400=cap_net_bind_servicegetpcaps says it in words: everything, minus cap_sys_resource, which the sandbox removed from the bounding set so that nothing inside can raise resource limits. That is the same mechanism a container runtime uses to run a “root” that cannot load modules or change the clock, and capsh --print inside any container shows exactly what was taken away. When a program with a file capability still fails, this is where to look: if the capability is not in the bounding set of the process that starts it, the file grant cannot add it back.
Where it does not reach
Capabilities carve root into about forty pieces, and a few of those pieces are nearly the whole: cap_sys_admin covers mounting, namespaces and much else, cap_dac_override bypasses file permissions, cap_sys_ptrace reads other processes’ memory, cap_setuid becomes anyone. A file carrying one of those is as sensitive as a setuid-root binary and should be treated as one; the gain from capabilities comes from the small ones — cap_net_bind_service, cap_net_raw, cap_sys_nice, cap_chown — and from giving a program two of them rather than all forty. They also do nothing about what a program does with its ordinary permissions, which is the half of the job that User=, ProtectSystem= and the rest of a hardened unit handle. And they travel badly: NFS and most network filesystems, FAT, and some backup tools do not carry the attribute, so a binary that works on local disk can lose its grant on the way to another host.
Common problems
| Symptom | Cause | Check / fix |
|---|---|---|
| Service worked until the last deploy, now “permission denied” on bind | The binary was overwritten; the write cleared the attribute | getcap the file; put setcap after the copy in the deploy |
cp or tar of the binary has no capability | Attribute not preserved | cp --preserve=xattr; tar --xattrs --xattrs-include='*' on create and extract; rsync -X |
setcap succeeds on a script, script still fails | Only the interpreter is executed by the kernel | Grant the (dedicated) interpreter, or use a systemd unit with AmbientCapabilities= |
setcap: Operation not supported | Filesystem without xattr support (FAT, some network mounts, tmpfs on old kernels) | Install the binary on a local ext4/xfs/btrfs path |
setcap: Operation not permitted as root | Caller lacks cap_setfcap — inside a container, usually | capsh --print; set the capability when building the image, not at runtime |
| File has the capability, process does not | Capability not in the parent’s bounding set, or dropped by NoNewPrivileges=/no_new_privs | getpcaps the parent; check the unit; CapabilityBoundingSet= |
getcap -r / floods with “Operation not supported” | /proc, /sys, special filesystems | 2>/dev/null, or -r /usr /opt /srv |
| Package upgrade removed a capability you set by hand | The new file replaced yours | Prefer the unit-file route; or a dpkg trigger / Ansible task that re-applies it |
Program with =p only still cannot do the thing | Permitted but not effective; the program never raised it | Use =ep for programs that are not capability-aware |
Quick reference
sudo setcap cap_net_bind_service=ep /path/bin # bind ports below 1024 without root
sudo setcap cap_net_raw=ep /path/bin # raw sockets (ping, tcpdump-style capture)
sudo setcap 'cap_net_raw,cap_net_admin=ep' /path/bin # two at once
sudo setcap -r /path/bin # remove
setcap -v cap_net_bind_service=ep /path/bin # verify (exit 0/1); for tests and CM
getcap /path/bin # what one file carries
getcap -r / 2>/dev/null # every file on the system that carries one
getfattr -d -m security.capability /path/bin # the raw attribute
cp --preserve=xattr src dst # copies that keep it
tar --xattrs --xattrs-include='*' -cf x.tar bin # archives that keep it (and -xf the same way)
getpcaps PID # what a running process holds
capsh --print # this shell's sets and bounding set
capsh --decode=$(awk '/CapEff/{print $2}' /proc/PID/status) # decode a status mask
# systemd alternative, in the unit: User=svc AmbientCapabilities=CAP_NET_BIND_SERVICERelated reading
- Permissions and Privilege — the long one on what a capability is, the five sets, and how they combine with setuid at
execve() - Hardening a Public-Facing Service —
AmbientCapabilities=,CapabilityBoundingSet=and the rest of the unit-file route that usually beats a file grant - The Privilege-Escalation Decade — why the list
getcap -r /prints deserves the same audit as the setuid list - nsenter, unshare and lsns — the bounding set a container runtime trims, seen from inside
- chmod, chown and umask — the ordinary permissions a capability sits on top of, and the
chownthat clears it - tar — the
--xattrsflags that do and do not carry the grant

