Enable sudo without a password on MacOS

My Problem

I would like to be able to run sudo commands on MacOS Sierra 10.12 without having to type a password.

What have I tried

I've read the following:

  • Why does sudo ask for a password in terminal?
  • allow sudo to another user without password
  • Use sudo Without a Password on Your Mac

And changed the relevant part of my /etc/sudoers file to:

root ALL=(ALL) ALL
%admin  ALL=(ALL) NOPASSWD: ALL
%wheel ALL=(ALL) NOPASSWD: ALL
%wheel ALL=(ALL) NOPASSWD: ALL
%sudo   ALL=(ALL) NOPASSWD: ALL
adamatan ALL=(ALL) NOPASSWD: ALL

My local user id (whoami) is adamatan.

I'm still being asked to type a password every few minutes when calling sudo. Any idea what's wrong?


Solution 1:

Open a terminal, run sudo visudo

Edit the line:

%admin ALL=(ALL) ALL

To say:

%admin ALL=(ALL) NOPASSWD: ALL

Now, you should now be able to run sudo without password.

Solution 2:

Better not to edit /etc/sudoers directly. Instead, the /etc/sudoers file does include a line:

## Read drop-in files from /private/etc/sudoers.d
## (the '#' here does not indicate a comment)
#includedir /private/etc/sudoers.d

So better simply add an empty file under /private/etc/sudoers.d/mysudo and use visudo to fill it with a content like:

mylogin            ALL = (ALL) NOPASSWD: ALL

Remember to always run sudo visudo after creating the empty file under /private/etc/sudoers.d/ so that visudo also checks for syntax on that file, or else you might end up with a broken sudo configuration and the inability to run visudo to fix it.

You have been warned