When sudo does not take your environment variables and the solution

Have you experienced the strange thing that when sudo on Linux, some environment variables just are just gone?

I had been stuck on a software issue with my Kinect (Model 1473) for quite a while. Today I just found out that the culprit is sudo. When I sudo, an environment variable that I set (e.g., in ~/.bashrc) was ``missing.''

Since I didn't add myself into plugdev group (I am stubborn), I had to use sudo to access Kinect. But I got the error that the driver module libfreenect.so couldn't be found.

$ sudo glview
glview: error while loading shared libraries: libfreenect.so.0.2: cannot open shared object file: No such file or directory

Then I checked with ldd. However, libfreenect.so was there.

$ ldd /usr/local/bin/glview | grep libfreenect
 libfreenect.so.0.2 => /usr/local/lib64/libfreenect.so.0.2 (0x00007f33d0494000)

But it was gone if I sudo:
$ sudo ldd /usr/local/bin/glview | grep libfrenect
 libfreenect.so.0.2 => not found

Then I came up with a solution. How about setting the environment variable when using sudo?
Bingo!

$ sudo LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH glview
Kinect camera test
Number of devices found: 1
GL thread
write_register: 0x0105 <= 0x00
write_register: 0x0006 <= 0x00

Solution:

Google helped me find an explanation: http://arstechnica.com/civis/viewtopic.php?f=19&t=1119442 All I need to do is to change the line
Default env_reset
in /etc/sudoers to
Default env_reset
Default env_keep="LD_LIBRARY_PATH"
You can get more information by reading
man sudoers


Oh, Guns up, (Texas Tech Red) Raiders!

No comments: