21

I have been trying to set environment variables that would be saved upon reboot, but without success.

When I set variables using the export or setenv command, as root or any other user, it gets saved on the session until reboot. After reboot, the variables are lost.

In particular, I need to set $JAVA_HOME.

So I do like this

enter image description here

but nothing is working as I indicated.

What am I doing wrong?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 2
    Please don't post screenshots of text, paste the actual text... – jasonwryan Mar 22 '16 at 18:08
  • 1
    I would argue that this is not a duplicate, the referenced question is not specifically for EL 7 type distros. – simon Oct 30 '17 at 08:30
  • @jasonwryan @Anthon This is not a duplicate questions. User environment variables and system wide env vars like the OP is trying to set are different concepts. The linked question references .bashrc and methods that only work for a single user. This question requires a system-wide method such as the one suggested by @tagwint – Routhinator Jun 28 '18 at 17:10

2 Answers2

21

Depending on your shell, there usually has to be a .<shellnamehere>rc file where you can store these variables that you want persisting across logout and reboot events. .bashrc is where you do it if your shell choice is bash

EDIT: in the terminal, run command

env | grep SHELL

if you see SHELL=bash then, run this command:

echo 'export JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.71-2.b15.el7_2.x86_64"' >> $HOME/.bashrc

this last command have some assumptions. If your SHELL doesn't come up as bash come back here and report what you get. After a logout and login back if you don't see your variable already set, again come back and report what error messages you see, if any.

MelBurslan
  • 6,966
21

If it is about setting JAVA_HOME system-wide, /etc/profile.d/ would be a good choice.

echo export JAVA_HOME="put path to java home here">/etc/profile.d/javaenv.sh
chmod 0755 /etc/profile.d/javaenv.sh

on your next logon you'll have it

Tagwint
  • 2,480