Categories
Linux

How to Use ‘Pkexec’ (a sudo alternative) to Run Commands as Root on Linux

sudo is the commonly used program for tasks requiring administrator access in Linux. But in case there is an error with the sudo command or if the sudoers file is invalid, or certain programs are blocked by sudo access, users can make use of a similar command line tool — pkexec.

Advertisements

pkexec runs in similar way as sudo:

pkexec <command> # Where <command> is the program to be run with super user access.

To be able to use pkexec, make sure that either you (or some other user) has the authority to run programs as root on the system. When you run the pkexec command, you’ll be aksed to enter the password for a user who is authorized to run programs with root privileges.

If you’ve physical (GUI) access to the machine, you will even get a GUI prompt to select a user to authenticate root privilege and execute the pkexec command.

Advertisements

Note: This prompt is actually opened by an already registered Authentication Agent by the program. All Policy Kit program register similar agents themselves.

To run the command as some other user, use the --user flag:

pkexec --user <username> <command> # Where <username> is the user to run the <command> as.

Fixing sudoers file using pkexec

pkexec can be a lifesaver when you’ve messed up the sudoers file on your system. You can run the visudo program using pkexec and fix any issues with the sudoers file to restore sudo features.

pkexec visudo

🍻 Cheers!