Installing homebrew after upgrading to MacOS High Sierra

high sierrahomebrewinstallterminal

Recently I upgraded to MacOS High Sierra (10.13.4). After upgrade my homebrew seems to have stopped working. So I uninstalled the homebrew by executing the below command

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

I tried to install homebrew again by executing the below command

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

But the installation failed with the error as discussed here.

Then I tried executing the command as suggested in the above issue.

sudo chown -R $USER:admin /usr/local

But it did not work because of the issue discussed here.

I executed the below command as suggested above

sudo chown -R $(whoami) $(brew --prefix)/*

It gave me some Operation not permitted warnings.

Then I again tried to install homebrew. Now it is showing me the below logs in console

...
==> Installation successful!

==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics.html

==> Next steps:
- Run `brew help` to get started
- Further documentation:
    https://docs.brew.sh
sudo: /etc/sudoers is owned by uid 504, should be 0
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin 

Can some one let me know what does the last set of warnings / errors denote? Can these be ignored?

Best Answer

The warning at the end is telling you that the root user (denoted by default with the uid 0) doesn't own the /etc/sudoers file anymore. This is bad. At some point you may have accidentally changed this, perhaps by mistyping a command. The sudoers file denotes which users and groups are allowed to escalate to a root privilege set by prefacing a command with sudo and typing their login password.

The chown command you've been using above changes the ownership of files from one user and/or group to another. Because you've changed ownership of the sudoers file you probably won't be able to sudo yourself out of trouble as the sudo command won't run for security reasons if the sudoers file from which it draws its configuration is owned by a user other than root (this is why homebrew is spitting out the last error message above).

I'd suggest the following:

  1. Enable the root user using Apple's instructions here (under the section 'Enable or disable the root user').
  2. Open Terminal and switch to the root user by typing su - and entering the password you assigned to your root user account in step 1.
  3. As the root user, change ownership of the sudoers file back to root and the wheel group with the following:

    chown root:wheel /etc/passwd
    
  4. Follow the instructions in step 1 again but this time disable the root user again.

You should now be able to use the sudo command again with your regular admin user. You can run sudo ls for instance in a new shell; if the contents of the current directory are listed and you don't see that ownership error above then all is well.

Although this falls outside the bounds of your question, I'm concerned that if you've unwittingly changed ownership of the sudoers file while attempting to change ownership of /usr/local/ you may have done this to other important files as well. Run ls -l /etc/ and examine the output of that command. The third column in from the left shows the user who owns a given file. By default, pretty much every file in the /etc/ directory is meant to be owned by root (an obvious one is /etc/passwd). If you see the majority are owned by another user (your own username for instance), the problem is far more extensive than your sudoers file.

As a general point, you need to be very careful running any command prefaced by sudo. Only run commands in your terminal if you understand the command and know exactly what it will do when passed a given set of options/arguments. There are some safeguards provided by the more recent Mac operating systems with System Integrity Protection (SIP) but you can still wreak plenty of havoc on your system.

If you want to learn what a particular command and its various arguments do, try checking the manual first by typing man + the command you want information on (man sudo or man chown for example).