AWS Mac EC2 bash command running as root assuming another user not finding binary that it should know about

32 views Asked by At

Sorry for the mouthful title, I wasn't sure how to describe this situation in more accurate, simpler terms, suggestions welcome.

I am running a script on an AWS MacOS machine, as root user. The script at some point runs a command as another user (in this case, ec2-user), but when it does, it errors out saying the binary (in this case, brew) I am calling is not found, even though if I log in as ec2-user you can find the binary.

The script, simplified, is as follows:

#!/bin/bash
su - "ec2-user" -c 'brew --version'

Which errors out as:

root$ ./script.sh
zsh:1: command not found: brew

root itself does not have brew in it's PATH, however, if I switch user to ec2-user, brew is found:

root$ brew --version
-sh: brew: command not found
root$ sudo su - ec2-user
ec2-user$ brew --version
Homebrew 4.1.3

When I replicate this exact same issue on my personal computer (substituting only the user), even though root doesn't know about brew, it works fine:

personal-computer-root$ brew --version
-sh: brew: command not found
personal-computer-root$ ./script.sh
Homebrew 4.1.11

Even if I do add brew to the PATH of the root user, nothing changes

root$ export PATH=$PATH:/opt/homebrew/bin:/opt/homebrew/sbin
root$ ./script.sh
zsh:1: command not found: brew

Both Macs seem to have the same build and product version

$sw_vers
ProductName:        macOS
ProductVersion:     13.5.2
BuildVersion:       22G91

I am completely lost as to why this is happening, and why the error is showing as zsh even though the shell running should be bash according to the shebang. If anyone has any ideas as to what could be happening, I would love to hear it.

0

There are 0 answers