5

I've got the following entry in /etc/sudoers to allow me to run any command after asking for my password

%sudo   ALL=(ALL:ALL) ALL

I also want to run a specific command with no password as part of a script. I've tried putting the following line in /etc/sudoers, but it always asks for a password when I run it.

gmc ALL = NOPASSWD: /sbin/hdparm -C /dev/sdc

Is there a way to have sudo use the specific command before using the general entry?

Eric Renouf
  • 18,431
Gordon
  • 61

1 Answers1

5

Order of rules is important.

From 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).

So if you have

gmc ALL = NOPASSWD: /sbin/hdparm -C /dev/sdc
%sudo   ALL=(ALL:ALL) ALL

The the last rule will apply, and a password will be required.

However if you have

%sudo   ALL=(ALL:ALL) ALL
gmc ALL = NOPASSWD: /sbin/hdparm -C /dev/sdc

Then the NOPASSWD: rule applies.