Grant a Binary One Capability With setcap Instead of setuid
Use setcap to give a binary only CAP_NET_BIND_SERVICE instead of making it setuid root, and see how the file capability bits change what the program can do.

A small server that must listen on port 443 used to have two bad options. Run it as root, or mark it setuid root and let it start with full power. Both give a bug in the server the keys to the whole machine. File capabilities give a third option: store the one privilege the binary needs in its extended attributes, and the kernel grants only that when it runs. This post covers the same ground as the existing guide to granting file capabilities and reading them in code, but from the other side: why you would replace a setuid bit, what the three flag letters mean, and the failure modes that make setcap look broken. The commands are from libcap 2.66 on Linux 6.12, and the model is in capabilities(7). The background on the sets is in Linux capabilities explained.
What setuid root costs
Mark a file setuid root with chmod u+s and any user who executes it runs with the file owner's identity, UID 0, and the full capability set that implies. The program does not ask for that power; it gets it before main runs. If it only ever needs to bind one port, 40 other capabilities are carried for no reason, and every environment variable, argument and input file is an attack surface for something that is effectively root.
You can list setuid binaries on a machine to see how many exist:
find /usr/bin /usr/sbin -perm -4000 -type f 2>/dev/null | head -4/usr/bin/chfn
/usr/bin/chsh
/usr/bin/gpasswd
/usr/bin/mountYour list will differ. Each entry is a program whose bugs are privilege escalations. The goal is to keep that list short.
The three letters after the capability
setcap writes the security.capability extended attribute. The syntax is setcap capabilities+flags file, where the flags are the letters e, i and p, and they stand for the file's effective bit, inheritable set and permitted set.
p(permitted): the capability is added to the new process's permitted set whatever its parent held.i(inheritable): the capability is added to the permitted set only if the caller also had it in its inheritable set.e(effective): a single bit, not a set. If on, the permitted capabilities start out in the effective set too, so the program works without calling any capability API.
For a server that is not capability-aware, you want ep: permitted and effective. A program that raises capabilities itself with libcap wants only p, and an e is a mistake because it removes the program's chance to run unprivileged until it needs the power.
Do it
Start from a program that binds port 443. This one is a placeholder that does only that, so you can see the file capability matter:
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main(void) {
int fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in a = {0};
a.sin_family = AF_INET;
a.sin_port = htons(443);
a.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd, (struct sockaddr *)&a, sizeof a) < 0) {
fprintf(stderr, "bind: %s\n", strerror(errno));
return 1;
}
printf("listening on 443 as uid %d\n", (int)getuid());
sleep(1);
return 0;
}Build it, run it as an ordinary user, then grant the capability:
cc -o listen443 listen443.c
./listen443
sudo setcap cap_net_bind_service=ep ./listen443
./listen443bind: Permission denied
listening on 443 as uid 1000The second run is the same binary, the same user, and a different outcome. The user ID is still 1000, because file capabilities add power without changing identity. That is the difference from setuid, and the reason a compromise of this process cannot read files that only root may read: it has no CAP_DAC_OVERRIDE.
Read it back
Reading the attribute is how you audit a system:
getcap ./listen443
getcap -r /usr/bin /usr/sbin 2>/dev/null./listen443 cap_net_bind_service=ep
/usr/bin/ping cap_net_raw=epThe -r form walks a tree. A distribution that has moved ping off setuid root shows exactly this line. Running the recursive form over your own /usr is a fast way to find which packages hold privilege and whether you meant them to.
To remove a capability:
sudo setcap -r ./listen443
getcap ./listen443(no output after setcap -r; getcap prints nothing for a file with no capabilities)When setcap seems to do nothing
Four reasons the capability is set but the program still fails, each seen often enough to check first.
The filesystem drops the attribute. File capabilities live in an extended attribute. A filesystem mounted without extended attribute support cannot hold it. Check with getcap right after setcap; if the line is missing, the attribute never stuck.
Copying the file loses it. cp does not carry extended attributes by default, so a deployment script that copies the binary silently produces an unprivileged one. Use cp --preserve=xattr or run setcap as the last step on the destination.
**The mount is nosuid.** The kernel ignores file capabilities on a filesystem mounted with nosuid, the same as it ignores setuid bits. Containers and hardened /tmp mounts often set it.
Scripts cannot hold them. The kernel does not honor capabilities on interpreted scripts started with #!. Put them on the interpreter's binary, which grants them to every script, or restructure so a small compiled helper holds the privilege.
A fifth reason applies to dynamically linked programs: when a binary has file capabilities, the dynamic loader switches to secure-execution mode and ignores LD_LIBRARY_PATH and similar variables, as it does for setuid files. If your program relied on one of those, it will break, and that is the loader doing its job.
Changing the binary breaks the grant
Writing to a file clears its security.capability attribute, as it clears the setuid bit. This is a defense: an attacker who can modify the binary cannot keep its privilege. It also means that a package manager upgrading the file must reapply the capability, which well-behaved packages do in a post-install script. If your own deploy replaces the binary, add setcap to it and verify with getcap as a deploy check.
A decision rule
Use file capabilities when a program needs a small, fixed set of privileges for its whole life and you control its install path. Prefer the systemd AmbientCapabilities= directive for a service, which grants the capability at start-up without touching the file, so there is nothing to lose on upgrade. Keep setuid for the rare program that really must run as another user's identity, and then write it to drop privileges as its first action.
Checking that the grant is what you meant
After setcap, read the result with getcap and then look at the running process with grep '^Cap' /proc/PID/status. The file line tells you what the binary can request, and the process lines tell you what it actually holds. For cap_net_bind_service=ep you should see 0000000000000400 in the permitted and effective masks of the running process and zero in the inheritable and ambient masks. If the effective mask is zero while the permitted mask is set, the e flag is missing from the file and the program must raise the capability itself. If both are zero, the kernel ignored the attribute, and the causes in the previous section apply. Add this check to your deployment script as an assertion, so a copy or a package update that strips the attribute fails the release instead of failing at midnight when the service restarts.


