sudo and LD_LIBRARY_PATH … never the twain shall meet

While writing something for our operations support team to use when running a particular process, I found that sudo does not recognize a previously exported value for LD_LIBRARY_PATH. It also won’t use what is in the .bashrc or .profile for the user that owns the script being sudo’d. You also can’t use os.environ in python to set it, as that only affects processes spawned in python after startup, but not the parent process itself.

It looks like this is due to a security setting in sudo that strips LD_LIBRARY_PATH out of the environment.

Unless you want to set it system wide as in /etc/profile, or change /etc/ld.so.conf, you can write a here document for the python code inside a shell script that exports LD_LIBRARY_PATH first. This is what I did.

While not enormously inconvenient, the process of discovering this took me a couple of hours to track down.

Below is an example that will fail in sudo…

#!/home/oracle/local/bin

import cx_Oracle

…and one that will work…

#!/bin/sh

. /home/oracle/.bashrc #which contains an LD_LIBRARY_PATH export
/home/oracle/local/bin/python <
	    

2 comments for “sudo and LD_LIBRARY_PATH … never the twain shall meet

  1. Kimsie
    September 5, 2012 at 1:13 PM

    just set the
    os.environ variable.
    that’s less clunky, and what the docs advise.

  2. November 16, 2012 at 11:52 AM

    Hi Kimsie,

    If I remember correctly, that didn’t work as I noted in the original post. That could have changed, or I could have been wrong originally, though.

    I will test again when I have time and reply to this.

    Thanks,

    Steve

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.