Sometimes, you may wish to run a command but you wouldn't want to pester the almighty super user (root / sudo) for the password.
This tip will step through how to exempt the command from the password prompt from
sudo like this prompt:
[sudo] password for user:
Be cautious with which programs you wish to exempt from having to enter the password. Some commands could cause damage or put your system at risk if accidentally executed incorrectly.
Some commands like
pm-suspend may just add convenience for your workflow.
Modifying the sudoers file.
1) First, we need to find out the path of the executable. Use the command
which to print its path.
For example, the
shutdown command is at
$ which shutdown
2) Next, open
This is a utility that allows you to properly edit the sudoers file. This is a very important safety measure, because if your sudoers file is invalid, you will lose access to sudo on your system. Thankfully, if you do make a mistake after you save,
visudo will not proceed.
Instead, you should create your own individual file, thanks to this tip by @layingback:
An advantage of saving to
/etc/sudoers.d is that it's easier to preserve copies if you perform a clean installation or if
visudo is overwritten later (for instance, due to an upgrade).
3) At the bottom of the file, add the following:
[tab] indicates an actual tab. Spaces might work.
username with your actual username.
- If you forgot who you are, see the lowercase name of home folder or type
/path/to/executable with the full path to the command as we found out in step 1.
Here's an example for
shutdown for the user
john ALL=NOPASSWD: /sbin/shutdown
When you are done, press CTRL+X to save. You will see a confirmation if your changes have been successfully saved.
Executing the command
Since it's still a privileged command, you still need to have
sudo at the start of the command.
So, let's say you wish to restart your system without entering a password:
sudo shutdown -r now
No sudo password prompt should appear!
Tip: You could bind this as a keyboard shortcut or execute it from within a script and not have to worry of an invisible password prompt.