Use carefully, there are security issues with sudo and variables.
From man sudoers
I found that you should use
Defaults env_reset
Defaults env_keep += "PYTHONPATH OTHERVARIABLE YETANOTHER"
In Ubuntu, sudo
does preserves some variables. sudo -i
is more like logging in as root and then running the command.
Both may be inconvenient, the former for sudo nano myfile
leaves root-owned files inside your home and the latter for sudo -i nano myfile
will try to open /root/myfile.
Run
sudo printenv PATH
and see what it gives. Here it gives
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin
for example. Now run sudo visudo
and add the line
Defaults secure_path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin
replacing by what you found just before. Append a new path to it if you need.
About libraries:
sudo LD_LIBRARY_PATH=/usr/lib/path/to/a/safe/library your command
Linux distributions take a lot of
care with PATH
, and you really should be careful before playing with it.
Be specially careful about adding paths like ".
" or /home/username
, it is unsecure.
One of the dangers of adding paths is that it opens for the possibility of files on these paths getting executed by root
, opening a windows in the system security that may be exploited by malicious software. There may be other dangers. Just make sure you know what you are doing. Bypassing sudo
security measures may render your Solaris as safe as Windows XP.
http://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo – Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-12-08T16:51:34.627