2

I want to give myself permissions to use pmset without using sudo.

I added this line to etc/sudoers:

myname          ALL = /usr/bin/pmset

But when I save and close with :wq and run pmset, I get:

$ pmset -b disablesleep 1
'pmset' must be run as root...

What is it I'm doing wrong?

3

The sudoers file only gets read by sudo so you still need to run

sudo pmset ...

to execute the command. What your addition to sudoers does is that every user on your Mac can now execute pmset without entering a password (or even be in the admin group).

If you want to avoid having to type sudo pmset each time define an alias

alias pmset="sudo /usr/bin/pmset"
0

The order of parameters in sudoers is like this:

  • root ALL=(ALL:ALL) ALL - This applies to user root
  • root ALL=(ALL:ALL) ALL - This rule applies to all user root logged in from all hosts
  • root ALL=(ALL:ALL) ALL - User root can run commands as all users
  • root ALL=(ALL:ALL) ALL - User root can run commands as all groups
  • root ALL=(ALL:ALL) ALL - These rules apply to all commands

Your addition myname ALL = /usr/bin/pmset to sudoers contains the first, second and fifth parameters and therefore means user myname on any host can run sudo pmset. If they were a member of the %admin group (as the first user created on macOS is) they would have been able to do this anyway.

What is doesn't do is tell sudo is that you don't want to enter a password which is done with the NOPASSWD parameter. Therefore you want, as the last line in sudoers for myname:

myname          ALL = NOPASSWD : /usr/bin/pmset

This will let user myname enter sudo pmset without prompting for a password. If you wish you can then make an alias as described in the other answer and add it to your ~/.bashrc or ~/.zshrc

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .