Ubuntu system environment variable configuration

 The configuration of environment variables cannot be avoided when using Ubuntu for development. Due to the strict permission management of the Linux system, the Ubuntu system has multiple environment variable configuration files. without the question of its role. This article will introduce the environment variables of Ubuntu Linux system.

1. Ubuntu Linux system environment variable configuration file

Ubuntu Linux system environment variable configuration files are divided into two types: system-level files and user-level files. The following describes the environment variable configuration files in detail.

1. System-level files :

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

/etc/bash.bashrc : The system-wide bashrc file, executed for every user running the bash shell. This file is read when the bash shell is opened.

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

2. User-level files :

~/.profile : The third file used when logging in is the .profile file. Each user can use this file to enter shell information dedicated to their own use. When the user logs in, this file is executed only once! By default Next, he sets some environment variables and executes the user's .bashrc file.

~/.bashrc : This file contains bash information specific to your bash shell and is read when logging in and every time a new shell is opened. It is not recommended to put it here, because every time a shell is opened, the file will be read once, which is not efficient in terms of efficiency.

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

~ ./bash_login: This is deprecated, these do not affect the GUI. And .bash_profile has higher priority than bash_login. When they exist, they are read when the login shell starts.

~/.bash_logout: This file is executed every time you exit the system (exit the bash shell).

~/.pam_environment : User-level environment variable setting file.

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

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

Let's do an experiment first:

先将export LANG=zh_CN加入/etc/profile ,退出系统重新登录,登录提示显示英文。将/etc/profile中的export LANG=zh_CN删除,将LNAG=zh_CN加入/etc/environment,退出系统重新登录,登录提示显示中文。 

用户环境建立的过程中总是先执行/etc/profile然后在读取/etc/environment。为什么会有如上所叙的不同呢? 

应该是先执行/etc/environment,后执行/etc/profile。 

/etc/environment是设置整个系统的环境,而/etc/profile是设置所有用户的环境,前者与登录用户无关,后者与登录用户有关。

系统应用程序的执行与用户环境可以是无关的,但与系统环境是相关的,所以当你登录时,你看到的提示信息,比如日期、时间信息的显示格式与系统环境的LANG是相关的,缺省LANG=en_US,如果系统环境LANG=zh_CN,则提示信息是中文的,否则是英文的。 

对于用户的SHELL初始化而言是先执行/etc/profile, 再读取文件/etc/environment. 

对整个系统而言是先执行/etc/environment。这样理解正确吗 

/etc/enviroment -->/etc/profile --> $HOME/.profile -->$HOME/.env (如果存在) 

/etc/profile 是所有用户的环境变量

/etc/enviroment是系统的环境变量 

登陆系统时shell读取的顺序应该是

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

原因应该是用户环境和系统环境的区别了 

如果同一个变量在用户环境(/etc/profile)和系统环境(/etc/environment) 有不同的值那应该是以用户环境为准了。 

备注:在shell中执行程序时,shell会提供一组环境变量。export可新增,修改或删除环境变量,供后续执行的程序使用。export的效力仅及于该此登陆操作。 

在登录Linux时要执行文件的过程如下:

在刚登录Linux时,首先启动/etc/profile 文件,然后再启动用户目录下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中的其中一个,执行的顺序为:~/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile文件存在的话,一般还会执行 ~/.bashrc文件。因为在 ~/.bash_profile文件中一般会有下面的代码:

 

if[ -f ~/.bashrc ] ; then
 ../bashrc
           fi
  ~/.bashrc中,一般还会有以下代码:
if[ -f /etc/bashrc ] ; then
 ./bashrc
fi

 

所以,~/.bashrc会调用/etc/bashrc文件。最后,在退出shell时,还会执行~/.bash_logout文件。 

执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc-> /etc/bashrc -> ~/.bash_logout 

三、设置环境变量的方法

由以上分析可知:

/etc/profile全局的,随系统启动设置【设置这个文件是一劳永逸的办法】

/root/.profile/home/myname/.profile只对当前窗口有效。

/root/.bashrc /home/yourname/.bashrc随系统启动,设置用户的环境变量【平时设置这个文件就可以了】

那么要配置Ubuntu的环境变量,就是在这几个配置文件中找一个合适的文件进行操作了;如想将一个路径加入到$PATH中,可以由下面这样几种添加方法:

1.控制台中:

$PATH="$PATH:/my_new_path"    (关闭shell,会还原PATH)

2.修改profile文件:

$sudo gedit /etc/profile

在里面加入:

exportPATH="$PATH:/my_new_path"

3.修改.bashrc文件:

$ sudo gedit /root/.bashrc

在里面加入:

export PATH="$PATH:/my_new_path"

后两种方法一般需要重新注销系统才能生效,最后可以通过echo命令测试一下:

$ echo $PATH

输出已经是新路径了。

举个列子,如果想把当前路径加入到环境变量中去,就可以这样做:

$  PATH ="$PATH:."

这样运行自己编写的shell脚本时就可以不输入./了

四、小结

To sum up, in Ubuntu system /etc/profile file is a global environment variable configuration file, which applies to all shells. When we log in to the Linux system, first start the /etc/profile file, and then start one of the ~/.bash_profile, ~/.bash_login or ~/.profile files in the user directory. The order of execution is the same as the above order. . If the ~/.bash_profile file exists, the ~/.bashrc file is generally executed.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324733352&siteId=291194637