sudo - Issue with environment variable

07
2014-07
  • newbie17

    Please help me running below command from user2 account. I cannot figure out what's the issue with this.

    [user2@localhost bin]# sudo -u user1 /opt/Tomcat/bin/shutdown.sh
    Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
    At least one of these environment variable is needed to run this program
    [user2@localhost bin]$ echo $JAVA_HOME
    /opt/Java_7
    

    Entry for user2 in sudoers file is as below :

    %user2 ALL=(user1) NOPASSWD:/opt/Tomcat/bin/startup.sh,/opt/Tomcat/bin/shutdown.sh
    

    However the environment variables are set. I run the same script with root user and it works fine.

    [root@localhost bin]# /opt/Tomcat/bin/shutdown.sh
    Using CATALINA_BASE:   /opt/Tomcat/
    Using CATALINA_HOME:   /opt/Tomcat/
    Using CATALINA_TMPDIR: /opt/Tomcat//temp
    Using JRE_HOME:        /opt/Java_7
    Using CLASSPATH:       /opt/Tomcat//bin/bootstrap.jar:/opt/Tomcat//bin/tomcat-juli.jar
    

    I have checked it further. JAVA_HOME is set for both the users.

    [root@localhost bin]# pwd
    /opt/Tomcat/bin
    [root@localhost bin]# echo $JAVA_HOME
    /opt/Java_7
    [root@localhost bin]# su user1
    [user1@localhost bin]$ echo $JAVA_HOME
    /opt/Java_7
    [user1@localhost bin]$ exit
    [root@localhost bin]# su user2
    [user2@localhost bin]$ echo $JAVA_HOME
    /opt/Java_7
    
    [user2@localhost bin]$ ps aux | grep java
    user1     47066  0.1 10.9 1004608 77364 pts/2   Sl   Jun12   0:15 /opt/Java_7/bin/java -Djava.util.logging.config.file=/opt/Tomcat//conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/Tomcat//endorsed -classpath /opt/Tomcat//bin/bootstrap.jar:/opt/Tomcat//bin/tomcat-juli.jar -Dcatalina.base=/opt/Tomcat/ -Dcatalina.home=/opt/Tomcat/ -Djava.io.tmpdir=/opt/Tomcat//temp org.apache.catalina.startup.Bootstrap start
    user2     49318  0.0  0.1 103240   840 pts/2    S+   02:11   0:00 grep java
    
    [user2@localhost bin]$ sudo -u user1 /opt/Tomcat/bin/shutdown.sh
    Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
    At least one of these environment variable is needed to run this program
    
  • Answers
  • user2313067

    To keep the JAVA_HOME environment variable when using sudo, you should add Defaults env_keep +="JAVA_HOME" to your sudoers file.

  • private_meta

    If you use sudo -u user1, then neither the JAVA_HOME path of user2, nor the path of root will be used, you will need to set the JAVA_HOME path of user1. From the way it looks, you did not set the JAVA_HOME path of user1


  • Related Question

    osx snow leopard - Make environment variables available when running command as sudo
  • W_P

    I am using Curl, and am having a problem trying to get it to recognize the $http_proxy environment variable when using sudo curl. I tried putting export http_proxy=.... in my /etc/profile and restarting the shell, to no avail. I would rather not use su -l and run the command while logged in as root.


  • Related Answers
  • Earlz

    Look in /etc/sudoers. There is a list of environmental variables that get shared between sudo and the regular user shell

  • Marnix A. van Ammers

    You might need to add:

    envkeep http_proxy
    

    to your sudoers file. Check your sudo environmental variables by running:

    sudo -V
    

    as root. Or by running:

    sudo sudo -V