0

I am running RHEL 7.3, however the procedure may be similar on other Linux systems.

I have logged in as root. I have a file called openrc which is full of lines like export OS_IDENTITY_API_VERSION=3. I'd like to move this file somewhere so it gets automatically loaded when another user logs in using SSH. Having read related threads, I tried adding this using this as a root user, but this only seems to add these values for the root user. When I login as another user, say 'admin01' the values are not loaded.

echo source /home/admin01/openrc>>~/.bash_profile

I also tried moving this file to /etc/profile.d, but that also seems to load only for root and not for any other user logging in. Can someone PLEASE explain what's going on.

cp /home/admin01/openrc /etc/profile.d/openrc.sh

The /etc/profile has a warning like this;

# 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.
0

Name it /etc/profile.d/openrc.sh or use /etc/environment if you need the variables to be defined for daemons as well.

Here's an example (CentOS 7):

# ls -lhaZ /etc/profile.d/
[...]
-rw-r--r--. root root unconfined_u:object_r:bin_t:s0   /etc/profile.d/foo.sh

Contents:

export HISTSIZE=10000
export HISTFILE=~/.bash_history
export HISTCONTROL=ignoredups:ignorespace:erasedups
export HISTTIMEFORMAT="%y-%m-%d %H:%M "

Logged in as a non-root user:

$ set | grep HIST
HISTCONTROL=ignoredups:ignorespace:erasedups
HISTFILE=/home/user/.bash_history
HISTFILESIZE=10000
HISTSIZE=10000
HISTTIMEFORMAT='%y-%m-%d %H:%M '

Using /etc/environment, put the lines into /etc/environment without export.

Keep in mind that you might need to stick to /etc/environment when you aren't using bash or not using bash as a login shell:

https://access.redhat.com/discussions/731373

  • Just named it openrc.sh and the file is visible from the new user, but still the values are not exported to bash by default. The /etc/environment file is empty in my system. Forgot to mention that in question. Are there any other things to try? – Zac Jul 28 '17 at 6:35
  • Based on your updated answer, my environment is very similar, the SH file present, but the variables are not loaded on non-root user's shell! Also, my /etc/environment file is EMPTY. No lines. Should I still continue adding all the export lines there? – Zac Jul 28 '17 at 6:49
  • Provide a little more details please. What do you do to find out that they aren't set? – fuero Jul 28 '17 at 6:51
  • I do echo $varName - that's empty. Set | grep like you showed, the variables don't exist there as well. I've tried closing the Putty session and logged back to try. Also tried SSH from the same Putty window, still the same result. – Zac Jul 28 '17 at 6:57
  • 1
    Does the openrc.sh have execute permissions? – Tero Kilkanen Jul 28 '17 at 10:39
0

Note that all changes made to /etc/profile and /etc/profile.d will be applied to every user that logs in to the system. Since I have a hunch that this openrc file has authentication credentials, you have to be sure that you actually want this applied to every user. If you only want it applied to certain users, you can copy the file to their home folder and include it in their ~/.profile with a line saying source ~/openrc

  • Or I could simply set chmod 700 and chown admin01 (i.e. for that particular user) to the particular openrc.sh under /etc/profile.d - this way other users can't open the file. Correct? – Zac Jul 29 '17 at 4:53
  • I guess the reason I wouldn't want it in the profile.d folder is tidiness and the purpose of it. profile.d is supposed to be files loaded by every user. In your case, every user does attempt to load the file, lack permissions, and move on (with the exception of root which can see it even though it's owned by admin01). However, it can be confusing later on for someone else who sees it in there but realizes it's not applied. That's why for one offs, it is best to just load it in that user's profile, IMO – Joe Aug 1 '17 at 22:09
  • I guess that was the question. Your statemetn 'include it in their ~/.profile' -how do I include it in that user's ~/.profile from root? It doesn't work when I include the source line, have you tested? – Zac Aug 3 '17 at 17:12
  • Assume you have a user bob whose home folder is /home/bob/ Edit /home/bob/.profile and add a line at the bottom that says source PATH_TO_FILE and it will get loaded. Root can edit any file. As long as that .profile file is owned by the bob user then bob will load it. The file being sourced also will need to be readable by bob for it to work. – Joe Aug 3 '17 at 17:34
  • This page is a nice comprehensive explanation of shells, environment variables and profile files: ibm.com/developerworks/library/l-lpic1-105-1/index.html – Joe Aug 3 '17 at 17:35

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.