On one particular machine I often need to run sudo
commands every now and then.
I am fine with entering password on sudo
in most of the cases.
However there are three sudo
commands I want to run without entering password:
sudo reboot
sudo shutdown -r now
sudo shutdown -P now
How can I exclude these commands from password protection to sudo
?
Use the
NOPASSWD
directiveYou can use the
NOPASSWD
directive in your/etc/sudoers
file.If your user is called
user
and your host is calledhost
you could add these lines to/etc/sudoers
:This will allow the user
user
to run the desired commands onhost
without entering a password. All othersudo
ed commands will still require a password.The commands specified in the
sudoers
file must be fully qualified (i.e. using the absolute path to the command to run) as described in thesudoers
man page. Providing a relative path is considered a syntax error.If the command ends with a trailing
/
character and points to a directory, the user will be able to run any command in that directory (but not in any sub-directories therein). In the following example, the useruser
can run any command in the directory/home/someuser/bin/
:Note: Always use the command
visudo
to edit thesudoers
file to make sure you do not lock yourself out of the system – just in case you accidentally write something incorrect to thesudoers
file.visudo
will save your modified file to a temporary location and will only overwrite the realsudoers
file if the modified file can be parsed without errors.Using
/etc/sudoers.d
instead of modifying/etc/sudoers
As an alternative to editing the
/etc/sudoers
file, you could add the two lines to a new file in/etc/sudoers.d
e.g./etc/sudoers.d/shutdown
. This is an elegant way of separating different changes to thesudo
rights and also leaves the originalsudoers
file untouched for easier upgrades.Note: Again, you should use the command
visudo
to edit the file to make sure you do not lock yourself out of the system:This also automatically ensures that the owner and permissions of the new file is set correctly.
If
sudoers
is messed upIf you did not use
visudo
to edit your files and then accidentally messed up/etc/sudoers
or messed up a file in/etc/sudoers.d
then you will be locked out ofsudo
.The solution could be to fix the files using
pkexec
which is an alternative tosudo
.To fix
/etc/sudoers
:To fix
/etc/sudoers.d/shutdown
:If the ownership and/or permissions are incorrect for any
sudoers
file, the file will be ignored bysudo
so you might also find yourself locked out in this situation. Again, you can usepkexec
to fix this.The correct permissions should be like this:
Use
pkexec
like this to fix ownership and permissions:Sorry but there's so much confusion over this and some really complicated answers, that i feel i must weigh in here before someone misunderstands and does something crazy.
Using visudo!!
Add the following lines to the config:
This allows the commands, reboot and shutdown with any parameters to be executed from any user. The first "ALL" refers to the users, so it means ALL users. The second ALL refers to ALL hosts.
For a more verbose explanation see
man sudoers
as this provides further examples and these examples are several pages down but they are actually there if you dig deep enough.Please stackexchange, just give simple, succinct answers.