Environment variable configuration file

The user Environment variable configuration file
All users * /etc/profile
  • /etc/bashrc
  • /etc/environment |

| root | * ~/.bashrc

  • ~/.bash-profile |

| | non-root users/home/non-root user name/bashrc |

 

Updating environment variables

source /etc/profile or . /etc/profileCopy the code

 

How to configure environment variables

Linux reads environment variables

Method of reading existing environment variables in the system

Export Displays all environment variables defined by the current system

Echo $PATH Prints the value of the current PATH environment variable

The PATH variable defines the search PATH of the command to run, separated by a colon:

 

Method 1: Export PATH

The export PATH = / usr/local/SRC/python3 / bin: $PATH # or put the PATH in front the export PATH = $PATH: / usr/local/SRC/python3 / binCopy the code

Pay attention to the point

  • Effective time: Immediately
  • Validity period: This parameter is valid for the currently opened terminal and invalid after the window is closed
  • Valid range: current login user
  • You need to add $PATH, otherwise the original PATH will be overwritten

 

Method 2: vim ~/.bashrc

Vim ~/.bashrc # is added on the last lineCopy the code
export PATH=$PATH:/usr/local/src/python3/bin
Copy the code

Pay attention to the point

  • Validity time: The validity takes effect when you open a new terminal using the same user, or manually source ~/. Bashrc
  • Validity period: Permanent
  • Valid range: current login user
  • The value of PATH may be overwritten by subsequent environment variable files

 

Method 3: vim ~/.bash_profile

Vim ~/.bash_profile # is added on the last lineCopy the code
export PATH=$PATH:/usr/local/src/python3/bin
Copy the code

Pay attention to the point

  • Validity time: The validity takes effect when you open a new terminal using the same user, or manually source ~/.bash_profile takes effect
  • Validity period: Permanent
  • Valid range: current login user
  • If there is no ~/. Bash_profile file, you can edit the ~/. Profile file or create a new one

 

Method 4: vim /etc/bashrc

If the /etc/bashrc file is not editable, Need to modify for editable chmod u + w - v/etc/bashrc vim/etc/bashrc # in the last line with the export PATH = $PATH: / usr/local/SRC/python3 / binCopy the code

Pay attention to the point

  • Valid time: It takes effect when you open a new terminal using the same user, or manually source /etc/bashrc
  • Validity period: Permanent
  • Valid range: all users

 

Method 5: vim /etc/profile

If the /etc/profile file is not editable, Need to modify for editable chmod u + w - v/etc/profile vim/etc/profile # in the last line with the export PATH = $PATH: / usr/local/SRC/python3 / binCopy the code

Pay attention to the point

  • Valid time: It takes effect when the same user opens a new terminal, or manually source /etc/profile takes effect
  • Validity period: Permanent
  • Valid range: all users

 

Method 6: vim /etc/environment

If the /etc/bashrc file is not editable, Need to modify for editable chmod u + w - v/etc/environment vim/etc/profile # in the last line with the export PATH = $PATH: / usr/local/SRC/python3 / binCopy the code

Pay attention to the point

  • Valid time: It takes effect when you open a new terminal using the same user, or manually source /etc/environment
  • Validity period: Permanent
  • Valid range: all users

 

Environment variable loading principle analysis

Consider: In what order does Linux load these six environment variables? Do they overwrite the same environment variables?

Classification of environmental variables

User level environment variable profile:

  •  ~/.bashrc 
  • ~/.profile 
  • ~/.bash_profile (centos7 None)
  • /home/ non-root user name /.bashrc

 

System-level environment variable configuration file:

  • /etc/bashrc 
  • /etc/profile 
  • /etc/bash_profile (centos7 None)
  • /etc/environment 

 

Loading order of environment variables

  1. /etc/environment
  2. /etc/profile
  3. /etc/bashrc
  4. ~/.profile
  5. ~/.bashrc