Save environment variable in linux

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!)

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 .

Читайте также:  Windows recovery tool linux

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.

Источник

Bash Scripts Set Environmental Variables with EXPORT [HowTo]

Bash Set/Export Environmental Variable

Generally, variables declared in Bash/Shell scripts exist only within the scope of that running Bash/Shell script.

To make them available elsewhere, they can be set as an environmental variable – meaning that the variable will be available when executing commands outside of the script on your system – for example, making the variable available from the command line after the script has completed.

Читайте также:  D link dwa 125 driver linux

The export keyword does this – here’s how to use it.

What is an Environmental Variable

An environmental variable works much like any other variable, but it’s available everywhere – inside scripts, on the command line, and to other running programs.

Your system already has many environmental variables defined – for example, your home directory is available by reading the environmental variable $HOME.

You can view all currently set environmental variables with the env command:

Set Environmental Variables with export

The following script, testScript.sh, sets an environmental variable and then exits:

#!/bin/bash export MY_ENV_VAR="Save the environment!"

Now, when the above script is executed:

The variable MY_ENV_VAR is available after it has been completed. This can be confirmed by running:

The environmental variable has been set and is now available across the system.

The printenv command can also be used to view an environmental variable

Persisting After Reboot

Environmental variables set with export will not persist a reboot of your computer. To permanently set an environmental variable, it must be declared in your ~/.bashrc file.

The ~/.bashrc file is a script that is run each time you log in. By adding your export statements to it, your environmental variables will be added for each session you log in to.

Above, the nano text editor is used to edit the file. Add your export statements to the end of the file, and they’ll be there after you reboot:

export MY_ENV_VAR="Save the environment!"

System-Wide Environmental Variables

These environmental variables will only be there for the current user. If you’re an administrator and you want to make them available to all users and processes, add the lines to the /etc/environment file instead:

Источник

How do I set environment variables?

I’m trying to set up Apache Tomcat on my pc, and it wants me to set up an environment variable for CATALINA_HOME . Does any know how to do this?

7 Answers 7

In bash you can set variables like this:

export CATALINA_HOME=/opt/catalina 

most other shells follow this convention, but not all. You can set it permanently in ~/.profile for bash (and as before, other shells have other locations)

for session-wide variables, help.ubuntu.com recommends ~/.profile as probably the best file for placing environment variable assignments in, since it gets executed automatically by the DisplayManager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.

Читайте также:  Обновить python linux mint

Updated the answer. The reason why i used .bashrc instead was that at some point .profile wasn’t sourced automatically. But if it works now, it’s better to use it.

It is to be noted that when you add new variables in ~/.bash_profile for instance, they won’t be automatically set in the environment until your next login. See man bash, and look for INVOCATION for more details.

To set permanent environment variables in latest Ubuntu versions (from 14.04 and above) add the variables to /etc/environment . For that follow the below instructions,

Open the terminal and run

sudo -H gedit /etc/environment 

the provide your password, then in the prompted text file

then add the variables like

Sample of the /etc/environment is given below

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" JAVA_HOME="/usr/lib/jvm/java-8-oracle/" AXIS2_HOME="/opt/axis2-1.7.4/" ANT_HOME="/opt/apache-ant-1.9.7/" 

don’t forget to logout and login again to enable the environment variables.

Environment variables should already work

If you are using the tomcat6 package from the Ubuntu repositories, then the CATALINA_HOME and other environment variables are already set, in the /etc/init.d/tomcat6 startup script.

If you are installing tomcat outside the package manager (hopefully in /opt or somewhere else outside the managed file system), then running the TOMCAT/bin/startup.sh should use the relative location to define the CATALINA_HOME.

Setting the Environment variable

If for some reason you still need to set an environment variable you can open a terminal window and type in the command:

export CATALINA_HOME=/path/to/the/root/folder/of/tomcat 

This environment variable will now work within that terminal window, but if you open another window or logout/login you loose that setting.

Make the environment variable permanent

To make the environment variable setting permanent, there are several places you can define the setting.

To be really sure the setting is being picked up, add the above setting to one of the startup script for tomcat:

yourtomcatfolder/bin/startup.sh yourtomcatfolder/bin/catalina.sh 

Note: startup.sh calls the catalina.sh. You should add the setting at the start of one of these files (after any initial comments)

The standard way for global environment variables would be to add an entry in /etc/environment (you do not use the command export in this file as it is not a normal bash script)

CATALINA_HOME=/path/to/the/root/folder/of/tomcat 

Not recommended

You can set the environment variables in the bash (command line shell) configuration files, but these are not recommended as they are not always picked up (eg. if you are running a server that you dont login to to run tomcat): ~/.bashrc | ~/.profile | /etc.bash.bashrc | /etc/profile

Источник

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