简体   繁体   中英

LD_LIBRARY_PATH with sudo python

I'm trying to use a newer Python (2.7.3) with an old CentOS.

I have a recipe to install python to a non-standard location:

 ./configure --prefix=#{install_path} --with-threads --enable-shared --with-zlib=/usr/include
 make
 make install

I set the PATH and LD_LIBRARY_PATH variables to find bin/python and the .so files using /etc/profile.d/ . This seems to mostly work.

With a non-root user, I get the right Python:

[vagrant@localhost ~]$ python
Python 2.7.3 (default, Dec 24 2012, 15:18:59) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

With a root user, I get the right Python:

[vagrant@localhost ~]$ sudo su
[root@localhost vagrant]# python
Python 2.7.3 (default, Dec 24 2012, 15:18:59) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

However, the $LD_LIBRARY_PATH hack seems to be a little wonked when using sudo:

[vagrant@localhost ~]$ sudo python
python: error while loading shared libraries: libpython2.7.so.1.0:
cannot open shared object file: No such file or directory

Even though the variables look right:

[vagrant@localhost ~]$ sudo which python
/opt/Python-2.7.3/bin/python

Adding Defaults env_keep += "LD_LIBRARY_PATH" to /etc/sudoers does not work.

sudo -i python does work. sudo -E python does not work.

I'm curious what I could do to get sudo to pick up the right Python without -i ?

related:

sudo changes PATH - why?

https://stackoverflow.com/questions/12593336/odd-path-behaviour-on-centos-python

Thanks to this blog post . You can forego the use of $LD_LIBRARY_PATH by linking with LDFLAGS in configure. Where #{ldlibpath} is #{install_path}/lib :

./configure --prefix=#{install_path} --with-threads --enable-shared \
--with-zlib=/usr/include LDFLAGS="-Wl,-rpath #{ldlibpath}"

As noted in the blog post, you will need to mkdir this ldlibpath before running configure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM