Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to permanently change sudo's $PATH variable (Ubuntu 9.x)

Tags:

path

sudo

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?

like image 392
simon slingsby Avatar asked Oct 04 '09 22:10

simon slingsby


People also ask

How do I permanently set a PATH variable in Linux?

Manipulating your PATH variable To make the change permanent, enter the command PATH=$PATH:/opt/bin into your home directory's . bashrc file. When you do this, you're creating a new PATH variable by appending a directory to the current PATH variable, $PATH .

How do I permanently add PATH in Ubuntu?

Permanently add a directory to $PATHbashrc file of the user you want to change. Use nano or your favorite text editor to open the file, stored in the home directory. At the end of this file, put your new directory that you wish to permanently add to $PATH. Save your changes and exit the file.


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

like image 97
h4unt3r Avatar answered Oct 23 '22 02:10

h4unt3r


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
like image 7
meder omuraliev Avatar answered Oct 23 '22 02:10

meder omuraliev