Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Comments on As root, how do I read another user's environment variables?

Post

As root, how do I read another user's environment variables?

+4
−0

I have a script that should be run using sudo, with the goal of "promoting" a script from a user-level installation to a system installation. Reproduced here:

Existing code
#!/bin/bash

if [[ $EUID -gt 0 ]]; then
    echo "This script must run as root"
    exit 1
fi
if [[ -z "${SUDO_USER-}" ]]; then 
    echo "Sudo user not found"
    exit 1
fi
if [[ -z "${1-}" ]]; then
    echo "Usage: sudo share-command SCRIPTNAME"
    exit 1
fi
user_bin="$(getent passwd $SUDO_USER | cut -d: -f6)/.local/bin"
src="$user_bin/$1"
dst="/usr/local/bin/$1"
if [[ -f "$dst" ]]; then
    echo "Already exists"
    exit 1
fi
/usr/bin/install -m 755 "$src" "$dst"

Currently, it looks up the sudo user's home directory and looks in .local/bin relative to that.

I would like it to be able to find the script being installed by name.

I do not want to set the root user's PATH; I understand that this is insecure and there are protections against it. But given that the script is running as root, and I know who the sudo user is, it seems like I should be able to determine that user's PATH, and then iterate over it manually to look for the script.

Is this indeed possible? How?

History

2 comment threads

XY Problem? (5 comments)
Inversion of control (2 comments)
XY Problem?
Michael‭ wrote 7 months ago

Is your main objective to do the installation? Can you sudo --preserve-environment and install to wherever root would instead of fishing around for all of a user's PATH and aliases?

Karl Knechtel‭ wrote 7 months ago · edited 7 months ago

The problem is finding where to install from without troubling the user any more than necessary; the destination is assumed (sibling to the installation script, by default). I'm not interested in aliases; I'm looking specifically for a file with the given name, that happens to be an executable script. --preserve-env could work (since the path to /usr/bin/install is hard-coded), but then the install script doesn't know whether that was used. It would be better not to look on the root user's path if --preserve-env isn't used, since that would be trying to "install" things that are already "installed".

Michael‭ wrote 7 months ago

Now I'm confused. If you say you know where you're installing to, what's the problem? I have a hard time imagining a global installation that depends on a user's PATH. Like are people adding their ~/Downloads to PATH and installing from ~? Is this to figure out what program can execute a certain shebang? What if that program is installed user-only?

Karl Knechtel‭ wrote 7 months ago

The idea is, if I use share-command foo, I have already decided that I will install foo into /usr/local/bin, but I don't know where foo is located. I want to search the user's $PATH for it (because it's an executable so it ought to be on there somewhere), instead of requiring the user to specify a path. "What if that program is installed user-only?" — yes, that's the point. The goal is to take something (self-contained) that "is installed user-only", and make a system installation from it.

Michael‭ wrote 7 months ago

Oh! I wildly misunderstood the premise. Thanks for clarifying!