防止 sudo 在运行不允许的命令时提示输入密码

use*_*982 16 sudo

我已授予组权限以通过 sudo 运行某些命令而无需密码。当其中一个用户打错字或运行错误的命令时,系统会提示他们输入密码,然后他们会收到错误消息。这让用户感到困惑,所以我只想显示一个错误而不是提示他们输入密码。这可能吗?

这是我的 sudoers 文件的示例:

%mygroup ALL=(ALL) NOPASSWD:/usr/local/bin/myscript.sh *
Run Code Online (Sandbox Code Playgroud)

当他们运行错误的脚本时的示例:

# 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>.
Run Code Online (Sandbox Code Playgroud)

期望的输出:

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

注意缺少密码提示。

我的谷歌福失败了我,只有在当用户不要求输入密码返回的结果允许运行的命令。

thr*_*rig 27

从快速阅读 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.
Run Code Online (Sandbox Code Playgroud)

对于怀疑者:

# grep jdoe /etc/sudoers
jdoe    ALL=(ALL) NOPASSWD: /bin/echo
#
Run Code Online (Sandbox Code Playgroud)

如此测试:

% 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:
Run Code Online (Sandbox Code Playgroud)

因此aliassudo对于这些人来说,for 可能会起作用,以防止密码提示。现在为什么这需要自定义编译sudo,我不知道,我只是阅读了手册。


meu*_*euh 7

对我有用的一件事(Sudo 版本 1.8.17p1),但仅满足您的部分问题,是将密码尝试次数设置为 0。

Defaults:%mygroup passwd_tries = 0
Run Code Online (Sandbox Code Playgroud)

当尝试任何需要密码的命令时,这会使 sudo 以代码 1 退出。但是,它不会产生任何类型的错误消息。