How to configure sudoers to always keep LD_LIBRARY_PATH envrionment variable?

Solution 1:

You might expect that you can do this using

Defaults env_keep += "LD_LIBRARY_PATH FRED" 

but a quick test on a CentOS 6.2 with Sudo version 1.7.4p5 doesn't pass LD_LIBRARY_PATH but does pass FRED. The sudoers man page has this to say

Note that the dynamic linker on most operating systems will remove variables that
can control dynamic linking from the environment of setuid executables, including
sudo. Depending on the operating system this may include _RLD, DYLD_, LD_, 
LDR_, LIBPATH, SHLIB_PATH, and others. These type of variables are removed from 
the environment before sudo even begins execution and, as such, it is not 
possible for sudo to preserve them

So it looks like the system removes the LD_LIBRARY_PATH from the environment before sudo sees it.

Solution 2:

In order to get this to work I had to do this:

  1. Add Defaults env_keep += "LD_LIBRARY_PATH" to sudoers
  2. Add this alias in my .bashrc file: alias sudo='sudo LD_LIBRARY_PATH=/mypath'

Now any command I call with sudo will have the variable setup.