Set environment variable linux global

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

Читайте также:  Arch linux x86 or 64

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

Источник

Setting environment variable globally without restarting Ubuntu

But that requires system restart or X restart. Is it possible to set an environment variable in Ubuntu / Linux so that immediately available system wide without restarting OS or logging out the user?

This means you cannot do what you ask, then: you will at least have to restart X. You cannot change the environment of an already running process.

@fge actually, you can but it requires you to attach to the process with gdb . It works but it’s very hackish

3 Answers 3

The simple answer is: you cannot do this in general.

Why can there be no general solution?

The «why?» needs a more detailed explanation. In Linux, the environment is process-specific. Each process environment is stored in a special memory area allocated exclusively for this process.

As an aside: To quickly inspect the environment of a process, have a look at /proc//env (or try /proc/self/env for the environment of the currently running process, such as your shell).

When a («parent») process starts another («child») process (via fork(2) ), the environment the environment of the parent is copied to produce the environment of the child. There is no inheritance-style association between those two environments thereafter, they are completely separate. So there is no «global» or «master» environment we could change, to achieve what you want.

Why not simply change the per-process environment of all running processes? The memory area for the environment is in a well-defined location (basically right before the memory allocated for the stack), so you can’t easily extend it, without corrupting other critical memory areas of the process.

Possible half-solutions for special cases

That said, one can imagine several special cases where you could indeed achieve what you want.

  • Most obviously, if you do «size-neutral» changes, you could conceivable patch up all environments of all processes. For example, replace every USER=foo environment variable (if present), with USER=bar . A rather special case, I fear.
  • If you don’t really need to change the environments of all processes, but only of a class of well-known ones, more creative approaches might be possible. Vorsprung’s answer is an impressive demonstration of doing exactly this with only Bash processes.
Читайте также:  Linux поиск удаленных файлов

There are probably many other special cases, where there is a possible solution. But as explained above: no solution for the general case.

Источник

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.

Читайте также:  Linux настройка сетевого интерфейса скорость

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.

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