Score:0

Is it possible to allow NOPASSWD on an alias of a command requiring sudo?

in flag

I added this alias:

alias shutdown2h='sudo shutdown -h +120'

which I would like to run without having to enter the password but also without adding NOPASSWD for the whole shutdown command.

Is it possible to add the alias to the sudoers file with the NOPASSWD alltogether? I tried it with the location of the command ~/.bash_aliases/shutdown2h which gave me a syntax error. Or is there another way?

Thank you very much!

waltinator avatar
it flag
Always paste your script into `https://shellcheck.net`, a syntax checker, or install `shellcheck` locally. Make using `shellcheck` part of your development process. Also read `man sudoers visudo`. Since the `alias` is replaced by the expansion by the parent shell before execution, `shutdown2h` doesn't get executed, `sudo shutdown -h +120` does.
Score:3
it flag

You want to ensure that your user is not able to make changes to what is being allowed to run. For example, by attempting to add a script owned by your user (~/.bash_aliases/shutdown2h) to be run as root via sudo without a password, you would allow any arbitrary command to be run. Simply edit that file and run it. It would be the same as allowing the user to run any command via sudoers. For example, the user would edit the file to read now instead of -h +120 and shut down the machine immediately.

Instead, it would be a better idea to add a script owned by the root user to the system, probably in /usr/local/bin/. For example, /usr/local/bin/shutdown2h, executable by everybody, but writable only by root. This way, you ensure that the user can only run the specific script, and that script cannot be changed by them to run something else. The path you place the script should be in your $PATH, which /usr/local/bin should be in by default.

  1. Create the script
sudo vim /usr/local/bin/shutdown2h

#!/bin/bash
shutdown -h +120
  1. Allow execution of the script
sudo chmod +x /usr/local/bin/shutdown2h
  1. Allow USER to run specific script
sudo visudo

# Add this entry
USER ALL=(ALL) NOPASSWD: /usr/local/bin/shutdown2h

Now, your user can run sudo shutdown2h without being asked for a password and be sure that that user will only be able to run shutdown -h +120 without a password.

Also, when making sudoers entries, be sure to provide the full path to the executable/script, and don't use any shortcuts like ~/ or $HOME.

Alin Anca avatar
in flag
Thank you very much!
vanadium avatar
cn flag
" probably in /usr/bin/": make that `/usr/local/bin`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.