Linux Capabilities Explained: Drop Root Privileges Without Breaking Your Apps


Most Linux programs that ask for root don’t actually need root. A web server needs to listen on port 80. A ping tool needs to send raw network packets. A backup script needs to read protected files. Each of those is one small superpower — but the traditional answer has been to hand over the entire crown: full root access, every power, no limits.

Linux capabilities fix that. They split root’s power into about 40 separate pieces, so you can give a program exactly the one piece it needs and nothing else.

How capabilities work

Instead of one all-or-nothing root, the kernel tracks individual capabilities such as:

  • CAP_NET_BIND_SERVICE — bind to ports below 1024
  • CAP_NET_RAW — send raw packets (what ping needs)
  • CAP_SYS_ADMIN — a broad admin power, close to root
  • CAP_DAC_OVERRIDE — bypass file permission checks

A process runs with a set of capabilities. If it doesn’t have the one it needs, the kernel says no — even if the user is root in every other sense. And a program can hold one capability while being an ordinary, unprivileged user in all other respects.

Seeing capabilities in action

Check what a binary can do:

getcap /usr/bin/ping

On most modern systems you will see something like:

/usr/bin/ping cap_net_raw=ep

That means ping runs as a normal user but carries the cap_net_raw capability — exactly the one power it needs, and nothing else. No setuid root binary, no full privileges.

Giving a program one power

Say you have a small web server binary that needs port 80 but should not run as root:

sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/myserver

Now it can bind to low ports while running as an unprivileged user. If it gets compromised, the attacker gets a low-privilege process with one narrow power — not a root shell.

Removing powers you don’t need

Capabilities also work in reverse. You can strip powers from a program:

sudo setcap 'cap_net_raw=-ep' /usr/bin/some-tool

And you can inspect any file’s capabilities with getcap -r /usr/bin to audit what is carrying extra power on your system. Anything with cap_sys_admin or a long list deserves a second look.

Why this matters for security

Privilege escalation is the attacker’s favourite next step after getting in. A program running as root that gets compromised hands over the whole machine. A program running with one capability hands over almost nothing. That is the principle of least privilege in practice: give every process the smallest set of powers that lets it do its job, and take the rest away.

Next time a tutorial tells you to run something as root, stop and ask: which single power does it actually need? More often than not, one capability is the whole answer.

Comments

Popular Posts