Tell me more ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I just want to setup a system wide environment variable, JAVA_HOME for all users, including root user.

Requirements:

  • accessible to normal users
  • accessible to root
  • always loaded, not only for bash (gnome-terminal does not start a bash by default)
  • to work on Ubuntu, Debian and optionally Red Hat
  • great if addition could be easily scripted
share|improve this question

6 Answers

I don't understand why you ruled out /etc/profile. That is the correct location.

share|improve this answer

On Debian/Ubuntu that would be /etc/environment

I don't know the Red Hat equivalent.

share|improve this answer
I believe /etc/environment is deprecated – Pat James Nov 25 '12 at 19:17

One a side note: Have a look at the Modules Environment. I use this every time I have to offer a complex, versioned, self-extensible, concise UNIX environment to dozens or hundreds of users. It's mainly used on large scale multi-user HPC environments. Just using it for one particular variable is certainly over-engineering it, but it does an awesome job once you need more than a few software packages and their environment.

share|improve this answer
up vote 1 down vote accepted

As nobody added a full answer here is what I currently consider the best, at least for ubuntu but I am open to adapt the answer for other *nix platforms.

# **create**: /etc/profile.d/java.sh
export JAVA_HOME=/usr/lib/jvm/default-java

Other options that were already considered but ruled out:

  • /etc/environment works but is harder to maintain as other tools or people can edit it.
  • /etc/profile - same as above
share|improve this answer

/etc/profile should work. I tested just now to be sure, put export SOMETEST=1234 to /etc/profile and after re-logging echo $SOMETEST gave me 1234 as expected. Also from gnome-terminal

share|improve this answer

You cannot use any ~ specific files in this case. so....

/etc/profile would be the proper place for it in this case. Current logged in users have to get a new login session though, but this should not be that big of a problem.

@wk01: /etc/profile is not loaded by a nonlogin shell by default. your .bashrc is probably loading it...

share|improve this answer

Your Answer

 
discard

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.