Search code examples
homebrewsudo

How does homebrew prevent being used with sudo?


When I try to install using homebrew with sudo privileges, for example sudo brew install lua, I get this message

Error: Running Homebrew as root is extremely dangerous and no longer supported. As Homebrew does not drop privileges on installation you would be giving all build scripts full access to your system.

I understand this error and think that it is a good idea, but how does brew detect that I'm using sudo? Is this feature something that I can implement in my applications as well?


Solution

  • The brew.sh uses id -u to verify if it runs as root. If it runs as root, it will return 0. See this line. After that, it's just a question of showing a message in accordance to the id obtained.