Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

username is not in the sudoers file. This incident has been reported to the administrator.

Hello everyone, I have a MacBook Air M1 (2020), with MacOS Sonoma 14.0. I am the only user of this computer, and I am the admin, I have admin privileges.


I was trying to edit sudoers file. But I made the mistake of trying to edit with vim, instead of using "sudo visudo." After that, and after seeing the sudoers file, I close it. But now, I can't use sudo commands.


For example, when I run:

sudo echo "hello world"


I got the next prompt:

username is not in the sudoers file.
This incident has been reported to the administrator.


I'm trying anything (recovery mode to edit sudoers, etc.), but I can't find a solution. I don't mind to restore sudo, sudoers, su or any file necessary.


Thank you.

MacBook Air 13″, macOS 14.0

Posted on Oct 8, 2023 7:31 AM

Reply
Question marked as Best reply

Posted on Oct 8, 2023 8:31 AM

Navigate to the /etc folder containing the sudoers in Finder and enter Time Machine and restore your sudoers file from your Time Machine backup. Use Command-Shift-. to toggle viewing of hidden files. This is the quickest approach.


If you’re in the “YOLO club for data preservation”, make a Time Machine backup now, wipe the Mac, use Recovery to install Sonoma, and migrate your login in from your just-created backup.


Since you probably won’t want either of those approaches (“for reasons”), create a sudoers file somewhere else using vim (probably using set paste), pico, or whatever and paste the macOS 13 Monterey sudoers contents below, and then use Finder to copy that file into /etc and authenticate when requested. Restart.


#
# Sample /etc/sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.

##
# Override built-in defaults
##
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"
Defaults	env_keep += "HOME MAIL"

Defaults	lecture_file = "/etc/sudo_lecture"

##
# User alias specification
##
# User_Alias	FULLTIMERS = millert, mikef, dowdy

##
# Runas alias specification
##
# Runas_Alias	OP = root, operator

##
# Host alias specification
##
# Host_Alias	CUNETS = 128.138.0.0/255.255.0.0
# Host_Alias	CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0
# Host_Alias	SERVERS = master, mail, www, ns
# Host_Alias	CDROM = orion, perseus, hercules

##
# Cmnd alias specification
##
# Cmnd_Alias	PAGERS = /usr/bin/more, /usr/bin/pg, /usr/bin/less

##
# User specification
##

# root and users in group wheel can run anything on any machine as any user
root		ALL = (ALL) ALL
%admin		ALL = (ALL) ALL

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

Similar questions

5 replies
Question marked as Best reply

Oct 8, 2023 8:31 AM in response to kikelg

Navigate to the /etc folder containing the sudoers in Finder and enter Time Machine and restore your sudoers file from your Time Machine backup. Use Command-Shift-. to toggle viewing of hidden files. This is the quickest approach.


If you’re in the “YOLO club for data preservation”, make a Time Machine backup now, wipe the Mac, use Recovery to install Sonoma, and migrate your login in from your just-created backup.


Since you probably won’t want either of those approaches (“for reasons”), create a sudoers file somewhere else using vim (probably using set paste), pico, or whatever and paste the macOS 13 Monterey sudoers contents below, and then use Finder to copy that file into /etc and authenticate when requested. Restart.


#
# Sample /etc/sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.

##
# Override built-in defaults
##
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"
Defaults	env_keep += "HOME MAIL"

Defaults	lecture_file = "/etc/sudo_lecture"

##
# User alias specification
##
# User_Alias	FULLTIMERS = millert, mikef, dowdy

##
# Runas alias specification
##
# Runas_Alias	OP = root, operator

##
# Host alias specification
##
# Host_Alias	CUNETS = 128.138.0.0/255.255.0.0
# Host_Alias	CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0
# Host_Alias	SERVERS = master, mail, www, ns
# Host_Alias	CDROM = orion, perseus, hercules

##
# Cmnd alias specification
##
# Cmnd_Alias	PAGERS = /usr/bin/more, /usr/bin/pg, /usr/bin/less

##
# User specification
##

# root and users in group wheel can run anything on any machine as any user
root		ALL = (ALL) ALL
%admin		ALL = (ALL) ALL

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

Oct 8, 2023 10:57 AM in response to MrHoffman

First of all, thank you so much for your fast reply. Unfortunately, I didn't have a Time Machine Backup, so I need to use the third approach. I made the new sudoer file, and put into /etc, and made the restart. But now, I have these message:


sudo: /etc/sudoers is owned by uid 501, should be 0
sudo: no valid sudoers sources found, quitting
sudo: error initializing audit plugin sudoers_audit



When I made ls -lh in /etc, I have these files and folders (among others):


-r--r-----   1 root  wheel   257B 16 sep 15:28 sudo_lecture
-rw-r--r--@  1 kike  staff   1,5K  8 oct 19:40 sudoers
drwxr-xr-x   2 root  wheel    64B 16 sep 15:28 sudoers.d


Sounds obviously, I need to change the owner and the group, and maybe the permissions (with recovery mode). So, what do you think? I am asking you because I don't want to change more files and broke and corrupt more my computer.


Thank you :)

Oct 8, 2023 6:47 PM in response to kikelg

FYI, it is never a good idea to edit the actual "sudoers" file. The better way to modify its behavior is taking advantage of the ability to create individual configuration file changes to the "/etc/sudoers.d" folder instead. You utilize the same format and you get to modify "sudoers" behavior without risking the default source file. In fact, this method allows you to have unique changes within individual files making it easier to see & manage the changes (you really shouldn't have very many modifications since those changes may not work exactly like you expect....meaning they could have unintended side effects).


Also, if you are not comfortable with using "vim", then you can instead use the "nano" editor, or any other command line editor you wish. Here is how you can add a custom "sudoers" configuration file called "mychanges" using the "nano" editor:

EDITOR=nano  sudo  visudo  -f  /etc/sudoers.d/mychanges


This will warn you on saving the file whether any syntax errors are detected. If you want to use the default system editor instead of "nano", then just remove the "EDITOR=nano" part.


username is not in the sudoers file. This incident has been reported to the administrator.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.