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

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/ 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


share|improve this question
up vote 3 down vote accepted

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

share|improve this answer

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.)

share|improve this answer

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

share|improve this answer

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

share|improve this answer

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.

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.