Sudo – Prevent Prompting for Password When Running Non-Permitted Command

sudo

I have granted a group permission to run certain commands with no password via sudo. When one of the users makes a typo or runs the wrong command the system prompts them for their password and then they get an error. This is confusing for the user so I'd like to just display an error instead of prompting them for a password. Is this possible?

Here is an example of my sudoers file:

%mygroup ALL=(ALL) NOPASSWD:/usr/local/bin/myscript.sh *

Example when they run the wrong script:

# sudo /usr/local/bin/otherscript.sh
[sudo] password for user:
Sorry, user user is not allowed to execute '/usr/local/bin/otherscript.sh' as root on <hostname>.

Desired output:

Sorry, user user is not allowed to execute '/usr/local/bin/otherscript.sh' as root on <hostname>. Please check the command and try again.

Note the lack of password prompt.

My google-fu has failed me and only returns results on not asking for a password when the user is permitted to run the command.

Best Answer

From a quick read of sudo(8)

   -n          The -n (non-interactive) option prevents sudo from
               prompting the user for a password.  If a password is
               required for the command to run, sudo will display an error
               message and exit.

And for the doubters:

# grep jdoe /etc/sudoers
jdoe    ALL=(ALL) NOPASSWD: /bin/echo
#

Tested thusly:

% sudo echo allowed
allowed
% sudo -n ed             
sudo: a password is required
% sudo ed               

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

Password:

So an alias for sudo for these folks would likely do the trick, to prevent the password prompt. Now why this requires custom compiling sudo, I don't know, I just read the manual.

Related Question