Skip to content

bug: brew not found / fails when script is run via sudo #121

Description

@gvatsal60

Describe the bug

When running the script using the recommended sudo elevation (e.g., curl ... | sudo bash), the script fails to detect or update Homebrew (brew).

This happens due to a dual conflict:

  1. PATH Reset: sudo strips out the user's custom environment variables, meaning /home/linuxbrew/.linuxbrew/bin is missing from the root $PATH. This causes check_command brew to report that Homebrew is not installed.
  2. Root Restrictions: Homebrew explicitly blocks or misbehaves when executed directly as the root user, which could corrupt file permissions in the user's home directory.

To Reproduce

Run the update script inside a modern Linux distribution (especially those using sudo-rs or strict secure_path defaults):

curl -fsSL "https://raw.githubusercontent.com/gvatsal60/Linux-All-In-One-Update-Script/HEAD/.update.sh" | sudo bash

Expected behavior

The script should be able to safely locate the Homebrew binary and drop privileges back down to the local user account (NON_ROOT_USER) to perform the brew update routines securely.

Proposed Solution / Code Fix

We can resolve this by updating check_command to explicitly look for the standard Linux Homebrew installation path, and updating update_brew to execute the commands via su - "${NON_ROOT_USER}".

1. Update check_command to handle brew paths under sudo:

check_command() {
    command_name="$1"

    # Explicit handling for brew when run under a stripped sudo PATH
    if [ "${command_name}" = "brew" ] && [ -x "/home/linuxbrew/.linuxbrew/bin/brew" ]; then
        return 0
    fi

    if ! command -v "${command_name}" >/dev/null 2>&1; then
        print_err "${command_name} is not installed."
        return 1
    fi

    return 0
}

2. Update update_brew to safely run as the non-root user:

update_brew() {
    println "Update Brew Formula's"

    if ! check_command brew; then
        return
    fi

    # Safely drop privileges back to the normal user to run brew commands
    if [ "\({NON_ROOT_USER}" != "nobody" ] && [ "\){NON_ROOT_USER}" != "root" ]; then
        su - "\${NON_ROOT_USER}" -c '
            eval "\$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
            brew update && brew upgrade && brew cleanup -s
            echo "\nBrew Diagnostics"
            brew doctor && brew missing
        '
    else
        # Fallback if no valid non-root user was resolved
        eval "\$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
        brew update && brew upgrade && brew cleanup -s
        println "Brew Diagnostics"
        brew doctor && brew missing
    fi
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions