Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

As example to my question, my ~/.bashrc file contains this lines:

> export LD_LIBRARY_PATH=/opt/intel/mkl/lib/ia32:$LD_LIBRARY_PATH
> export LD_PRELOAD=/opt/intel/mkl/lib/ia32/libmkl_core.so

so that Numpy (Python) could find libraries that it needs to run, as it's build with MKL and Intel compilers. This workflow isn't the best, but that's another story.

My question is how can I pass arbitrary variables (like those in ~/.bashrc) when I run program with 'sudo' (but not root)?

Currently, if I run:

sudo python -c "import numpy"

I get an error:

ImportError: libimf.so: cannot open shared object file: No such file or directory*

Some suggestions as sudo -i or sudo -E does not change anything here.


Edit:

I can't answer my question (not enough points :D ) but I'll comment here, in a hope that there are other Linux newbies wondering about sudo traps.

[Only temporarily!] This works for me (~/.bashrc):

alias sudo='sudo env PATH=$PATH VAR1=SOME_VALUE VAR2=SOME_VALUE...'

share|improve this question
    
As soon as you can add in your answer and accept it: gets you a bit of rep and everyone can see it's got an accepted answer ;) –  Rinzwind Aug 20 '11 at 6:37
    
I'm not that ambitious. Although as this is my 2nd question, and my 1st question was answered by myself, I'm to be suspected for sure :D –  zetah Aug 20 '11 at 7:54
add comment

2 Answers

up vote 9 down vote accepted

Environment variables can be simply passed after sudo in form ENV=VALUE and thay'll be accepted by followed command. It's not known to me if there are restrictions to this usage, so my example problem can be solved with:

sudo LD_LIBRARY_PATH=/opt/intel/mkl/lib/ia32:$LD_LIBRARY_PATH LD_PRELOAD=/opt/intel/mkl/lib/ia32/libmkl_core.so python -c "import numpy"
share|improve this answer
    
add comment

The -E option you mention seems to work just fine:

enzotib@host:~$ export DUMMY=dummy
enzotib@host:~$ sudo -E sh -c 'echo $DUMMY'
dummy
enzotib@host:~$ sudo -E env | grep DUMMY
DUMMY=dummy
share|improve this answer
    
Sorry, but I don't understand your answer. Can you provide example on scenario I posted above? i.e. to be able to run sudo python -c "import numpy" with LD_LIBRARY_PATH and LD_PRELOAD defined as written above? –  zetah Aug 20 '11 at 7:24
1  
@zetah: ok, i'm wrong, because it works for generic variable, but not for dymanic linking controlling variables, as stated in section SECURITY NOTES of sudo manual page. –  enzotib Aug 20 '11 at 7:52
    
It is simpler then that (as in provided answer), although man sudo does not make it clear, and when user tries to follow references pointed there, it is very easy to be discouraged by all branches needed to be followed, to be able to decrypt supposed meaning. –  zetah Aug 20 '11 at 9:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.