"sudoers is owned by uid 501, should be uid 0" message, but sudo works fine [duplicate]
You can boot into single user mode by pressing Cmd-S on startup (see OS X: How to start up in single-user or verbose mode for details) which should give you a root shell. Then run
mount -uw /
chown root:wheel /etc/sudoers
chmod 440 /etc/sudoers
reboot
to fix the problem and restart.
You can fix this via AppleScript, without needing to reboot, or even log out & back in, or an existing root shell, or... All you need is a valid administrator password.
Run this command:
osascript -e 'do shell script "chown root:wheel /etc/sudoers; chmod 440 /etc/sudoers; chmod -N /etc/sudoers" with administrator privileges'
...and then authenticate as an administrator when requested.
Explanation: this is a bit indirect. It uses the shell command osascript
to run a short AppleScript that uses the do shell script
command to run a short shell script. The reason for the shell->AppleScript->shell thing is to use the with administrator privileges
option, which has much the same effect as sudo
but will work even if /etc/sudoers is completely messed up.
For macOS Mojave, here is what worked for me:
- Start up in recovery mode (Cmd+R during boot up)
- Disk Utilities > Select Macintosh HD, and Mount
- Close Disk Utilities
- Utilities > Terminal
- Type:
chown root:wheel "/Volumes/Macintosh HD/etc/sudoers"
- Reboot
You may also need to change the ownership of ts, back in the Terminal:
sudo su
sudo chown -R root:wheel /var/db/sudo/ts/