Skip to content

Instantly share code, notes, and snippets.

@shreewatsa
Last active February 22, 2023 05:15
Show Gist options
  • Save shreewatsa/0ba9d3453bf4d4f017290df8c9b318aa to your computer and use it in GitHub Desktop.
Save shreewatsa/0ba9d3453bf4d4f017290df8c9b318aa to your computer and use it in GitHub Desktop.
Run commands and scripts requiring sudo without typing the sudo password

Problem:

I want to shutdown my OS without typing the sudo password.

Solution:

Create a new file in /etc/sudoers.d/ directory.

Note that, any files under this directory is sourced in the /etc/sudoers file. And, always use visudo to edit sudoers files, as it safeguards you from getting locked out due to bad sudo configurations.

$ sudo visudo -f /etc/sudoers.d/nosudopassword;  # any filename works, just a matter of choice.

ALL ALL= (root) NOPASSWD: /sbin/shutdown,/usr/local/bin/someOtherScript

Note: Always use absolute path of the commands/scripts. Find the absolute path using

$ which shutdown;

Conclusion:

Now, you can type $ sudo shutdown; and your OS will turn off right away.

Extras:

Command alias in sudoers file is as given below (not tested)
Cmnd_Alias  MYSCRIPT = /usr/local/bin/someOtherScript
%adm       ALL = (ALL) MYSCRIPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment