Linux set env variable permanent

How to set environment variable in Linux permanently

How I can set the new environment variables and their value permanently in Linux I used export to set the env variables. But the problem is its session specific. If I open new session the values set will be gone. Thank you in advance

put them into /etc/environ and reboot. However, some services may run with a clean environment though. that depends on the service

@hek2mgl, correct. Those are just examples. I did not answer as this should go to superuser and no need to reboot. Just sudo source /etc/environ

6 Answers 6

If you want to set a variable in your shell (as opposed to the superuser’s):

Keep in mind this depends on your shell.

In point 4, it would be good to add that the .bashrc needs to be reloaded, using : «source .bashrc» , considering you are in the same cwd of .bashrc.

Solution: In order to export and keep environment variables persistent on linux you should export variables decelration into one of the following files: ~/.bash_profile / ~/. bash_login / ~/.profile .

When bash is invoked as an interactive/non-interactive login shell, it first reads and executes commands from the file /etc/profile (if exists) and after looks for these following files ( in that order and do the same) ~/.bash_profile , ~/. bash_login , ~/.profile .

Example: adding secret token into my user profile.

cat > ~/.profile export SECRET_TOKEN=abc123!@# End 

The usual place is ~/.bashrc assuming that you’re using bash, which is the default in most distributions. Check yourself with echo $SHELL . You may use ~/.bash_profile , if you only want to set the variable in login shells (but i.e. not in scripts).

Its bash. only problem is I dont have access to any of the files ~/.bash_profile,.bashrc,/etc/environment. Is it still possible to set the path using command which will not be depend on session?

If you haven’t even access to files in $HOME you will need to set them each time again or include in any scripts anyway. if you have some path like /myapp/ writable you can use /myapp/vars and include source /myapp/vars into your scripts. (Take care of syntax errors in vars, which will be included in your scripts!)

Читайте также:  Install android sdk platform tools linux

there’s a good explanation of when to put it where here : http://www.linuxfromscratch.org/blfs/view/6.3/postlfs/profile.html if you don’t have root access put it somewhere local like .bash_profile or depending on which shell you use. Find your shell by typing the command ps .

This is working inside Debian 11. It should work on other Debian based distributions like Ubuntu etc. I am using the old school nano to edit the file named pam_env.conf that resides in /etc/security/ directory, you may use whatever else you want instead of nano.

sudo nano /etc/security/pam_env.conf

Format for setting the environment variable inside this file is following:

VARIABLE [DEFAULT=[value]] [OVERRIDE=[value]]

(For example, let’s set DXVK_HUD variable with the value full . This is equivalent to export DXVK_HUD=full You can replace the variable name and it’s value to whatever you want for your use cases.)

This is how it is going to look inside that file as a new line:

DXVK_HUD DEFAULT=full OVERRIDE=full

Make the changes by saving it ( CTRL+O key combination is used for making changes through nano). Then press return (Enter key). And then press CTRL+X to exit from nano.

Restart your system. And type env in Terminal and see if you can see your environment variable in the list. It should be there.

Set it in /etc/environment . In my Ubuntu installation this is the place where you can set environment variables persistently. The file may be different for a different distribution. Following is the contents of my /etc/environment file.

See how the environment variable PATH is being set above.

A note on export command

export varname makes variable varname available to any subshell run from the your current shell, i.e. the shell on which you ran the export command. Any other shell, which is either unrelated to your current shell or is a parent will not have this variable. Knowing this, assuming you are using bash shell, you can write the export command in .bashrc file. .bashrc is a file which is run every time a bash shell is started, and hence any command you write in it gets executed in any bash shell opened. Thus writing the export command in the .bashrc file is another option. Similar will be the process for any other shell you are using. for eg. for Z shell the file is .zshrc.

Читайте также:  Server monitoring linux web

Источник

How to Set Environment Variables in Linux

In this tutorial, you will learn how to set environment variables in Ubuntu, CentOS, Red Hat, basically any Linux distribution for a single user and globally for all users. You will also learn how to list all environment variables and how to unset (clear) existing environment variables.

Environment variables are commonly used within the Bash shell. It is also a common means of configuring services and handling web application secrets.

It is not uncommon for environment specific information, such as endpoints and passwords, for example, to be stored as environment variables on a server. They are also used to set the important directory locations for many popular packages, such as JAVA_HOME for Java.

Setting an Environment Variable

To set an environment variable the export command is used. We give the variable a name, which is what is used to access it in shell scripts and configurations and then a value to hold whatever data is needed in the variable.

For example, to set the environment variable for the home directory of a manual OpenJDK 11 installation, we would use something similar to the following.

export JAVA_HOME=/opt/openjdk11

To output the value of the environment variable from the shell, we use the echo command and prepend the variable’s name with a dollar ($) sign.

And so long as the variable has a value it will be echoed out. If no value is set then an empty line will be displayed instead.

Unsetting an Environment Variable

To unset an environment variable, which removes its existence all together, we use the unset command. Simply replace the environment variable with an empty string will not remove it, and in most cases will likely cause problems with scripts or application expecting a valid value.

To following syntax is used to unset an environment variable

For example, to unset the JAVA_HOME environment variable, we would use the following command.

Listing All Set Environment Variables

To list all environment variables, we simply use the set command without any arguments.

An example of the output would look something similar to the following, which has been truncated for brevity.

BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=([0]="0") BASH_ARGV=() BASH_CMDS=() BASH_COMPLETION_VERSINFO=([0]="2" [1]="8") BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="5" [1]="0" [2]="3" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu") BASH_VERSION='5.0.3(1)-release' COLUMNS=208 DIRSTACK=() EUID=1000 GROUPS=() HISTCONTROL=ignoreboth HISTFILE=/home/ubuntu/.bash_history HISTFILESIZE=2000 HISTSIZE=1000 HOME=/home/ubuntu HOSTNAME=ubuntu1904 HOSTTYPE=x86_64 IFS=$' \t\n' LANG=en_US.UTF-8 LESSCLOSE='/usr/bin/lesspipe %s %s' LESSOPEN='| /usr/bin/lesspipe %s' LINES=54

Persisting Environment Variables for a User

When an environment variable is set from the shell using the export command, its existence ends when the user’s sessions ends. This is problematic when we need the variable to persist across sessions.

Читайте также:  Astra linux orel sources list

To make an environment persistent for a user’s environment, we export the variable from the user’s profile script.

    Open the current user’s profile into a text editor

export JAVA_HOME=/opt/openjdk11

Adding the environment variable to a user’s bash profile alone will not export it automatically. However, the variable will be exported the next time the user logs in.

To immediately apply all changes to bash_profile, use the source command.

Export Environment Variable

Export is a built-in shell command for Bash that is used to export an environment variable to allow new child processes to inherit it.

To export a environment variable you run the export command while setting the variable.

export MYVAR="my variable value"

We can view a complete list of exported environment variables by running the export command without any arguments.

SHELL=/bin/zsh SHLVL=1 SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.1pB5Pry8Id/Listeners TERM=xterm-256color TERM_PROGRAM=vscode TERM_PROGRAM_VERSION=1.48.2

To view all exported variables in the current shell you use the -p flag with export.

Setting Permanent Global Environment Variables for All Users

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.

All global profile settings are stored under /etc/profile. And while this file can be edited directory, it is actually recommended to store global environment variables in a directory named /etc/profile.d, where you will find a list of files that are used to set environment variables for the entire system.

    Create a new file under /etc/profile.d to store the global environment variable(s). The name of the should be contextual so others may understand its purpose. For demonstrations, we will create a permanent environment variable for HTTP_PROXY.

sudo touch /etc/profile.d/http_proxy.sh
sudo vi /etc/profile.d/http_proxy.sh
export HTTP_PROXY=http://my.proxy:8080
export HTTPS_PROXY=https://my.proxy:8080
export NO_PROXY=localhost. 1,.example.com

Conclusion

This tutorial covered how to set and unset environment variables for all Linux distributions, from Debian to Red Hat. You also learned how to set environment variables for a single user, as well as all users.

Источник

Оцените статью
Adblock
detector