在 /etc/sudoers 中添加 NOPASSWD 不起作用

Zac*_*Zac 51 root sudo password

14.04 在这里。我通过 SSH 连接到我的机器,将以下行添加到/etc/sudoers

myuser   ALL=NOPASSWD: ALL
Run Code Online (Sandbox Code Playgroud)

然后尝试运行:

sudo mkdir /etc/blah
Run Code Online (Sandbox Code Playgroud)

...我被要求输入密码。为什么?!?

希望这样的操作时,要问我的密码。请注意,当我运行时,ls -ltr /我得到:

drwxr-xr-x 94 root root  4096 Jul 30 13:28 etc
Run Code Online (Sandbox Code Playgroud)

但是我认为这并不重要,因为我已经将自己设置为“sudoer”,对吗?

更重要的是,我需要做什么才能以sudo mkdir /etc/blah当前用户 ( myuser) 的身份运行而不会被要求输入密码?

这是我的整个/etc/sudoers文件:

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root      ALL=(ALL:ALL) ALL
fizzbuzz  ALL=NOPASSWD: ALL
chadmin   ALL=NOPASSWD: ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
Run Code Online (Sandbox Code Playgroud)

Let*_*ety 78

导致这种情况的是规则的顺序/顺序。最后一条规则优先。

为了解决您的问题,只需移动您的线条,

fizzbuzz  ALL=NOPASSWD: ALL
chadmin   ALL=NOPASSWD: ALL
Run Code Online (Sandbox Code Playgroud)

sudoers文件到

sudo visudo -f /etc/sudoers.d/myOverrides 
Run Code Online (Sandbox Code Playgroud)

这是比sudoers使用纯文本编辑器编辑文件更好的方法。如果您不小心在文件中插入了错误,您可能无法再运行sudo. 始终使用visudo,以便检查语法并收到有关错误的警告!

您的指令不起作用,因为它被以下内容覆盖:

%admin ALL=(ALL) ALL
%sudo   ALL=(ALL:ALL) ALL
Run Code Online (Sandbox Code Playgroud)

如果您运行该groups命令,您应该会看到您的用户属于这些组。


Kos*_*tyn 23

如果myusersudo组中,那么此行顺序将不会提供无密码访问(如 Florian Diesch 所述),因为第 3 行覆盖了第 1 行。

myuser    ALL=(www-data:www-data) NOPASSWD: ALL
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
Run Code Online (Sandbox Code Playgroud)

所以只需将这些行按以下顺序排列:

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
myuser    ALL=(www-data:www-data) NOPASSWD: ALL
Run Code Online (Sandbox Code Playgroud)

myuser帐户下用于sudo -l检查myuser具有哪些权限。


Flo*_*sch 12

如果多个条目与用户匹配,则使用最后一个。因此,如果fizzbuzzchadmin是组的成员,admin或者sudo他们仍将被要求输入密码。

将两行放在sudoers文件末尾的#includedir行之后。