I am installing Tomcat on a RHEL5 machine, and am having some trouble starting it, because JAVA_HOME isn't set. Tomcat is being started using

sudo /sbin/service tomcat start

and runs under the user tomcat. When I am logged in as myself or as the root user, JAVA_HOME is properly set (using /etc/profile.d/java.sh). I am guessing that this isn't being properly passed through by sudo.

Can someone point me in the direction of the change I need to make to get this variable either passed through by sudo or picked up by the tomcat user? I believe there are a number of ways.

Thanks in advance



One way to do it is to add JAVA_HOME to the tomcat start up script, which should be in /etc/init.d


mazianni's answer is correct.

More info:

For a non-interactive shell /etc/profile is not read and therefore /etc/profile.d/*.sh aren't read either.

JAVA_HOME is set in your interactive shell. sudo strips out most environment variables. You could configure the env_keep in /etc/sudoers to allow JAVA_HOME through but if you ever set tomcat to automatically start this method will no longer work. (Hence mazianni's solution is the best.)


Try sudo -E <your command>. This preserves (most of) the environment. See man sudo for more information.


You can temporarily set a variable before execution in BASH. This will be available to the command immediately following.

JAVA_HOME=/path/to/java sudo /sbin/service tomcat start


Read man sudoers.

Using visudo (never edit /etc/sudoers directly!), you can choose to set !env_reset to globally allow (most) environment variables to pass through sudo, or add to env_keep which specific variables should be kept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.