"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:

  1. Start up in recovery mode (Cmd+R during boot up)
  2. Disk Utilities > Select Macintosh HD, and Mount
  3. Close Disk Utilities
  4. Utilities > Terminal
  5. Type: chown root:wheel "/Volumes/Macintosh HD/etc/sudoers"
  6. 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/