How to set environment variable in linux permanent

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.

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)

Читайте также:  Восстановить root права linux

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

Источник

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

Читайте также:  Minimal linux with browser

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.

Источник

How can I permanently export a variable in Linux?

That variable is lost when the terminal is closed. How do I permanently add this so that this variable value always exists with a particular user?

Читайте также:  Check write permissions linux

6 Answers 6

You can add it to your shell configuration file, e.g., $HOME/.bashrc or more globally in /etc/environment.

After adding these lines, the changes won’t reflect instantly in GUI-based systems. You have to exit the terminal or create a new one and on the server, log out the session and log in to reflect these changes.

@mini-me — ~/bashrc is pulled each time you open a shell. To load it explicitly, use source e.g. — > source ~/.bashrc .

@mini-me: the environment of a process is usually set by the caller and changed from within the process. Changing env from outside a running process is unusual and not doable with export , but try with a debugger

@Mr.Hyde: It usually doesn’t matter. Files are parsed from top to bottom, so if a var definition depends on another, they should be ordered accordingly. So yes the end of the file is fine.

just note Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session. from help.ubuntu.com/community/EnvironmentVariables

You have to edit three files to set a permanent environment variable as follow:

~/.bashrc

When you open any terminal window this file will be run. Therefore, if you wish to have a permanent environment variable in all of your terminal windows you have to add the following line at the end of this file:

~/.profile

/etc/environment

If you want your environment variable in every window or application (not just terminal window) you have to edit this file. Add the following command at the end of this file:

Normally you have to restart your computer to apply these changes. But you can apply changes in bashrc and profile by these commands:

$ source ~/.bashrc $ source ~/.profile 

But for /etc/environment you have no choice but restarting (as far as I know)

A Simple Solution

I’ve written a simple script for these procedures to do all those work. You just have to set the name and value of your environment variable.

#!/bin/bash echo "Enter variable name: " read variable_name echo "Enter variable value: " read variable_value echo "adding " $variable_name " to environment variables: " $variable_value echo "export "$variable_name"="$variable_value>>~/.bashrc echo $variable_name"="$variable_value>>~/.profile echo $variable_name"="$variable_value>>/etc/environment source ~/.bashrc source ~/.profile echo "do you want to restart your computer to apply changes in /etc/environment file? yes(y)no(n)" read restart case $restart in y) sudo shutdown -r 0;; n) echo "don't forget to restart your computer manually";; esac exit 

Save these lines in a shfile then make it executable and just run it!

Источник

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