Detailed explanation of Kali Linux environment variables

Detailed explanation of Kali linux environment variables

1 Introduction

Environment variables (Environment variables), as the name suggests, are variables that store environmental parameters. They are similar to how we define variables to store data when programming. However, environment variables do not store ordinary arithmetic values, but environment parameters used by the operating system. Environment Variables typically contain information used by one or more applications.

Environment variables are not a unique product of the Linux system. In fact, most people may not understand the environment variables of Linux, but they must be familiar with the environment variables of the Windows system, including the famous environment variables PATH.

2. Function

Environment variables are special variables in the operating system that store critical system information and can be accessed by the system and applications. Environment variables store some system and user parameters, such as system directory path, user directory path, program installation path and other information. This information contributes to convenient interaction between the operating system and applications, as detailed below.

(1) Provide system and user parameter information, such as path, version number, etc.

(2) Simplify operations to facilitate users to use certain commands and programs directly in the command line.

(3) Provide configuration information for systems and applications to facilitate the calling and use of systems and applications.

(4) Convenient system management, allowing administrators to easily configure and manage the system.

For example, for pathenvironment variables, when you use a command to ask the system to run a program without specifying the path where the program is located, the system will look for the program in the current directory. If it cannot be found, it will go to the path specified by the variable to search. This makes it easier for us to use the terminal to pathrun For some programs, you do not need to enter the directory where the program is located in the terminal. Later we will focus on understanding the environment variables of the Linux system.

3. Classification

Environment variables in Linux are divided into two categories like Windows systems, user environment variables and system environment variables . After all, Linux systems and Windows systems are multi-user systems, so user environment variables can isolate the variable parameters of each user to prevent Destruction by other users can also prevent the abuse of environment variables.

Linux environment variables are time-sensitive, and environment variables can be set to permanent or temporary . When 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. When an environment variable is declared in the current terminal, it will become invalid after closing the Shell terminal. This is temporary.

4. Command

When we use Windows systems, we generally modify and add environment variables through the graphical environment variable manager. In Linux systems, we need to manage environment variables through proprietary commands and modifying configuration files.

Let's first learn about the use of commands related to environment variables.


(1) Set or display environment variable commands export.

export[变量名称]=[变量值], create a specified environment variable. The specific function of export is to pass the value of a variable to the child process. When a variable is exported, it becomes a global variable and can be accessed by the current Shell and its child processes.

Options:

-f, represents [变量名称]the function name. Using this option, you can also specify the function at the variable name.

-n, delete the specified environment variable. In fact, the environment variable is not deleted, but it is no longer effective.

-p, lists all the environment variables assigned to the program by the Shell. You can also directly execute the export command without specifying this option.

For example:

zhbi98@kali:~/Desktop$ export -p
declare -x HOME="/home/zhbi98"
declare -x LANG="en_US.UTF-8"
declare -x LOGNAME="zhbi98"
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/arm-linux-gcc-eabi/bin"
declare -x PWD="/home/zhbi98/Desktop"
declare -x USER="zhbi98"
declare -x USERNAME="zhbi98"

(2) Display all defined environment variable commands printenv.

Only used to view environment variables.

(3) Display the specified environment variable command printenv [环境变量名称].

Only used to view environment variables.

(4) Display all defined environment variable commands env.

Only used to view environment variables.

(5) Display the specified environment variables echo ${[环境变量名称]}.

Only used to view environment variables.

5. Environment variable configuration

We generally use exportthe command to configure environment variables, and configuration files have different configuration files according to different uses. We can just choose one of the appropriate configuration files to add our environment variables.

Let's take a closer look at the various methods and differences. For example, here we want to /usr/local/arm-linux-gcc-eabi/binadd a GCC program directory to the Linux environment variables PATHto facilitate calling GCC in the terminal.

Note: An environment variable name in Linux can contain multiple variable values, and each variable value needs to be :separated by a colon.

5.1 Modify directly in the terminal

Directly use the export command in the current terminal to modify PATHthe value of and append our program directory to the variable PATH, as follows.

export PATH=$PATH:/usr/local/arm-linux-gcc-eabi/bin

This variable takes effect immediately after modification, but is only effective for the current user and current terminal, so the variable becomes invalid after the current terminal is closed.

5.2 Modify .bashrc file

.bashrcThe file is one of the configuration files of the Bash shell. The file is located in ~/.bashrc. It stores user-defined environment variables, aliases, functions and other Bash shell configuration information. When a user logs into a Bash shell, it automatically executes the commands in the .bashrc file to set up the user's environment.

.bashrcThe file is a hidden file, usually located in the user's home/directory. Users can open and modify the file using any text editor. For example, .bashrcadd your own custom commands, aliases, functions, etc. to the file to facilitate your own use.

.bashrcThey will be automatically loaded every time the Bash shell is started. Environment variables can be configured by modifying the files in the user directory , as follows.

(1) Execute the directory switching command to the directory where the file is located. If the file cannot be edited, you need to modify the file permissions to be writable before executing it.

ls -a # 执行该命令可以使你看见该隐藏文件
vim .bashrc

(2) Add the path to be added to the last line of the existing content of the file, as follows.

export PATH=$PATH:/usr/local/arm-linux-gcc-eabi/bin

It takes effect when a new terminal is opened under the same user, or source .bashrcthe command is manually executed in the terminal, but it is only effective for the current user and is permanent.

If subsequent environment variables load the file and overwrite PATHthe contents, they may no longer take effect.

5.3 Modify the .bash_profile file

.bash_profile(named by some distributions .profile) are the Bash shell's configuration files. The files are located in ~/.bash_profile. They are automatically loaded each time the Bash shell is started. Environment variables can be configured by modifying this file, and the effect is .bashrcsimilar to.

(1) Execute the directory switching command to the directory where the file is located. If the file cannot be edited, you need to modify the file permissions to be writable. If there is no .bash_profilefile, you can create a new one yourself and execute it again.

ls -a # 执行该命令可以使你看见该隐藏文件
vim .bash_profile

(2) Add the path to be added to the last line of the existing content of the file, as follows.

export PATH=$PATH:/usr/local/arm-linux-gcc-eabi/bin

It takes effect when a new terminal is opened under the same user, or source .bash_profilethe command is manually executed in the terminal, but it is only effective for the current user and is permanent.

5.4 Modify bashrc file

This method is to modify the system configuration, where the file is located /etc/bashrc, and modification requires administrator privileges (such as root privileges).

(1) Execute the directory switching command to the directory where the file is located. If the file cannot be edited, you need to modify the file permissions to be writable before executing it.

chmod -v u+w bashrc
vim bashrc

(2) Add the path to be added to the last line of the existing content of the file, as follows.

export PATH=$PATH:/usr/local/arm-linux-gcc-eabi/bin

It takes effect when you open a new terminal, or manually execute source bashrcthe command in the terminal. It is effective for all users of the system and is permanent.

5.5 Modify profile file

These files are system-level shell configuration files located in /etc/profile(some distributions name bash_profile) files that are loaded automatically when the user logs in. In these files, you can set environment variables using the export command or directly writing the variable name and value on one line.

(1) Execute the directory switching command to the directory where the file is located. If the file cannot be edited, you need to modify the file permissions to be writable before executing it.

chmod -v u+w profile
vim profile

(2) Add the path to be added to the last line of the existing content of the file, as follows.

export PATH=$PATH:/usr/local/arm-linux-gcc-eabi/bin

It takes effect when you open a new terminal, or manually execute source profilethe command in the terminal. It is effective for all users of the system and is permanent.

5.6 Modify environment file

This file is a system-level environment variable configuration file. The file is located at /etc/environment. It will be automatically loaded when the system starts. In this file, you can directly write the variable name and value in one line to set the environment variables, but generally the environment is not defined in this file. variables, and I hope this file will not be modified easily.

(1) Execute the directory switching command to the directory where the file is located. If the file cannot be edited, you need to modify the file permissions to be writable before executing it.

chmod -v u+w environment
vim environment

(2) Add the path to be added to the last line of the existing content of the file, as follows.

export PATH=$PATH:/usr/local/arm-linux-gcc-eabi/bin

It takes effect when you open a new terminal, or manually execute source environmentthe command in the terminal. It is effective for all users of the system and is permanent.

5.7 Summary

System-level configuration files are effective for all users of the Linux system, while user-level configuration files are only effective for the current user. The environment variables set in these files will be loaded by the system or user's Shell and can be used by subsequent commands or programs.

(1) System-level environment variable configuration file: /etc/environment, /etc/profile, /etc/bashrc.

(2) User-level environment variable configuration file: ~/.bashrc, ~/.bash_profile.

6. Loading order

We have previously explained how to modify and define environment variables in five configuration files. At this time, you will definitely consider which file your environment variables should be defined in.

In fact, environment variables defined in any file are equivalent and can take effect. However, the difference between definitions in different files is the order in which the variables are executed. In other words, these configuration files are not loaded at the same time when the Linux system starts. , but are loaded by the system or Shell in a certain order.

6.1 Login Shell Non-login Shell

Before understanding the configuration file loading sequence, let's review the login Shell and interactive non-login Shell.

The login shell refers to the shell that is started by the system by default when a user logs in to the system (that is, the shell that is used to output startup information when Linux first starts, or allows you to enter your username and password to log in), usually bash. In this Shell environment, it will Execute some system-level configuration files, such as /etc/profile file and ~/.bash_profile file, etc., to set global environment variables and aliases.

Interactive non-login Shell refers to a new Shell that the user starts manually after logging in to the system, such as entering commands such as bash or sh in the terminal command line, or opening a Terminal window in GNOME or KDE. This Shell does not execute the system Level configuration files, but execute user-level configuration files, such as ~/.bashrc, etc.

6.2 Configuration file loading sequence

When bash is executed in login shell mode, the system-level configuration file needs to be loaded, /etc/profile is read, etc/profile.d is read again, ~/.bash_profile. If these two files do not exist, read ~/ .bash_login reads the ~/.profile file if it does not exist. Generally, if the currently loaded file does not exist, it is ignored and the next file is loaded.

When bash is executed as an interactive non-login shell, the ~/.bashrc file is read.

Depending on the shell type, the configuration file loading order is also different. The specific loading order is as follows:

  Linux Start
     /\
    /  \
   /    \
(Shell)  (Login Shell)
  |           |
  |       etc/profile (bash_profile)
  |           |
  |       etc/profile.d
  |           |
  |       ~/.bash_profile (.profile)
  |           |
  |       ~/.bash_login
  |           |
  |       ~/.profile
  |           |
  +—————> ~/.bashrc

In summary, the process on the right side of the figure is used for login Shell, and the process on the left side is used for interactive non-login Shell. To put it simply, the process on the right is executed every time you log in, and the process on the left is executed when a new command line window is started.


The above is the content related to environment variables. It discusses the concept and function of environment variables, and also discusses the definition or modification of environment variables. This article is also applicable to Ubuntu. If it is helpful to my friends, please give it a thumbs up.

Guess you like

Origin blog.csdn.net/jf_52001760/article/details/130759084