I want add some directory to the $PATH when running sudo, this is a (semi) permanent requirement, not something that needs to be added to the scripts themselves. I notice that Django has managed to do it, (my $PATH when running sudo is "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/django/django-trunk/django/bin") - so how did it do that?
3 Answers
This is the line in the sudoers file that resets:
Defaults env_reset
You can work around this by adding PATH
to env_keeps
or by adding this line:
Defaults env_keep = "PATH"
EDIT: meder, you do not disable env_reset, you simply bypass the path reset
Or you can remove the offending env_reset
line.
Even better though, you can declare a secure_path
that will replace PATH
when sudo is run:
Defaults secure_path="/bin:/usr/bin"
That way you can control what specific directories to include in the path.

- 846
- 10
- 14
-
1This should be the accepted answer. You can limit access to only the paths needed with the sudoer mechanism instead of opening all of root's access. – Bernard Jan 01 '14 at 08:27
-
Access the sudoers file via 'sudo visudo' – fiat Mar 03 '15 at 09:55
I think this should work out if you save it in /root/.bashrc:
export PATH=/www/foo:$PATH
I forget if it's PATH or PYTHONPATH and if it actually matters, this is based on my user's .bashrc:
export PYTHONPATH=/www/django:$PYTHONPATH

- 183,342
- 71
- 393
- 434
-
No problem. Don't forget to select an answer so others can know it has been solved :) – meder omuraliev Oct 04 '09 at 23:27
-
1This doesn't work for me. I added the path to /root/.bashrc, then ran "sudo env | grep PATH" and the added path was not present. Perhaps it works for PYTHONPATH, but not for PATH. – Jason R. Coombs Mar 27 '10 at 00:50
-
It didn't work for me too. I asked a new question at http://stackoverflow.com/questions/2717043/changed-sudo-path-command-continues-not-being-found – Delirium tremens Apr 26 '10 at 21:38
-
1This is a bad solution. The file /etc/sudoers exists to allow you to change the behavior of sudo. Opening the default root path to a folder that potentially should not be in the root path is a HORRIBLE idea. Also it is path, not pythonpath that controls where bash looks for executables. – h4unt3r Jan 09 '14 at 21:46
You can set the variable in /etc/environment, and then use "sudo -i" to run the script (works in ubuntu 10.10).

- 626
- 6
- 3