Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

No matter what I try, it seems that the LD_LIBRARY_PATH environment variable is not kept after I run a command with sudo. The only way I managed to have it stick, is to prefix my sudo command with LD_LIBRARY_PATH=/the/path whenever I call it from the command-line, but I would like to not have to do this every time.

It seems the env_keep option ignores this variable, and so does the exempt_group option.

My %group currently has ALL=(ALL) NOPASSWD:ALL as its access in sudoers. I would like this specific environment variable to be kept for any command I run.

How can I do this?

My server is running Red Hat Enterprise Linux 5.7.

share|improve this question
You know that allowing this through sudo allows execution of arbitary code as root, right? – pjc50 Mar 20 '12 at 16:47
Yes I know, but it's a requirement in my case. We are well aware of the implications. If it's the NOPASSWD option that is bugging you, it's just an example. I have set it on a local VM server to test this out and got tired of typing my password... – Yanick Girouard Mar 20 '12 at 17:22
up vote 9 down vote accepted

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.

share|improve this answer
That is indeed the behavior that I'm seeing. The question is: Is there a way to work around that without reinventing the wheel? – Yanick Girouard Mar 20 '12 at 17:23
It's not really sudo's problem as it never gets to see the LD_LIBRARY_PATH. I would wrap the commands I want to run in a script and set the environment variable within the secipt. – Iain Mar 20 '12 at 17:51
It must work differently on Solaris then, because we have another server where this behavior does not occur. It's only doing this on Red Hat. Thanks for the explanation though! – Yanick Girouard Mar 20 '12 at 19:19

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.

share|improve this answer
For me (don't know why) it was sufficient just to do step 2, i.e. sudo LD_LIBRARY_PATH=/mypath <cmd> – jfritz42 Aug 6 '14 at 19:07

It sounds like you haven't you done export LD_LIBRARY_PATH yet. Doing so will make environmental variable available to everything, and solve this problem you're having.

Alternatively, if you want a sure-fire persistent environmental variable, consider appending it to your bashrc (~/.bashrc) file, then opening a new terminal session.

share|improve this answer
The variable is exported just fine in my own user's environment. It gets lost after I run anything via sudo. – Yanick Girouard Mar 20 '12 at 17:20

This is happening not only for LD_LIBRARY_PATH but a few other environment variables that could temper with your build environment. In my case, sudo has become:

share|improve this answer

Your Answer


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.