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.

Answer from Kieron on Stack Overflow
Top answer
1 of 4
385

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:

  1. Login/Non-login shell
  2. Interactive/Non-interactive shell

bash

  1. Bash as login shell will load /etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile in the order
  2. Bash as non-login interactive shell will load ~/.bashrc
  3. 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.

2 of 4
102

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 "$@"
Discussions

linux - Set persistent environment variable for all users - Unix & Linux Stack Exchange
I am running Ubuntu on a local PC with the following linux distro/kernel: $ lsb_release -a >> ubuntu 16.04.3 LTS $ uname -r >> 4.10.0-33-generic I have a python (3.5) script which calls More on unix.stackexchange.com
🌐 unix.stackexchange.com
September 5, 2017
How to set environment variables globally and permanently so they can be available in all shell processes I open and not just single one ?
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. Yes - thats how it works. if you want to set things for each new bash shell, you can do that in your bash configs. particular shell process Exported variables set up a COPY of the variable to the CHILDREN processes. Thats basically IT.. one way, and the children do not alter each others variables. https://www.reddit.com/r/linux4noobs/comments/1cyzrqb/when_setting_the_environment_variables_on_the/ https://ioflood.com/blog/bash-environment-variables/ More on reddit.com
🌐 r/linux4noobs
2
2
May 24, 2024
path - How do I set a user environment variable? (permanently, not session) - Unix & Linux Stack Exchange
This is irritating me. I seen several suggestions (all using different files and syntax) and none of them worked. How do I set an environment variable for a specific user? I am on debian squeeze. ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
September 28, 2011
Debian Linux - Setting an environment variable for all users - Unix & Linux Stack Exchange
I added the following in /etc/profile so that all my users can see the path export JAVA_HOME=/opt/jdk11 export PATH=$PATH:$JAVA_HOME/bin But after restarting my laptop, I cannot see the changes us... More on unix.stackexchange.com
🌐 unix.stackexchange.com
🌐
InterServer
interserver.net › home › linux › how to set environment variables permanently in linux
How to Set Environment Variables Permanently in Linux - Interserver Tips
January 2, 2026 - You can set environment variables permanently in several ways: For just your user account: Edit your shell configuration file (like .bashrc, .bash_profile, or .zshrc). For all users: Edit the global /etc/environment file or /etc/profile. For system services: Use systemd service overrides.
🌐
LabEx
labex.io › questions › how-to-make-environment-variables-permanent-in-linux-17996
How to Make Environment Variables Permanent in Linux | LabEx
July 25, 2024 - To make an environment variable permanent using the export command, add the following line to your shell configuration file (e.g., .bashrc, .bash_profile, or .profile): ... This will set the environment variable for the current shell session ...
🌐
Reddit
reddit.com › r/linux4noobs › how to set environment variables globally and permanently so they can be available in all shell processes i open and not just single one ?
r/linux4noobs on Reddit: How to set environment variables globally and permanently so they can be available in all shell processes I open and not just single one ?
May 24, 2024 -

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 ?

Top answer
1 of 2
2
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. Yes - thats how it works. if you want to set things for each new bash shell, you can do that in your bash configs. particular shell process Exported variables set up a COPY of the variable to the CHILDREN processes. Thats basically IT.. one way, and the children do not alter each others variables. https://www.reddit.com/r/linux4noobs/comments/1cyzrqb/when_setting_the_environment_variables_on_the/ https://ioflood.com/blog/bash-environment-variables/
2 of 2
2
That may depend on the distribution and the types of shell you are using. I don't have one here that uses an /env directory on the root file system. On Debian, Ubuntu and a lot of other systems you have /etc/profile to setup stuff systemwide and for all users (valid for sh, ash, ksh, bash, zsh). csh would be different but this generally not used by most Linux users. Fish might be different, too, I do not use it. NixOS has a different structure. General stuff might go into /etc/nix/nix.conf If it is only for one user in his/her home directory: there is $HOME/.profile to set this up (also sh, ash, ksh, bash, zsh). Concerning containers: you have to set this up per container. This is also valid if you are using toolbox inside an "immutable" or "atomic" distribution like Fedora Silverblue. By the way, instead of predefining specific shell variables globally, you may create shell scripts for all users residing in /usr/local/bin that contains the shell variables needed. E.g. /usr/local/bin/linuxoracle.sh: #!/usr/bin/env bash # file: linuxoracle.sh HOST=$(hostname) echo "$(shuf -i 2020-2030 -n 1) is the Year of the Linux Desktop on $HOST"
Find elsewhere
🌐
Greenwebpage
greenwebpage.com › home › blog › how to set environment variables in linux
How to Set Environment Variables in Linux - Greenwebpage Community
March 14, 2024 - You can also use the “env” ... on your Linux system: After setting the environment variable permanently, you can find it (TEST_ENV=Set the test value as permanent) in your variables list.
🌐
Serverlab
serverlab.ca › home › how to set environment variables in linux
How to Set Environment Variables in Linux - Serverlab -
September 11, 2020 - A permanent environment variable that persists after a reboot can be created by adding it to the default profile. This profile is loaded by all users on the system, including service accounts.
🌐
FOSS Linux
fosslinux.com › home › linux distributions › debian & ubuntu › ubuntu › configuring permanent environment variables in ubuntu
How to Set Permanent Environment Variables in Ubuntu (2026 Guide)
May 6, 2026 - To add a permanent variable here, I open the file using sudo nano /etc/environment and append the key-value pair, such as JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64". Once saved, a complete reboot of the system is required for these global ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-set-an-environment-variable-in-linux
How to Set an Environment Variable in Linux
October 26, 2022 - Sometimes you might need to define a global environment variable that is accessible by all users. For that, we need to first declare a variable and make changes in relevant files where environment variables are read from.
🌐
Red Hat
redhat.com › sysadmin › linux-environment-variables
Linux environment variable tips and tricks
November 20, 2025 - There is a line in /etc/profile that reads: export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL · To make permanent changes to the environment variables for all new accounts, go to your /etc/skel files, such as .bashrc, and change the ...
Top answer
1 of 2
3

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.

2 of 2
3

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.

🌐
Cherry Servers
cherryservers.com › home › blog › linux › how to list, set and manage linux environment variables
How to List, Set and Manage Linux Environment Variables | Cherry Servers
November 7, 2025 - To set system-wide Linux environment variables, you can edit the /etc/environment file. Instead of appending export commands to the file, append the <NAME>='<VALUE>' pair to the end of the file.
🌐
HackerNoon
hackernoon.com › mastering-environment-variables-in-linux-a-comprehensive-guide
Mastering Environment Variables in Linux: A Comprehensive Guide | HackerNoon
November 22, 2023 - Comprehensive guide to setting environment variables in Linux for single users and system-wide configurations, including code examples and detailed explanations
🌐
Linode
linode.com › docs › guides › how-to-set-linux-environment-variables
How to Set and Use Linux Environment Variables | Linode Docs
May 4, 2021 - The example appends the example-directory located in the user’s home directory to the PATH environment variable. ... PATH is a default environment variable that defines directories where your shell can look for executables. This variable allows you to run an executable without having to specify its path. The unset command removes an environment or shell variable from the session. This example removes EXAMPLE_VARIABLE. ... You can set an environment variable permanently between shell sessions and users.