Skip to content

Instantly share code, notes, and snippets.

@kbaynes
Last active November 9, 2022 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbaynes/912b27ce355ad3bed2d9f69daccb155f to your computer and use it in GitHub Desktop.
Save kbaynes/912b27ce355ad3bed2d9f69daccb155f to your computer and use it in GitHub Desktop.
Add sudoer on Mac

Add an Account to sudoers on Mac

To add an account to the list of sudoers, you'll need an administrator account.

Test your sudo access by running a simple command, such as:

If you simply want to make sure that your current user has sudo access AND this command works, then you already have sudo access and you're done.

sudo echo hello world

List user accounts

Make sure that there is an 'admin' group.

dscl . list /Users | grep -v “^_”

List admin group users

dscacheutil -q group -a name admin

Show the current active user

id -un

At this point, if you just want for your user to have sudo access AND your user exists in the admin group, then you are done because all admin users have sudo access. Otherwise, continue to add the target user to the sudoers file. You will need a user with sudo access to do this.

Switch to an administrator account:

(If your current user is not in the admin group, OR you want to add give sudo access to another user)

su <administrator_account>

Run sudo cat /etc/sudoers and verify that the following line exists somewhere in that file:

(This line usually exists already, this is just a verification step)

sudo cat /etc/sudoers
#includedir /private/etc/sudoers.d

Use visudo to create a new sudoers file

sudo visudo -f /etc/sudoers.d/sudoers

You're now in the vi editor. Here are instructions for those who don't know vi.

Press the i key for “insert” mode, then type:

<your_account> ALL=(ALL) ALL

Press esc, then type :wq followed by the return key to save your changes

Verify your changes:

sudo cat /etc/sudoers.d/sudoers

Type exit to logout of the administrator account and return to your account

Test your sudo access by running a simple command, such as:

sudo echo hello world

Notes:

On Mac, /etc is a symlink to /private/etc

Type info visudo in Terminal for help.

It's best practice to create a new sudoers file instead of editing the existing one at /etc/sudoers. See this stackoverflow thread.

@zwbetz-gh
Copy link

@kbaynes
Copy link
Author

kbaynes commented Jul 19, 2022

Thanks @zwbetz-gh ! It was so good I didn't want to lose the link, so I copied. Apologies for omitting attribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment