On Linux systems, the sudo
command is commonly used to perform tasks requiring administrative privileges. However, if you encounter errors with sudo
, such as an invalid sudoers file or blocked programs, pkexec
offers a reliable alternative for executing commands as the root user.
The pkexec
command allows you to run programs with elevated privileges in a manner similar to sudo
:
pkexec <command>
# Replace <command> with the application or script you wish to run as root.
Before using pkexec
, ensure that you or another user has the authority to execute programs as the root
user on the system. When you run pkexec
, it will prompt you to enter the password of an authorized user who can grant root privileges.
If you're operating on a system with a graphical user interface (GUI), pkexec
will display a GUI prompt, allowing you to select a user for authentication and grant the necessary root permissions to execute the command.
Note: This graphical prompt is provided by an authentication agent registered by PolicyKit. All PolicyKit applications automatically register similar agents.
To execute a command as a different user, utilize the --user
option with pkexec
:
pkexec --user <username> <command>
# Replace <username> with the target user and <command> with the program you wish to run.
Fixing the sudoers file using pkexec
If you've accidentally corrupted your sudoers file and sudo
commands are failing, pkexec
can help you repair the issue. You can launch the visudo
editor using pkexec
to correct any mistakes and restore sudo
functionality.
pkexec visudo
🍻 Happy computing!
Member discussion