DEV Community

Marcos Rezende
Marcos Rezende

Posted on

User is not in the sudoers file after Monterey upgrade

After MacOS Monterey upgrade I got this error while trying to run any command with sudo:

sudo apachectl -k restart
Password:
rezende79 is not in the sudoers file. This incident will be reported.
Enter fullscreen mode Exit fullscreen mode

While it wasn't possible to run sudo visudo to edit the sudoers file properly, I created a /tmp/sudoers file with the following content:

# sudoers file.

#
# This file MUST be edited with the 'visudo' command as root.
# Failure to use 'visudo' may result in syntax or file permission errors
# that prevent sudo from running.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

# Host alias specification

# User alias specification

# Cmnd alias specification

# Defaults specification
Defaults    env_reset
Defaults    env_keep += "BLOCKSIZE"
Defaults    env_keep += "COLORFGBG COLORTERM"
Defaults    env_keep += "__CF_USER_TEXT_ENCODING"
Defaults    env_keep += "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE"
Defaults    env_keep += "LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME"
Defaults    env_keep += "LINES COLUMNS"
Defaults    env_keep += "LSCOLORS"
Defaults    env_keep += "SSH_AUTH_SOCK"
Defaults    env_keep += "TZ"
Defaults    env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY"
Defaults    env_keep += "EDITOR VISUAL"

# Runas alias specification

# User privilege specification
root    ALL=(ALL) ALL
rezende79    ALL=(ALL) ALL
%admin  ALL=(ALL) ALL

# Uncomment to allow people in group wheel to run all commands
%wheel    ALL=(ALL) ALL
%admin    ALL=(ALL) ALL

# Same thing without a password
# %wheel    ALL=(ALL) NOPASSWD: ALL

# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now
ALL ALL=(ALL)     NOPASSWD:/opt/dplat/bin/Revision/CMUpdatePackage/Installer.app/Contents/MacOS/Installer
ALL ALL=(ALL)     NOPASSWD:/opt/dplat/bin/UpdatePackageInstaller.app/Contents/MacOS/UpdatePackageInstaller
Enter fullscreen mode Exit fullscreen mode

Please note that the user rezende79 had already belonged to admin group and the user was inserted inside the file above.

After this, you only need to run the following command at your terminal to copy the content from /tmp to /etc folder:

osascript -e 'do shell script "cat /tmp/sudoers > /etc/sudoers; chown root:wheel /etc/sudoers" with administrator privileges'
Enter fullscreen mode Exit fullscreen mode

This post is based on the following solution:
I accidentally deleted the sudoers file on Mac OS X

Discussion (0)