As well as /etc/profile which others have mentioned, some Linux systems now use a directory /etc/profile.d/; any .sh files in there will be sourced by /etc/profile. It's slightly neater to keep your custom environment stuff in these files than to just edit /etc/profile.
As well as /etc/profile which others have mentioned, some Linux systems now use a directory /etc/profile.d/; any .sh files in there will be sourced by /etc/profile. It's slightly neater to keep your custom environment stuff in these files than to just edit /etc/profile.
If your Linux OS has this file:
/etc/environment
You can use it to permanently set environmental variables for all users.
Extracted from: Linux: Variables de entorno permanentes
You can add it to the file .profile or your login shell profile file (located in your home directory).
To change the environmental variable "permanently" you'll need to consider at least these situations:
- Login/Non-login shell
- Interactive/Non-interactive shell
bash
- Bash as login shell will load
/etc/profile,~/.bash_profile,~/.bash_login,~/.profilein the order - Bash as non-login interactive shell will load
~/.bashrc - Bash as non-login non-interactive shell will load the configuration specified in environment variable
$BASH_ENV
$EDITOR ~/.profile
#add lines at the bottom of the file:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
zsh
$EDITOR ~/.zprofile
#add lines at the bottom of the file:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
fish
set -Ux LD_LIBRARY_PATH /usr/lib/oracle/11.2/client64/lib
set -Ux ORACLE_HOME /usr/lib/oracle/11.2/client64
ksh
$EDITOR ~/.profile
#add lines at the bottom of the file:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
bourne
$EDITOR ~/.profile
#add lines at the bottom of the file:
LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
ORACLE_HOME=/usr/lib/oracle/11.2/client64
export LD_LIBRARY_PATH ORACLE_HOME
csh or tcsh
$EDITOR ~/.login
#add lines at the bottom of the file:
setenv LD_LIBRARY_PATH /usr/lib/oracle/11.2/client64/lib
setenv ORACLE_HOME /usr/lib/oracle/11.2/client64
If you want to make it permanent for all users, you can edit the corresponding files under /etc/, i.e. /etc/profile for Bourne-like shells, /etc/csh.login for (t)csh, and /etc/zsh/zprofile and /etc/zsh/zshrc for zsh.
Another option is to use /etc/environment, which on Linux systems is read by the PAM module pam_env and supports only simple assignments, not shell-style expansions. (See Debian's guide on this.)
These files are likely to already contain some assignments, so follow the syntax you see already present in your file.
Make sure to restart the shell and relogin the user, to apply the changes.
If you need to add system wide environment variable, there's now /etc/profile.d folder that contains sh script to initialize variable.
You could place your sh script with all you exported variables here.
Be carefull though this should not be use as a standard way of adding variable to env on Debian.
To do if for all users/shells, depending on distro you could use /etc/environment or /etc/profile. Creating a new file in /etc/profile.d may be preferable if it exists, as it will be less likely to conflict with updates made by the packaging system.
In /etc/environment, variables are usually set with name=value, eg:
ORACLE_HOME=/usr/lib/oracle/11.2/client64
In /etc/profile, you must use export since this is a script, eg:
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
Same goes for a file under /etc/profile.d, there also may be naming restrictions which must be met for the file to work. On Debian, the file must have the extension .sh (although does not need a bang line or executable permissions since it is sourced). check your distro documentation or look at the /etc/profile script to see how these files are loaded.
Note also though that setting LD_LIBRARY_PATH permanently is potentially problematic, including being a security risk. As an alternative, I would suggest finding some way to prepend the LD_LIBRARY_PATH to the start of the command line for each program that needs it before running. Eg:
LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib myprog
One way to do this is to use a wrapper script to run the program. You could give this the same name as your program and put it in /usr/local/bin or anywhere that appears before the location of your program in PATH. Here is an example script (don't forget to chmod +x the script):
#!/bin/sh
LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib /real/location/of/myprog "$@"
linux - Set persistent environment variable for all users - Unix & Linux Stack Exchange
How to set environment variables globally and permanently so they can be available in all shell processes I open and not just single one ?
path - How do I set a user environment variable? (permanently, not session) - Unix & Linux Stack Exchange
Debian Linux - Setting an environment variable for all users - Unix & Linux Stack Exchange
Videos
/etc/profile (and hence /etc/profile.d) is read for login shells. sudo su other_user does not run a login shell. It is a bad practice, it leaves the environment contaminated with variables from the original user. You should do either of these commands instead:
sudo -iu other_user
sudo su - other_user
Both of these load /etc/profile because they start login shells, and both start with relatively clean environments.
Ideally, though, the variables should be set in /etc/environment if possible. That file should be read by su, so variables there would be available irrespective of whether a login shell is started. However, that file doesn't support shell syntax, so if you need complex shell code to set your variables, you can't use it.
Following How to permanently set environmental variables · U&L bash will load
/etc/profile
so that's the right place to add variables effective for all users.
Hello. I am still noob in Linux ecosystem. I wanted to set some environment variables and ran command export VAR=value , but as it turns out this only sets an env variable in a particular shell process and when I open other shell and run printenv I no longer see those environment variables that I set.
How can I set env variables that can be used in all shells ? Should I set it in /etc/profile.d/ directory or is there some other location I should write my env variables to ? If this is not the right directory where should I define the env variables ?
You have to put the declaration in the initialization files of your shell:
If you are using bash, ash, ksh or some other Bourne-style shell, you can add
ABC="123"; export ABCin your
.profilefile (${HOME}/.profile). This is the default situation on most Unix installations, and in particular on Debian.If your login shell is bash, you can use
.bash_profile(${HOME}/.bash_profile) or.bash_logininstead.Note: If either of these files exists and your login shell is bash,
.profileis not read when you log in over ssh or on a text console, but it might still be read instead of.bash_profileif you log in from the GUI. Also, if there is no.bash_profile, then use.bashrc.If you've set zsh as your login shell, use
~/.zprofileinstead of~/.profile.If you are using tcsh, add
setenv ABC "123"in
.loginfile (${HOME}/.login)if you are using another shell look at the shell manual how to define environment variables and which files are executed at the shell startup.
Use /etc/environment file for setting the environment variables. Then add the following line inside the /etc/environment file.
ABC="123"
Now the ABC variable will be accessible from all the user sessions. To test the variable output first refresh the environment variable using command
source /etc/environment
and run echo $ABC.
Environments for shells
Essentially, anything that runs processes will tend to read a configuration file on starting up, and to affect the environment of that, you need to hit its configuration file.
For user shells, "obvious" places are .profile, .bashrc, .bash_profile (I think) and maybe a couple of others I don't remember. Obviously, more and others if you use zsh, csh, tcsh or whatever as a shell.
There are initialization files read by your windowing environment, which may be either KDE or Gnome. The particular window manager you run underneath that may also read a config file. I admit I don't know the names of those files even for my own installation.
Finally, there are usually "master" configuration files for all those environments somewhere in /etc. They provide defaults for stuff the users don't.
I think that programs that install themselves conscientiously check the various possibilities. Various Linux distributions may offer some helper scripts for this.
cron
This one is a lot easier. For security reasons, cron only passes a couple of environment variables to subprocesses, ALWAYS. I think USER is one of those, and MAILTO another. As far as I know, there's no PATH set - this often annoys newbies. The environment of a cron job is completely different from your shell environment! Anything you want in the environment, you either pass in on the command line in crontab, or you start up a script and let that set up whatever environment it needs.
To apply a bashrc change to all users, you can modify /etc/bash.bashrc (This is for Ubuntu).
Also, as indicated in the answer above, make sure that this file is sourced by /etc/profile.
You can put the variable assignments in one of the shell startup scripts:
/etc/profile
/etc/bash_profile
/etc/bashrc
for bash and some other shells, depending on how your particular distro is configured. There are equivalents for most shells. There is also a file
/etc/environment
which is not a script but contains enviroment variable assignments, and is loaded at login via pam_env.so. Obviously this works only on systems that use PAM for authentication (almost all Linux distros, but probably not Solaris).
Aside from /etc/profile, etc., adding a script into /etc/profile.d to achieve your feature is ideal because it works well with packagers which expect to be able to install and uninstall.
To set variable only for current shell:
VARNAME="my value"
To set it for current shell and all processes started from current shell:
export VARNAME="my value" # shorter, less portable version
To set it permanently for all future bash sessions add such line to your .bashrc file in your $HOME directory.
To set it permanently, and system wide (all users, all processes) add set variable in /etc/environment:
sudo -H gedit /etc/environment
This file only accepts variable assignments like:
VARNAME="my value"
Do not use the export keyword here.
Use source ~/.bashrc in your terminal for the changes to take place immediately.
To set an environment variable once, use the export command in the prompt, not in a shell script:
$ export THEVAR=/example
The variable will be set for the rest of the shell session or until unset.
To set an environment variable everytime, use the export command in the .bashrc file (or the appropriate initialization file for your shell).
To set an environment variable from a script, use the export command in the script, and then source the script. If you execute the script it will not work.
For an explanation of the difference between sourcing and executing see this answer:
- What is the difference between executing a Bash script vs sourcing it?
Perhaps you are testing in a non-interactive shell. For interactive shells, both /etc/profile and /etc/bash.bashrc are sourced, but for non-interactive shells, only /etc/bash.bashrc is sourced. You might try putting your variables in that file.
/etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile and related files on other shells are discussed at length in this answer.
First lines in /etc/bash.bashrc state this:
System-wide .bashrc file for interactive bash(1) shells. To enable the settings / commands in this file for login shells as well, this file has to be sourced in /etc/profile.
So, system wide additional environment variables, to use in any user terminal, should be located in it. Even root environment will use these.