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

Post History

60%
+1 −0
Q&A How does the root user locate executables?

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 pa...

1 answer  ·  posted 1y ago by Karl Knechtel‭  ·  last activity 1y ago by r~~‭

Question path which root-user
#1: Initial revision by user avatar Karl Knechtel‭ · 2023-09-24T08:06:03Z (about 1 year ago)
How does the root user locate executables?
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?