5

I use Monterey macOS v12.3.

Do you know how can I find the /etc folder from the cd /Macintosh/ HD?

I corrupted my sudoers and I want to modify it from single-user mode, but I am not able to reach the path. I tried the following looking for /etc.

 cd /Macintosh/ HD/System/Volumes/Data
 cd /Macintosh/ HD/System/private

I read this topic, but there was no clue how to reach it: What's /System/Volumes/Data?

Below is the text taken from the currently closed question asked at Stack Overflow.

Title: I corrupted my sudoers file on MAC- how to modify from it from single user mode?

By mistake, I corrupted my sudoers file. I modified it and mis-spelled a word that corrupted the file.

I wrote NOPASSED instead of NOPASSWD.

In the meantime, I am not able to edit it any more.

I am using a MacBook Pro (16-inch, 2021) with macOS 12.3.

I rebooted with single user mode, and opened the Terminal, but I am not able to reach the directory to modify the file.

This log below is from the normal Terminal macOS, not from the single user mode.
oahmed@oahmed-mac /etc % ls -l | grep sudo
-r--r-----   1 root wheel      257 26 Feb 02:05 sudo_lecture
-r--r-----   1 root wheel     1573  2 Feb 12:18 sudoers
drwxr-xr-x   2 root wheel       64 26 Feb 02:05 sudoers.d
oahmed@oahmed-mac /etc % more sudoers
sudoers: Permission denied
oahmed@oahmed-mac /etc % sudo vi sudoers
/etc/sudoers:56:34: syntax error
%admin      ALL = (ALL) NOPASSED: ALL
                             ^
Password:
Sorry, user oahmed is not allowed to execute '/usr/bin/vi sudoers' as root on oahmed-mac.

 

7
  • 6
    I believe /Macintosh/ HD should be /Macintosh\ HD. Is this what you entered in a Terminal application window or just when entered your question? Mar 27, 2022 at 7:46
  • I'd just download BBEdit and edit the file with that -- it can edit as root without going through sudo. Mar 27, 2022 at 9:58
  • 1
    @GordonDavisson: Have you tried to edit sudoers with BBEdit. I was able with hosts, but not sudoers. I had to add user write access to sudoers, then I could. However, the OP can not do this because he can not use sudo. I was following the instructions given here. Mar 27, 2022 at 11:31
  • 2
    Adding NOPASSWD is a bad idea and you should not do it. Mar 27, 2022 at 17:36
  • 2
    Did you edit the file without visudo? visudo is the command you should always use to edit sudoers specifically because it checks for syntax errors before replacing the old files and avoids bricking your system...
    – GACy20
    Mar 28, 2022 at 14:30

4 Answers 4

7

You do not need to boot to single user mode to fix your error. What you need to do is switch your user ID to superuser (root). Under normal unix, the command would be su, however this command will not work as expected until you enable the root user. The procedure is given below.

Enable the root User

Note the lock icon should appear similar to the image shown below.

lock icon

The steps are given below.

  1. Choose Apple menu () > System Preferences, then click Users & Groups (or Accounts).
  2. Click the lock icon, then enter an administrator name and password.
  3. Click Login Options.
  4. Click Join (or Edit).
  5. Click Open Directory Utility.
  6. Click the lock icon in the Directory Utility window, then enter an administrator name and password.
  7. From the menu bar in Directory Utility, choose Edit > Enable Root User, then enter the password that you want to use for the root user.
  8. Quit System Preferences. For convenience, leave the Directory Utility window open.

Fix the sudoers File

Proceed with the following steps.

  1. Goto a Terminal application window and enter the command su. Enter the root user's password when asked.
  2. Once you are the root user, make the necessary correction to the sudoers file.
  3. If locked, click the lock icon in the Directory Utility window, then enter an administrator name and password.
  4. From the menu bar in Directory Utility, choose Edit > Disable Root User.
  5. Quit the Directory Utility.

References

3
  • That option worked for me, thank you very much Mar 29, 2022 at 2:09
  • Omar: If an answer worked for you, then you should accept the answer. This will let others know a solution was found. Also, a green checkmark will appear next to the answer. Mar 29, 2022 at 2:16
  • Sorry, I just did, I am now to stack overflow :) Mar 30, 2022 at 13:39
8

When booting to singe user mode, the following messages can be observed.

To access the /etc directory, read the above and enter the following commands.

/sbin/mount -P 1
/usr/libexec/init_data_protection
/sbin/mount -P 2

Entering ls /etc/sudoers will result in the following.

-sh-3.2# ls /etc/sudoers
/etc/sudoers

As you can see, the name of the volume containing the root directory is irrelevant. In other words, you do not need to enter Macintosh\ HD.

5
  • I tried this solution, but it didn't show me the file, I wanted to share a screenshot, but there is no option to attach .. I uploaded to my Google drive photos.app.goo.gl/4tmLU3nxujfm9vAj6 Mar 30, 2022 at 13:37
  • Omar: I believe your image shows you are not in single-user mode. I think you are in macOS Recovery or booted from an USB macOS installer. Your question explicitly states the following: "I want to modify it from single-user mode". Additionally, your image shows you entering the command /sbin/mount -uw /. My answer does not instruct you to enter this command. It is important that you follow the instructions that are actually stated in the answer. Mar 30, 2022 at 18:00
  • Hi David: I am new to Mac, and I thought that mode is the single user mode, but according to your reply, seems it is not that one. Thanks fro clarifying that to me :). so it seems that was what is called recovery mode. I will google it now to get the single with M1 machine. Mar 31, 2022 at 12:53
  • 1
    and for the extra command /sbin/mount -uw / , it was there in the screenshot you share, i thought you just missed in the text, actually i tried the steps two time, one time with the command, and one time without. but anyway, i am the wrong mode in both cases. Mar 31, 2022 at 12:56
  • Omar: I did add another answer which explains how to fix the problem from macOS Recovery. This new answer is probably what you were looking for in the first place. Mar 31, 2022 at 15:02
3

Using macOS Recovery or a Bootable Installer for macOS

This answer was tested using a bootable installer for macOS 12.0.1 (Build 21A559). You could also boot from macOS Recovery instead.

Here I assume a Terminal window was opened by selecting Utiities > Terminal from the Recovery menubar.

First, I should note that the OP incorrectly entered Macintosh/ HD. Instead, the OP should have used Macintosh\ HD or "Macintosh HD".

Execute the steps given below.

  1. Enter the command below to invoke a Z shell. The chroot command changes the root directory to be the same as when booted to macOS Monterey. If you prefer a Bash shell, then substitute bash for zsh.

    Note: If you wish to access any installed third party software, then you may elect to add /usr/local/bin to the PATH variable.

    chroot "/Volumes/Macintosh HD" zsh
    
  2. Make the necessary correction to the sudoers file. For example, the command below uses the nano editor to edit the sudoers file.

    nano /etc/sudoers
    

    Note: User GACy20 posted a comment recommending the use of the visudo command.

  3. When finished, enter the command below to return to the original Bash shell.

    exit
    

Below is sample output.

-bash-3.2# chroot "/Volumes/Macintosh HD" zsh
root@Mac / # nano /etc/sudoers
root@Mac / # exit

Saving session...shell_session_save:7: read-only file system: /dev/null

...copying shared history...
...saving history...truncating history files...
...completed.
-bash-3.2# 

References

0

This series of answers - all from David Anderson (until now) - are treasures for those of us who feel stymied and/or frustrated by Apple's practice of software, systems development & administration.

I add this answer only to augment the information provided in Mr. Anderson's first answer, and perhaps help other users running older versions of macOS. Specifically:

  • I still use macOS Catalina (10.15.6) on my machine (because it has been my experience that Apple's upgrades create a larger risk surface for dysfunction).

  • In following the procedure for enabling the root user - which I completed successfully thanks to the detailed recipe - I found that upon completion, the su approach remained unavailable to me on my system:

    % su
    Password:
    su: Sorry
    % <repeated several times>
    

    Which seems odd as su is definitely installed on Catalina, and is functional for other users (including Admins). Perhaps this was done in /etc/pam.d/su?

    Instead, I had to resort to this to get the root prompt, which of course is useless for recovery on a Catalina system (maybe others?) if you had made the OP's spelling error:

    % sudo su     # or 'sudo su -' if you prefer
    Password:
    sh-3.2# whoami
    root
    sh-3.2#
    

    ref addenda notes 1. & 2. in the sequel

  • The OP asked another question:

    Do you know how can I find the /etc folder from the cd /Macintosh/ HD?
    * Subsequently corrected to cd /Macintosh\ HD

    On my old Catalina system, Macintosh HD appears Under Finder's "Locations", but:

    % cd /Macintosh\ HD
    cd: no such file or directory: /Macintosh HD  
    % cd Macintosh\ HD 
    cd: no such file or directory: Macintosh HD 
    % cd 'Macintosh HD'
    cd: no such file or directory: Macintosh HD
    

    Yet, I can see the contents of Macintosh HD when I click it in the Finder window. I'm sure it's just me: this strikes me as a bit of a disconnect... but not a reason to upgrade :)

    But back to the question at hand, here's what I see when trying to list the contents of the /etc folder on my Catalina system:

    % ls -l /etc
    lrwxr-xr-x@ 1 root  admin  11 Dec  5  2019 /etc -> private/etc 
    
    # should this have been '/etc -> /private/etc' ?? 
    

    However, we can get there like this:

    ~ % cd /etc 
    /etc % ls -l
    total 1072
    -rw-r--r--   1 root  wheel     515 Nov  9  2019 afpovertcp.cfg
    lrwxr-xr-x   1 root  wheel      15 Dec  5  2019 aliases -> postfix/aliases
    -rw-r-----   1 root  wheel   16384 Nov  9  2019 aliases.db
    
    ...
    
    /etc %
    
  • In closing, wrt editing /private/etc/sudoers (or whatever it's now called), NOTE there is also a folder named :/private/etc/sudoers.d

    sudoers.d is empty on my Catalina system, but it is potentially a great place for adding things like assigning the NOPASSWD option (or NOPASSED if you prefer :) to a user. Note that if you want to create additional rules in sudoers.d, you'll need to include this line in your sudoers file:

    #includedir /etc/sudoers.d

    One should use visudo -f for editing files under sudoers.d - it does the same syntax & "spelling" checks on the entries, but if an entry is added using a conventional editor, it is said that errors will not lock one out of their system. It has other advantages, but I don't know how useful they are for macOS users.

    Always use sudo visudo to edit the sudoers file - regardless of where it's been located on your system. This will avoid the need for an excursion into Single User Mode - especially important if the su command won't get you to the root prompt on your system (e.g. Catalina).


The "Addendum/FYI/Not part of the answer section":

Note 1:

I still find it odd that this works even before (and after) the root user was enabled... which beggars the question, "What were they thinking?"

Note 2:

As an aside, for those who are curious about sh:

% sh --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Copyright (C) 2007 Free Software Foundation, Inc.

:) lol

You must log in to answer this question.

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