Background Info:
I am trying to configure a Linux server running CentOS 7 for multiple users. After installing java I want to globally set the $JAVA_HOME
environment variable for all users and add it to the $PATH
.
To do this I went to the /etc/profile
script that gets called on startup. I followed the advice at the top of the script:
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
I created my own /etc/profile.d/custom.sh
script to configure the environment for java
custom.sh
# Java configuration
export JAVA_HOME=/usr/lib/jvm/java
export PATH=${JAVA_HOME}/bin:${PATH}
The Problem:
Whenever I log in, the /etc/profiles.d/custom.sh
script (along with any other script I put in /etc/profiles.d
) gets called twice and adds $JAVA_HOME
to the $PATH
twice.
I realized that this seems to happen because all of the scripts in /etc/profiles.d
are called from both /etc/profile
AND /etc/bashrc
.
# This is in both /etc/profile and /etc/bashrc
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
This really doesn't seem to be correct, but I did some digging and it turns out that this seems to just be the way it is…
Questions:
-
Is this a bug in the CentOS 7 operating system?
-
Is there a good workaround that I can use to create global environment settings without them being set twice? (I don't want to set them in
/etc/environment
because I can't use bash variables in that file.)
Best Answer
I've been spawning hundreds of centos 7 the last 6 months, tweaking them in some ways, and I've never seen what you decribe. Like said Thomas, by default, /root/.bashrc doesn't call/source scripts in /etc/profile.d.
EDIT : Actually, /root/.bashrc DOES source /etc/prodile.d/*, by /etc/bashrc, and prevent the echos :
Guess my recent problems where different, which I had to solve them fast and didn't dig enough. My apologies.
OLD ANSWER
1/ I guess it's really not a bug (what ISO did you get for your centos 7 ? Did you md5sum it ? Asking that question sounds actually weird to me)
2/ and then there's no workaround, but removing the code that makes scripts sourced twice ?
/etc/profiles.d seems the right place to put some scripts, if they're "silent", meaning defining some environment variables but not making echo's or calling some dynamic motd, or it will interact in some bad ways at some point, with other scripts you'll run (got the experience with netdata install for lately).
Some interesting points to read on unix.stackexchange.