Set system variables in Debian and CentOS

As indicated in title, this request is probably very common for sysadmins.

For example, when a Java environment is ready, we need to make sure that JAVA_HOME is globally available for some system accounts, so that they can restart java instance smoothly.

The following section would show an example on how to set the variable ORACLE_HOME to be a global one on both Debian and CentOS. Before we start, it’s suggested to have a glance on our former post “difference between login shell and non-login shell“.

Set system variables in Debian / Ubuntu

What is considered the best way to set globally exported environment variables in debian?  There are two ways:

For global settings, you can simply add a new line in /etc/environment like this:

ORACLE_HOME=/opt/oracle

After this modification, re-login is needed, then it would take effect for both login shell and non-login shells.

For a single user, you can apply this line in ~/.bashrc

export ORACLE_HOME=/opt/oracle

The above setting would take effect on both shells.
Normally, we just need to support login shells, so you can append the new line in /etc/profile. It would do the same work, BUT will not take effect on non-login shells.

Set global variables in CentOS / RHEL

There’s a standard way to set global environment variables on Redhat based systems, including Redhat 9.0, Fedora series, Red Hat Enterprise series, CentOS 4/5.
You can create a new file in /etc/profile.d/, and put the variables in it. This directory is used to store new variables:

example# ls /etc/profile.d/
colorls.csh  glib2.sh               krb5-devel.sh         lang.sh
colorls.sh   gnome-ssh-askpass.csh  krb5-workstation.csh  less.csh
cvs.sh       gnome-ssh-askpass.sh   krb5-workstation.sh   less.sh
glib2.csh    krb5-devel.csh         lang.csh              which-2.sh

The file you created should have a suffix “.sh”, so that it would be recognized as variable files by bash. No executable permission is needed for this new file. An example file is listed below:

example# cat oralce.sh
ORACLE_HOME=/opt/oracle

Note: All the above examples are assumed that you’re using BASH. If you use CSH as default shell, you should name this new file as “oracle.csh”, means the suffix should be “.csh”.

If you have any issue on this post, please leave a comment or raise a thread at our support forum, thanks!

Share Button

Leave a comment

Your email address will not be published. Required fields are marked *