我有以下条目/etc/sudoers允许我在询问密码后运行任何命令
%sudo ALL=(ALL:ALL) ALL
Run Code Online (Sandbox Code Playgroud)
我还想在脚本中运行一个没有密码的特定命令。我试过将以下行放在 中/etc/sudoers,但它在我运行时总是要求输入密码。
gmc ALL = NOPASSWD: /sbin/hdparm -C /dev/sdc
Run Code Online (Sandbox Code Playgroud)
有没有办法sudo在使用通用条目之前使用特定命令?
规则的顺序很重要。
从man sudoers
When multiple entries match for a user, they are applied in order. Where
there are multiple matches, the last match is used (which is not neces-
sarily the most specific match).
Run Code Online (Sandbox Code Playgroud)
所以如果你有
gmc ALL = NOPASSWD: /sbin/hdparm -C /dev/sdc
%sudo ALL=(ALL:ALL) ALL
Run Code Online (Sandbox Code Playgroud)
最后一条规则将适用,并且需要密码。
但是如果你有
%sudo ALL=(ALL:ALL) ALL
gmc ALL = NOPASSWD: /sbin/hdparm -C /dev/sdc
Run Code Online (Sandbox Code Playgroud)
然后应用 NOPASSWD: 规则。