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:
Precaution!
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 shutdown
and 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 /sbin/shutdown
:
$ which shutdown /sbin/shutdown
2) Next, open visudo
.
sudo visudo
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:
username
[tab]ALL=NOPASSWD:
[tab]/path/to/executable
- [tab] indicates an actual tab. Spaces might work.
- Replace
username
with your actual username. - If you forgot who you are, see the lowercase name of home folder or type
whoami
. - Replace
/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
:
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.