0

I always want to run this /anaconda/anaconda3/bin/python executable whenever any of the system user runs below command:

python
python3
sudo python
sudo python3

I've modified /etc/environment file to below value

PATH="/anaconda/anaconda3/bin:/sbin:/bin:/usr/sbin:/usr/bin"

All which pyhton patterns returns the expected path

$ which python
/anaconda/anaconda3/bin/python
$ which python3
/anaconda/anaconda3/bin/python3
$ sudo which python
/anaconda/anaconda3/bin/python
$ sudo which python3
/anaconda/anaconda3/bin/python3

But when I run them with sudo, it is not executing /anaconda/anaconda3/bin/python

$ sudo python --version
Python 2.7.5
# This is I believe /bin/python
$ sudo python3 --version
Python 3.6.7
# This is I believe /bin/python3. This is certainly not `/anaconda/anaconda3/bin/python`

I want to execute /anaconda/anaconda3/bin/python when any of the user runs sudo python or sudo python3.

Does this mean /etc/environment isn't being able to change secure_path for sudo?

Please suggest a solution.

My OS is:

$ cat /etc/os-release
NAME="Red Hat Enterprise Linux Server"
VERSION="7.5 (Maipo)"
...
...
shashwat
  • 7,851
  • 9
  • 57
  • 90
  • 1
    Is the `secure_path` option set in `/etc/sudoers`? – Kyle Willmon May 06 '19 at 14:42
  • @KyleWillmon Yes, there is. Do I have to modify it? Actually, I didn't find a straight forward way to update it so I can put it in Ansible task. I need to automate this configuration. – shashwat May 06 '19 at 14:43
  • 1
    Yes, you'll have to modify that if you want to change the path that `sudo` uses. You can read on this topic at the [many](https://unix.stackexchange.com/questions/91541/why-is-path-reset-in-a-sudo-command), [many](https://unix.stackexchange.com/questions/83191/how-to-make-sudo-preserve-path), [other questions](https://stackoverflow.com/questions/257616/why-does-sudo-change-the-path/375780) asked about this – Kyle Willmon May 06 '19 at 14:49
  • @KyleWillmon I've gone through some of them. But why `sudo which python` reveals `/anaconda/anaconda3/bin/python` which means it respects `/etc/environment` overrides. – shashwat May 06 '19 at 15:28
  • It's possible that `which` is an alias on your system (try `type which` to find out). You might get a more reliable result if you use `sudo /usr/bin/which python`. You could also do something like `sudo /bin/sh -c 'command -v python'`. – Daniel Pryden May 06 '19 at 16:09

0 Answers0