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 How does the root user locate executables?

Parent

How does the root user locate executables?

+1
−0

A little while ago I was helping someone with running Python in a virtual environment, as root, for some specific purpose. I determined easily enough that this requires explicitly specifying the path to the virtual environment's Python executable (which to some extent defeats the purpose of the "virtual environment"); what I'm trying to understand is why.

When I activate a Python virtual environment (which modifies the PATH) and then try e.g. sudo which python I get a different result from which python (i.e. the Python that came with the OS install, rather than the one in the virtual environment); but sudo echo $PATH gives the same result as echo $PATH (i.e. the path to the virtual environment's bin subdirectory is present, and precedes the system Python path). If I actually run Python, I can similarly verify from within Python that different installations are found this way.

I had understood that locating an executable involves looking in each directory in the PATH environment variable, in order. I wouldn't be surprised if, say, sudo had to open a new shell, re-initialize everything and thus fail to see the change to PATH; but it clearly does see that change.

On my system, which is explicitly documented to "... [do] this by searching the PATH for executable files matching the names of the arguments". That doesn't match the behaviour I see as root.

How is the root user locating the executable, and why is it different?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+6
−0

but it clearly does see that change.

Nope. Your environment is being reloaded in the sudo process.

sudo echo $PATH gives the same result as echo $PATH because $PATH is expanded by your shell first thing, before sudo gets spawned. If you want to see the value of PATH from inside sudo, run sudo bash -c 'echo $PATH' instead.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Ah, of course (1 comment)
Ah, of course
Karl Knechtel‭ wrote about 1 year ago

This makes it feel like my original question is "a typo" by the reasoning I would normally apply - but I sense that there's the working of a more general principle here that could be written up in a better designed Q&A.

I also notice, for example, that sudo whoami && whoami gives my own username the second time, whereas sudo whoami && sudo whoami would start separate shells. But sudo bash -c 'whoami && whoami' works around both problems, and the same technique would allow for e.g. having the root user activate the virtual environment and then run Python.

And indeed, sudo bash -c 'source path/to/activate && echo $PATH' (the first command is activating the virtual environment) gives me a modified path that is specific to that spawned bash process.