CentOS7 set system environment variables

One, the concept of environmental variables

1. The meaning of environment variables

The execution of programs (operating system commands and applications) requires a runtime environment, which is composed of multiple environment variables.

2. Classification of environmental variables

1) Classified according to the effective scope.

System environment variables : public, effective for all users.

User environment variables : the user's private and customized personalized settings, which only take effect for the user.

2) Classified by life cycle.

Permanent environment variables : Configured in the environment variable script file, these scripts will be automatically executed every time the user logs in, which is equivalent to permanent effect.

Temporary environment variables : temporarily defined in the Shell when in use, and become invalid after exiting the Shell.

3. Linux environment variables

Linux environment variables are also called Shell environment variables. They start with underscores and letters, and consist of underscores, letters (case sensitive), and numbers. Capital letters are customarily used, such as PATH, HOSTNAME, LANG, etc.

Two, commonly used environment variables

1. View environment variables

1) env command

Under Shell, use the env command to view all the environment variables of the current user.

Insert picture description here

The above figure only intercepts part of the environment variables, not all of them.

When using the env command, a lot of environment variables are displayed on the full screen, which is inconvenient to view. You can use grep to filter.

env|grep 环境变量名

For example, check the environment variable that contains PATH in the environment variable name.

env|grep PATH

Insert picture description here

2) echo command

echo $环境变量名

Insert picture description here

Note that the symbol $ cannot be missing, this is a grammatical rule.

2. Commonly used environment variables

1)PATH

Search directory for executable programs, executable programs include Linux system commands and user applications. The specific usage of the PATH variable is described in detail in the later chapters of this article.

Insert picture description here

2) LANG

The language, region, character set, and specific usage of the LANG variable of the Linux system are described in detail in the later chapters of this article.

Insert picture description here

3)HOSTNAME

The host name of the server.

4)SHELL

The shell parser currently used by the user.

5)HISTSIZE

Save the number of historical commands.

6)USER

The username of the currently logged in user.

7)HOME

The home directory of the currently logged in user.

8)PWD

The current working directory.

9)LD_LIBRARY_PATH

C/C++ language dynamic link library file search directory, it is not Linux default environment variable, but it is very important for C/C++ programmers, specific usage is described in detail in the later chapters of this article.

10)CLASSPATH

The directory searched for by JAVA language library files is not a Linux default environment variable, but it is very important to JAVA programmers. The specific usage is described in detail in the later chapters of this article.

Three, set the amount of environment

变量名='值'
export 变量名

or

export 变量名='值'

If the value of the environment variable has no special symbols such as spaces, it can be enclosed without single quotation marks.

Example:

export ORACLE_HOME=/oracle/home
export ORACLE_BASE=/oracle/base
export ORACLE_SID=snorcl11g
export NLS_LANG='Simplified Chinese_China.ZHS16GBK'
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin:.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib:.

The environment variables set by export will become invalid after exiting the Shell and need to be reset the next time you log in. If you want the environment variable to take effect permanently, you need to configure it in the login script file.

1. System environment variables

System environment variables are effective for all users. There are three ways to set system environment variables.

1) Set in the /etc/profile file.

When the user logs in, execute the /etc/profile file to set the system environment variables. However, Linux does not recommend setting system environment variables in the /etc/profile file.

2) Add environment variable script files in the /etc/profile.d directory. This is the recommended method for Linux.

/etc/profile will execute all the script files under /etc/profile.d every time it starts. /etc/profile.d is easier to maintain than /etc/profile. If you don't want any variables, just delete the corresponding shell script under /etc/profile.d.

There are many script files in the /etc/profile.d directory, for example:

Insert picture description here

In the above example, the oracle.sh in the /etc/profile.d directory is the environment variable configuration file of the Oracle database. The content is as follows:

Insert picture description here

3) Set environment variables in the /etc/bashrc file.

The environment variables configured in this file will affect the bash shell used by all users. However, Linux does not recommend setting system environment variables in the /etc/bashrc file.

2. User environment variables

User environment variables only take effect for the current user, and there are many ways to set user environment variables.

In the user's home directory, there are several special files, which lsare invisible and ls .bash_* can be seen by users  .

Insert picture description here

1).bash_profile (recommended first choice)

It is executed when the user logs in, and each user can use this file to configure their own environment variables.

2).bashrc

The file will be read when the user logs in and every time a new Shell is opened. It is not recommended to configure user-specific environment variables in it, because every time a Shell is opened, the file will be read once, and the efficiency will definitely be affected.

3).bash_logout

This file is executed every time you exit the system (exit the bash shell).

4).bash_history

Save the historical commands used by the current user.

3. The execution order of environment variable script files

The execution sequence of environment variable script files is as follows:

/etc/profile->/etc/profile.d->/etc/bashrc->用户的.bash_profile->用户的.bashrc

If the environment variable with the same name is configured in multiple scripts, the configuration in the last executed script shall prevail.

There is another problem that needs attention. The script of /etc/profile.d is executed in /etc/profile. The code is as follows:

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

Therefore, the execution order of /etc/profile.d and /etc/profile depends on how the code is written.

Four, detailed explanation of important environmental variables

1. PATH environment variable

Search directory for executable programs, executable programs include Linux system commands and user applications. If the directory of the executable program is not in the directory specified by PATH, you need to specify the directory when executing.

1) The PATH environment variable stores a list of directories, separated by a colon:, and the dot at the end represents the current directory.

export PATH=目录1:目录2:目录3:......目录n:.

2) By default, PATH contains the directory where Linux system commands are located (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin). If these directories are not included, common Linux commands are also Unable to execute (you must enter the absolute path to execute).

Example:

Insert picture description here

3) In the user's .bash_profile file, PATH will be expanded, as follows:

export PATH=$PATH:$HOME/bin

4) If there is no dot in the PATH variable, you need to add ./or use an absolute path to execute the program in the current directory.

Example:

Insert picture description here

2. LANG environment variables

The LANG environment variable stores the language, region, and character set of the Linux system. It does not need to be manually set by the system administrator. /etc/profile will call the /etc/profile.d/lang.sh script to complete the PATH setting.

The CentOS6.x character set configuration file is in the /etc/syscconfig/i18n file.

The CentOS7.x character set configuration file is in the /etc/locale.conf file, the content is as follows:

Insert picture description here

3. LD_LIBRARY_PATH environment variable

C/C++ language dynamic link library file search directory, it is not Linux default environment variable, but it is very important for C/C++ programmers.

The LD_LIBRARY_PATH environment variable also stores a list of directories. The directories are separated by a colon:, and the dot at the end represents the current directory, which has the same format as PATH.

export LD_LIBRARY_PATH=目录1:目录2:目录3:......目录n:.

4、CLASSPATH

The directory searched for by JAVA language library files is not a Linux default environment variable, but it is very important for JAVA programmers.

The CLASSPATH environment variable also stores a list of directories. The directories are separated by a colon:, and the dot at the end represents the current directory, which has the same format as PATH.

Five, the entry into force of environment variables

1) Under Shell, the environment variables set by export take effect immediately for the current Shell, and become invalid after the Shell exits.

2) The environment variable set in the script file will not take effect immediately. It will take effect when you log in again after exiting the Shell, or use the source command to make it take effect immediately, for example:

source /etc/profile

Six, application experience

Although there are many ways to set environment variables, it is recommended that system environment variables be configured in the /etc/profile.d directory, and user environment variables are configured in the user's .bash_profile . It is not recommended to configure environment changes in other script files. Increase the trouble of operation and maintenance, easy to make mistakes.

Guess you like

Origin blog.csdn.net/JineD/article/details/112967573