How can I set an environment variable permanently in CentOS?

I used a following commmand in SSH terminal on my server:

export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib

and it works fine: my program finds all libraries at this point (if I launch it from my SSH command line).

A command

env

shows that LD_LIBRARY_PATH is set properly.

But! When I close my SSH client and reconnect again there is no LD_LIBRARY_PATH set any more.

How to set environment variables constantly on my server under CentOS? Thanks.


Solution 1:

As mentioned in your own answer and the one given by @Fegnoid, exporting the variables in a .sh file inside /etc/profile.d/ or in ~/.bash_profile would do the trick. Keep in mind that if you intend to use these environment variables in a service script, it might not work as you expect since service purges all environment variables except a few.

See https://unix.stackexchange.com/a/44378/148497.

Solution 2:

Export the variables in the ~/.profile or ~/.bash_profile of the user to have them set on login so the line would be

LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib
export LD_LIBRARY_PATH