11

Years ago, you could get away with running brew as root just by chowning brew to root, but they removed that feature. So in Homebrew 1.1.6, I used to edit /usr/local/Homebrew/Library/Homebrew/brew.sh to remove the root check (the one that said it's "extremely dangerous") so I could run brew as root.

The newer version of Homebrew still works with that but keeps reverting the brew.sh file back every time after I run brew, even if I chmod 500 it. I could write a script to change it then run brew automatically, but I'm not sure if this will always work and don't want to take chances. Has anyone found a different method to run Homebrew as root?

  • 3
    Out of curiosity, why do you want to run brew as root? Also, is sudo not sufficient for your purposes? – Allan May 3 '17 at 1:14
  • 2
    @Allan sudo does run it as root, and it gives me the same "extremely dangerous" error. I want to run as root because it otherwise has problems installing certain packages, especially if anything was installed not through Homebrew (e.g. through pip). I've gone down that route many times. Plus it doesn't work properly for multiple users without weird workarounds unless I install everything as root. – sudo May 3 '17 at 1:54
  • $ su -> $ brew whateveryouwant ? – StrawHara May 3 '17 at 9:34
  • 1
    Building a software package as root is risky. Even building a package without a package manager, you would build the package as a non-privilege user (yourself) then install the package as root with sudo. Package managers such as Fink and MacPorts both build with their own non-privilege users then install as root. Homebrew does not have such a mechanism. – fd0 May 3 '17 at 14:50
  • 1
    Hmm, I don't see the issue on my end wherein the brew.sh file is getting reverted after modifying it and then running a brew command. So I can run sudo brew whatever and brew whatever in tandem just fine. My brew --version output is thus: Homebrew 1.2.1-98-g803f9cbf7b-dirty Homebrew/homebrew-core (git revision d3d6; last commit 2017-05-28) FYI, I modified my brew.sh file to comment out all lines in the check-run-command-as-root function and placed a single return statement at the top of the function instead. – GDP2 Jun 1 '17 at 3:12
2

After making your change to the script, try setting the immutable flag on brew.sh.

chflags uchg /usr/local/Homebrew/Library/Homebrew/brew.sh

I have not tested. As a general rule, I do not give root to applications whose developers are begging me not to.

  • I also agree with Jason. There is a reason they say not to run brew as root. Mostly because they are the devs and i'lll trust them with their software. Also, unlike macports, which last time i checked still requires that, brew doesn't need or want root permissions so if you screw something up it most likely wont bork your whole system. Everything is contained in your user account and no need to be messing around with sudo. – user9950573 Jun 16 '18 at 18:46
  • 1
    Confirmed working, thanks. It takes some time to attempt to update the first time, but later sudo brew installs run as normal. – sudo Jun 17 '18 at 5:28
  • 1
    There isn't much to mess up and render a system unusable with the packages I'd be installing on a Mac. Anyway, I can restore my system within 30 minutes. Cleaning up my user is a lot harder, and many things straight up don't work unless installed system-wide, as I explain in the comments above. – sudo Jun 17 '18 at 5:29
1

Typical users shouldn't do this, but you're not typical are you?

Two issues:

  1. You can't stop root. If you insist on using root, then root can overwrite brew.sh no matter what.

  2. You can't stop brew, after all, you're talking about running brew.

So we'll work with that. Let's setup an alias to copy over brew.sh then run brew.

alias brew='cp /<modified>/brew.sh /<actuall>/brew.sh; /usr/local/bin/brew'

(Fix the paths to match your system).

Now all you need to do is:

sudo brew <arguments>

(It'll become root, copy over brew.sh, then run brew as root with arguments, afterwards brew will overwrite brew.sh and you'll be ready to do it all over again)

Note: You could put sudo into the alias too, but this way feels better because you'll be prompted for a password every time.

Typically you can't use setuid (chmod u+s) to root on shell scripts, for security. Perl has a separate program suidperl that could be installed and could escalate user privilege, but such solutions are a bit deep for a quick answer.

You must log in to answer this question.

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