How to override a PATH environment variable in sudo?

I set PATH in /etc/profile. I do not have any problems in the case of the general user, but PATH does not go when I become the root authority in sudo.

What would you do to override the PATH which is set by /etc/profile even if I become the root authority in sudo?


Solution 1:

Sudo resets PATH, along with many other environment variables, for security reasons. It doesn't matter what is set in /etc/profile, unless you run sudo -i.

The sudo manual page, under SECURITY NOTES, gives an insight about the security implications, along with the options you have to fiddle in your /etc/sudoers file to modify these restrictions, after you understand the implications of doing so.

Solution 2:

root's path is not changed by /etc/profile (security ...)

you have to set it up separately, (e.g. in root's ~/.bash_profile)

Solution 3:

update:

If you want to add a specific PATH to the default PATH of your "root" account when using sudo, or sudo -i:

With Ubuntu 10.04.x LTS:

Modify accordingly both the /etc/environment and the etc/login.defs files.

With Ubuntu 12.04.x LTS:

Modify accordingly both the /etc/environment and the /etc/login.defs files as well as the line Defaults secure_path of the /etc/sudoers file (using visudo).

If you're using a GUI and aren't comfortable with the nano editor in text mode, use the following command to edit the /etc/sudoers file with gedit:

sudo EDITOR=gedit visudo

When you'll save the /etc/sudoers file, visudo will check the syntax of your modifications.

If all is OK, you'll go back to the terminal session.

If there is an error, press the letter "e" to edit the /etc/sudoers file again and correct your modification.

I didn't try with Ubuntu 12.10 yet but I guess (and hope) it's the same thing than for Ubuntu 12.04.x!

Cheers.