Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the configuration files of Ubuntu Linux system environment variables

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the Ubuntu Linux system environment variable configuration files". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the Ubuntu Linux system environment variable configuration files are.

UbuntuLinux system environment variable configuration file UbuntuLinux system environment variable configuration file is divided into two types: system-level file and user-level file, the following details of the environment variable configuration file. 1. System-level files:

/ etc/profile: the first file used by the operating system to customize the user environment when logging in, which sets the environment information for each user of the system, and is executed when the user logs in for the first time. And collect the settings for shell from the configuration file in the / etc/profile.d directory. This file is usually called the / etc/bash.bashrc file.

/ etc/bash.bashrc: a system-level bashrc file that executes this file for every user running bash shell. When bash shell is opened, the file is read.

/ etc/environment: the second file used by the operating system when logging in. The system sets the environment variable of the environment file before reading your own profile.

two。 User-level files:

~ / .profile: the third file used when logging in is the .profile file, which each user can use to enter shell information dedicated to his or her own use. When the user logs in, the file is executed only once! By default, he sets some environment variables to execute the user's .bashrc file.

~ / .bashrc: this file contains bash information specific to your bash shell, which is read when you log in and each time you open a new shell. It is not recommended to put it here, because every time you open a shell, this file will be read once, which is not efficient.

~ / .bash_profile: each user can use this file to enter shell information dedicated to their own use. When the user logs in, the file is executed only once! By default, he sets some environment variables to execute the user's .bashrc file. ~ / .bashrc is interactive and login is used to enter bash to run ~ / .bashrc is interactive non-login to enter bash to run, usually the settings of the two are roughly the same, so usually the former will call the latter.

~. / bash_login: this is not recommended and will not affect the graphical interface. And .bash _ profile has a higher priority than bash_login. When they exist, they are read when the login shell starts.

~ / .bash_logout: execute this file every time you exit the system (exit bash shell).

~ / .pam_environment: user-level environment variable settings file.

In addition, the variables (global) set in / etc/profile can act on any user, while the variables (locally) set in ~ / .bashrc, etc., can only inherit the variables in / etc/profile, they are "father-son" relationships.

2. Comparison between / etc/profile and / etc/ enviroment

First, let's do an experiment:

First add export LANG=zh_CN to / etc/profile, log out of the system and log in again, and the login prompt is displayed in English. Delete the export LANG=zh_CN in / etc/profile, add the LNAG=zh_CN to / etc/environment, log out of the system and log in again, and the login prompt displays in Chinese.

The process of establishing a user environment always executes / etc/profile first and then reads / etc/environment. Why is there such a difference as described above?

/ etc/environment should be executed first, followed by / etc/profile.

/ etc/environment is the environment that sets up the entire system, while / etc/profile is the environment that sets up all users, the former has nothing to do with the logged-in user, and the latter is related to the logged-in user.

The execution of the system application may be independent of the user environment, but related to the system environment, so when you log in, the prompt information you see, such as the display format of date and time information, is related to the LANG of the system environment, the default LANG=en_US, if the system environment LANG=zh_CN, the prompt message is in Chinese, otherwise it is in English.

For the user's SHELL initialization, execute / etc/profile first, and then read the file / etc/environment.

For the whole system, / etc/environment is executed first. Is this understood correctly?

/ etc/enviroment-- > / etc/profile-- > $HOME/.profile-- > $HOME/.env (if present)

/ etc/profile is the environment variable for all users

/ etc/enviroment is the environment variable of the system

The order in which shell is read when logging in to the system should be

/ etc/profile-> / etc/enviroment-- > $HOME/.profile-- > $HOME/.env

The reason should be the difference between user environment and system environment.

If the same variable has different values in the user environment (/ etc/profile) and the system environment (/ etc/environment), it should be based on the user environment.

Note: when executing a program in shell, shell provides a set of environment variables. Export can add, modify or delete environment variables for use by subsequent programs. The effect of export is limited to this login operation.

The procedure to execute the file when logging in to Linux is as follows:

When you first log in to Linux, start the / etc/profile file first, and then start one of the ~ / .bash_profile, ~ / .bash_login, or ~ / .profile files in the user directory, in the order of ~ / .bash_profile, ~ / .bash_login, ~ / .profile. If the ~ / .bashrc file exists, the ~ / .bashrc file is usually executed. Because there is usually the following code in the ~ / .bash_profile file:

In if [- f ~ / .bashrc]; then.. / bashrcfi ~ / .bashrc, there is usually the following code: if [- f / etc/bashrc]; then. / bashrcfi

So, ~ / .bashrc invokes the / etc/bashrc file. Finally, when you exit shell, the ~ / .bash_logout file is also executed.

The order of execution is / etc/profile-> (~ / .bash_profile | ~ / .bash_login | ~ / .profile)-> ~ / .bashrc-> / etc/bashrc-> ~ / .bash_logout

Third, the method of setting environmental variables

From the above analysis, we can see that:

/ etc/profile global, set up with the system startup [setting this file is a way to do it once and for all]

/ root/.profile and / home/myname/.profile are valid only for the current window.

/ root/.bashrc and / home/yourname/.bashrc start up with the system, set the user's environment variables [usually set this file]

To configure the environment variable of Ubuntu, you need to find an appropriate file to operate in these configuration files. If you want to add a path to $PATH, you can add it in the following ways:

1. Console Central:

$PATH= "$PATH:/my_new_path" (close shell and restore PATH)

two。 Modify the profile file:

$sudo gedit / etc/profile

Add to it:

ExportPATH= "$PATH:/my_new_path"

3. Modify the .bashrc file:

$sudo gedit / root/.bashrc

Add to it:

Export PATH= "$PATH:/my_new_path"

The latter two methods generally need to log out of the system again to take effect. Finally, you can test them with the echo command:

$echo $PATH

The output is already a new path.

For example, if you want to add the current path to the environment variable, you can do this:

$PATH = "$PATH:."

In this way, you don't have to enter. / when you run the shell script you wrote.

Thank you for your reading, the above is the content of "what is the Ubuntu Linux system environment variable configuration file". After the study of this article, I believe you have a deeper understanding of what the Ubuntu Linux system environment variable configuration file has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report