Wednesday, October 28, 2009

Set system-wide environment variables in Linux

You can set environment variables at any time or from any location in Linux. If you want to set a variable that will be persistent, that is to say will be effective for all users and remain even if the system is rebooted, you have to put them in /etc/environment. For example, if you want to set a variable called JAVA_HOME, you would do it like this:
$ sudo echo -e "JAVA_HOME=\"/usr/lib/jvm/java-6-sun\"" >> /etc/environment
This assumes that you want to be using the "java-6-sun" Java package and not one of the others. Also, make sure you create a backup of the original /etc/environment file if this is your first time editing it:
$ sudo cp /etc/environment /etc/environment.orig
And also make sure you use two angle brakets (>>) and not just one. Using only one (>) will overwrite the file instead of concatenating to the end of it.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.