polkit Explained: Why the Desktop Asks for a Password Instead of sudo
sudo answers “may this user run this program as root”. polkit answers a different question: “may this already-running process ask a privileged service to do this specific thing”. Confusing the two is why people edit sudoers to fix a problem that lives somewhere else entirely.
The model
A privileged system service is already running as root: systemd-logind, NetworkManager, udisks2, packagekit. Your unprivileged application wants it to do something, so it sends a request over D-Bus.
The service does not decide for itself. It asks polkit, passing the action being requested and the subject making the request.
polkit consults its rules and returns one of:
- Allow, proceed
- Deny, refuse
- Authenticate, ask for a password first
If authentication is needed, an authentication agent supplied by your desktop shows a dialog. The answer goes back to the service, not to the application.
That indirection is the whole design. The application never holds privileges and never sees your password. It asks, something else decides, and the privileged service acts.
Why this exists
Before polkit, a graphical tool needing root ran the whole application as root, which meant a browser-sized GUI toolkit executing with full privileges. That is a very large attack surface for the sake of changing a network setting.
polkit lets the GUI stay unprivileged and ask a small, auditable service to do the one privileged thing. Our systemd service hardening guide covers the same principle applied to services.
Seeing it work
# what actions exist on your system
pkaction | head -30
pkaction | wc -l
# the detail of one
pkaction --verbose --action-id org.freedesktop.login1.reboot
org.freedesktop.login1.reboot:
description: Reboot the system
message: Authentication is required to reboot the system
vendor: The systemd Project
implicit any: auth_admin_keep
implicit inactive: auth_admin_keep
implicit active: yes
Read the three implicit lines, because they are the default policy and they explain a great deal of desktop behaviour:
| Context | Meaning |
|---|---|
active | You are at the seat, session in the foreground |
inactive | Logged in, session not in front |
any | Any other case, including remote |
So rebooting from your own graphical session is allowed outright, and rebooting over ssh requires admin authentication. That is not an accident and it is not sudo doing it.
# check an authorization without performing it
pkcheck --action-id org.freedesktop.login1.reboot --process $$
echo $?
The values
| Value | Behaviour |
|---|---|
yes | Allow, no prompt |
no | Deny outright |
auth_self | Authenticate as yourself |
auth_admin | Authenticate as an administrator |
auth_self_keep | As above, remembered briefly |
auth_admin_keep | As above, remembered briefly |
The _keep variants are why you are not asked five times in a row when installing updates.
Who counts as an administrator is configured, and it differs by distribution: the wheel group on Fedora and Arch, sudo on Debian and Ubuntu.
grep -r AdminIdentities /etc/polkit-1/ /usr/share/polkit-1/ 2>/dev/null
Writing a rule
Local rules are JavaScript in /etc/polkit-1/rules.d/.
// /etc/polkit-1/rules.d/49-nopasswd-reboot.rules
polkit.addRule(function(action, subject) {
if (action.id == "org.freedesktop.login1.reboot" &&
subject.isInGroup("operators")) {
return polkit.Result.YES;
}
});
Rules are processed in lexical filename order, and the first rule to return a result wins. A rule returning nothing falls through to the next.
That ordering is the most common reason a rule appears to do nothing. Distribution defaults frequently live at 50- or higher, so a local rule numbered 49- runs first. Number yours deliberately.
# what rules exist, in the order they run
ls /etc/polkit-1/rules.d/ /usr/share/polkit-1/rules.d/
# never edit these, updates overwrite them
ls -l /usr/share/polkit-1/rules.d/
More realistic examples:
// let a group manage one service without sudo
polkit.addRule(function(action, subject) {
if (action.id == "org.freedesktop.systemd1.manage-units" &&
action.lookup("unit") == "myapp.service" &&
subject.isInGroup("deployers")) {
return polkit.Result.YES;
}
});
// mounting internal disks, for a trusted group
polkit.addRule(function(action, subject) {
if (/^org\.freedesktop\.udisks2\.filesystem-mount(-system)?$/.test(action.id) &&
subject.isInGroup("storage")) {
return polkit.Result.YES;
}
});
// deny something outright, regardless of group membership
polkit.addRule(function(action, subject) {
if (action.id == "org.freedesktop.login1.power-off") {
return polkit.Result.NO;
}
});
action.lookup() reads request details, which is what lets a rule target one unit rather than all of them. That granularity is polkit’s real advantage over sudoers for this class of problem.
Debugging
# watch decisions as they happen
sudo journalctl -f -u polkit
# syntax check by restarting and reading the log
sudo systemctl restart polkit
sudo journalctl -u polkit -n 30 --no-pager
A JavaScript syntax error makes polkit ignore the entire file, usually with a message in the journal and no other symptom. Restart and read the log after every edit.
// logging from inside a rule, for working out what is being matched
polkit.addRule(function(action, subject) {
polkit.log("action=" + action.id + " user=" + subject.user);
});
Put that in a high-numbered file temporarily, since it returns nothing and therefore does not interfere. Remove it afterwards; it is noisy.
Our journalctl guide covers filtering the output.
pkexec
pkexec whoami
pkexec --user postgres psql
pkexec is the closest polkit analogue to sudo: run a command as another user, with polkit deciding.
It is reasonable for a desktop application launching a privileged helper, because the authentication dialog comes from the desktop rather than a terminal.
For scripts, prefer sudo. pkexec sanitises the environment aggressively, behaves differently without a session, and is noticeably less predictable in a cron job or a systemd unit. Our sudo guide covers the alternative, and run0 versus sudo-rs covers the newer options.
An application wanting a .policy file of its own installs it to /usr/share/polkit-1/actions/:
ls /usr/share/polkit-1/actions/ | head
Those XML files declare actions and their defaults. Rules in rules.d override them without editing them.
Where it fits
Four mechanisms, frequently confused, each answering a different question:
PAM decides whether you may log in at all.
sudo decides whether you may run a command as another user.
polkit decides whether a running process may request a privileged operation.
Capabilities decide which privileged operations a process may perform itself, without being root.
A problem in one is not fixed by editing another, and working out which you are looking at is most of the diagnosis. The symptom to recognise: a graphical prompt you did not expect, or a request refused with no terminal involved, is polkit.
Frequently Asked Questions
What is the difference between polkit and sudo?
sudo runs a command as another user, deciding per command and per user. polkit decides whether an already running process may ask a privileged system service to perform a specific action, deciding per action with context about the session. sudo is about executing programs, polkit is about requesting operations.
Why does a graphical application ask for my password without a terminal?
Because it is making a polkit-mediated request to a system service over D-Bus, and the policy for that action requires authentication. An authentication agent supplied by your desktop shows the dialog, and the result is returned to the service rather than to the application.
Where do I put polkit rules?
Local rules go in /etc/polkit-1/rules.d as JavaScript files, which take precedence over the defaults in /usr/share/polkit-1/rules.d. Never edit the files under /usr/share, because package updates overwrite them. The older pkla format in /etc/polkit-1/localauthority is deprecated.
Why does my polkit rule have no effect?
Usually the filename sorts after a rule that already returned a result, since the first rule to return wins and files are processed in lexical order. Other common causes are a syntax error in the JavaScript, which makes polkit ignore the whole file, and matching the wrong action id.
What is pkexec and should I use it in scripts?
pkexec runs a command as another user with polkit deciding authorization, so it is roughly the polkit equivalent of sudo. It is reasonable for a desktop application that needs to launch a privileged helper, and sudo remains the better choice in scripts because its behaviour around the environment and terminal is more predictable.
Can polkit authorize without prompting?
Yes. A rule can return yes to allow an action outright, no to deny it, or one of the auth values to require a password. Returning yes for a specific action and group is how you let a particular set of users do one privileged thing without granting them broader access.