Ubuntu environment variable Detailed profile environment

Source: Internet
Author: User
Tags i18n locale readline regular expression switches ssh egrep
There are several files in Ubuntu that can set environment variables

/etc/profile: The first file that is used when the operating system customizes the user environment at logon, which sets the environment information for each user of the system, which is executed when the user logs on for the first time.

/etc/environment: The second file used by the operating system at logon, the system sets environment variables for environment files before reading your own profile

~/.profile: The third file that is used to log on is the. profile file, which each user can use to enter shell information that is specific to their own use, which is executed only once when the user logs on. By default, he sets some environment variables, Executes the user's. bashrc file.

/ETC/BASHRC: Executes this file for each user running the bash shell. When the bash shell is opened, the file is read.

~/.BASHRC: This file contains bash information dedicated to your bash shell, which is read when you log in and every time you open a new shell.

There are three ways to set environment variables generally:
First, temporary settings
Export Path=/home/yan/share/usr/local/arm/3.4.1/bin: $PATH

Second, the current user's global settings
Open ~/.BASHRC, add line:
Export Path=/home/yan/share/usr/local/arm/3.4.1/bin: $PATH

Make effective

source. BASHRC
Third, global settings for all users
$ vim/etc/profile
Add in:
Export Path=/home/yan/share/usr/local/arm/3.4.1/bin: $PATH
Make effective
Source Profile

Test echo $PATH
######################################################################################
The process of loading profile and BASHRC after user login is as follows:


1)/etc/profile-------->/etc/profile.d/*.sh
2) $HOME/.bash_profile--------> $HOME/.BASHRC---------->/ETC/BASHRC


Description
Bash executes the/etc/profile script first, and the/etc/profile script executes the/etc/profile.d/*.sh in turn first
Bash then executes the. bash_profile script in the user's home directory, and the. Bash_profile script executes the. BASHRC script under the user's home directory,
and the. BASHRC script executes the/ETC/BASHRC script


At this point, all the environment variables and initialization settings have been loaded and completed.
Bash then calls Terminfo and INPUTRC to complete the terminal properties and keyboard mapping settings.


Where path is a variable that is specifically explained:
If the superuser is logged in, path has been set to the following path before/etc/profile is executed:
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
In the case of a normal user, path is set to the following path before/etc/profile executes:
/usr/local/bin:/bin:/usr/bin


Note here: When the user switches and loads variables, such as Su-, then, if the user switches themselves, such as the root user and then switch with Su-root, the loaded path and the above is not the same.
To be exact, it is not always the same. So, in the/etc/profile script, the following configuration is done:
If [' id-u ' = 0]; Then
Pathmunge/sbin
Pathmunge/usr/sbin
Pathmunge/usr/local/sbin
Fi


If the superuser is logged in, in the/etc/profile.d/krb5.sh script, add/usr/kerberos/sbin:/usr/kerberos/bin to the front of the path variable search route.
If you are a regular user, in the/etc/profile.d/krb5.sh script, add/usr/kerberos/bin to the front of the path variable search route.


In the/etc/profile script, the/usr/x11r6/bin directory is added at the end of the path variable
In $home/.bash_profile, the $home/bin directory is added at the end of the path variable


As an example of root user, the final path will be this (no other custom basis)
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/x11r6/ Bin:/root/bin


Take Alice User (normal user) as an example
/usr/kerberos/bin:/usr/bin:/bin:/usr/x11r6/bin:/home/alice/bin




Here is the script parsing done:
###############################################
#strace-o su-e trace=open su-alice
#grep ^open/etc/su|grep-v Null|grep "= 3" |nl
###############################################


Open the file as follows:
1 Open ("/etc/ld.so.cache", o_rdonly) = 3
2 Open ("/lib/libcrypt.so.1", o_rdonly) = 3
3 Open ("/lib/tls/libc.so.6", o_rdonly) = 3
4 Open ("/usr/lib/locale/locale-archive", o_rdonly| O_largefile) = 3
5 Open ("/etc/nsswitch.conf", o_rdonly) = 3
6 Open ("/etc/ld.so.cache", o_rdonly) = 3
7 Open ("/lib/libnss_files.so.2", o_rdonly) = 3
8 Open ("/etc/passwd", o_rdonly) = 3
9 Open ("/etc/shadow", o_rdonly) = 3
Ten open ("/etc/group", o_rdonly) = 3
One open ("/etc/ld.so.cache", o_rdonly) = 3
Open ("/lib/libtermcap.so.2", o_rdonly) = 3
Open ("/lib/libdl.so.2", o_rdonly) = 3
+ Open ("/lib/tls/libc.so.6", o_rdonly) = 3
Open ("/dev/tty", o_rdwr| o_nonblock| O_largefile) = 3
Open ("/etc/mtab", o_rdonly) = 3
+ Open ("/proc/meminfo", o_rdonly) = 3
Open ("/etc/nsswitch.conf", o_rdonly) = 3
Open ("/etc/ld.so.cache", o_rdonly) = 3
Open ("/lib/libnss_files.so.2", o_rdonly) = 3
+ Open ("/etc/passwd", o_rdonly) = 3
Open ("/etc/profile", o_rdonly| O_largefile) = 3
("/etc/profile.d/", o_rdonly| o_nonblock| o_largefile| o_directory) = 3
Open ("/etc/profile.d/colorls.sh", o_rdonly| O_largefile) = 3
(".", o_rdonly| o_nonblock| o_largefile| o_directory) = 3
+ Open ("/etc/profile.d/glib2.sh", o_rdonly| O_largefile) = 3
Open ("/etc/profile.d/gnome-ssh-askpass.sh", o_rdonly| O_largefile) = 3
Open ("/etc/profile.d/krb5.sh", o_rdonly| O_largefile) = 3
Open ("/etc/profile.d/lang.sh", o_rdonly| O_largefile) = 3
Open ("/etc/sysconfig/i18n", o_rdonly| O_largefile) = 3
Open ("/usr/lib/locale/locale-archive", o_rdonly| O_largefile) = 3
Open ("/usr/lib/gconv/gconv-modules.cache", o_rdonly) = 3
Open ("/etc/profile.d/less.sh", o_rdonly| O_largefile) = 3
/etc/profile.d/qt.sh Open ("o_rdonly|", O_largefile) = 3
Open ("/etc/profile.d/vim.sh", o_rdonly| O_largefile) = 3
Open ("/etc/profile.d/which-2.sh", o_rdonly| O_largefile) = 3
PNS Open ("/ceno/product/imeg/etc/profile", o_rdonly| O_largefile) = 3
/home/alice/.bash_profile Open ("o_rdonly|", O_largefile) = 3
/HOME/ALICE/.BASHRC Open ("o_rdonly|", O_largefile) = 3
("/ETC/BASHRC", o_rdonly| O_largefile) = 3
Open ("/home/alice/.bash_history", o_rdonly| O_largefile) = 3
("/home/alice/.bash_history", o_rdonly| O_largefile) = 3
Open ("/etc/termcap", o_rdonly) = 3
o_rdonly| Open ("/etc/inputrc", O_largefile) = 3
(".", o_rdonly| o_nonblock| o_largefile| o_directory) = 3
Open ("/home/alice/.bash_logout", o_rdonly| O_largefile) = 3
Open ("/home/alice/.bash_history", o_wronly| o_append| O_largefile) = 3
/home/alice/.bash_history Open ("o_rdonly|", O_largefile) = 3




########################################################
The first part:
From 1-21 rows, it is essential to open the dynamic-link library file and the authentication file.
########################################################




########################################################
Part II:
The 22nd line is to open the/etc/profile file, as follows:
########################################################
#/etc/profile


# System wide environment and startup programs, for login setup
# Functions and aliases Go IN/ETC/BASHRC


########################################################
#定义pathmunge函数
#echo $PATH | /bin/egrep-q "(^|:) $ ($|:) " is to filter the executable directory in $path, if egrep to a batch, return true but reverse
#也就是不增加执行目录到 $PATH, the option-Q disables all output to standard output, regardless of the matching line. If the input line is selected, exit with 0 status
#这里由 | More than one regular expression separated by a line break matches a string that matches any one regular expression
Grouping #一个正则表达式可以被包括在 "()" (brackets)
########################################################


Pathmunge () {
if! echo $PATH | /bin/egrep-q "(^|:) $ ($|:) "; then
If ["$" = "after"]; Then
Path= $PATH: $
Else
Path=$1: $PATH
Fi
Fi
}


###########################################
#如果uid为0的用户, the $path variable is added to the/sbin/,/usr/sbin,/usr/local/sbin three directory-Q "(^|:) ($|:)"
#因为调用pathmunge函数, the After parameter is not added, so the above three directories are added to the front of the $path variable.
###########################################
# Path Manipulation
If [' id-u ' = 0]; Then
Pathmunge/sbin
Pathmunge/usr/sbin
Pathmunge/usr/local/sbin
Fi






##########################################
#调用pathmunge函数, add/usr/x11r6/bin behind the $path
##########################################
Pathmunge/usr/x11r6/bin after


Unset Pathmunge


##########################################
#ulimit设定-S for soft control,-C for the core file size, here is no limit
##########################################
# No core Files by default
Ulimit-s-C 0 >/dev/null 2>&1


##########################################
#id-un is the current user name for the printout, for example: root
#定义了LOGNAME变量和MAIL变量, there will be programs that use these variables
##########################################
User= "' Id-un '"
Logname= $USER
Mail= "/var/spool/mail/$USER"




##########################################
#通过/bin/hostname Get host name
#定义history的记录数为1000
##########################################
Hostname= '/bin/hostname '
histsize=1000




##########################################
#如果没有定义 $INPUTRC and no $home/.inputrc file exists
#定义变量INPUTRC的值为/ETC/INPUTRC
##########################################
If [-Z] $INPUTRC "-A!-F" $HOME/.inputrc "]; Then
Inputrc=/etc/inputrc
Fi


Export Remote_java_debug=on
Export PATH USER LOGNAME MAIL HOSTNAME histsize INPUTRC


##########################################
#执行 all scripts under/etc/profile.d/,-R is to confirm that they are readable
##########################################
For I in/etc/profile.d/*.sh; Do
If [-R "$i"]; Then
. $i
Fi
Done


unset I
. /ceno/product/imeg/etc/profile


Export ps1= "\[\e[32;1m\][\u@\h]\[\e[33;1m\]:\[\e[31;1m\]\w>\\$ \[\e[0m\]"






########################################################
Part III:
Rows from 24 to 36 are all scripts executed under/etc/profile.d/, and this execution is defined in/etc/profile.
See script analysis in the previous/etc/profile.


Here is a brief description of the script under/etc/profile.d/, which mainly sets the environment variables (e.g. PATH), alias, etc.
/etc/profile.d/colorls.sh: Extract the/etc/dir_colors, and set it with Dircolors, and finally define some of the LS alias
/etc/profile.d/glib2.sh: Set G_broken_filenames=1
/etc/profile.d/gnome-ssh-askpass.sh: Set Ssh_askpass=/usr/libexec/openssh/gnome-ssh-askpass
/etc/profile.d/krb5.sh: Add/usr/kerberos/bin or/usr/kerberos/sbin to the PATH variable
/etc/profile.d/lang.sh: Setting the locale will first load the environment variable (lang,supported,sysfont) in the/etc/sysconfig/i18n into the shell,
According to the above variables to define the language environment to support a subset, and finally set the terminal according to Lang
/etc/profile.d/less.sh: Set lessopen= "|/usr/bin/lesspipe.sh%s", langvar= $LANG
/etc/profile.d/qt.sh: Set qtdir= "/usr/lib/qt-3.1"
/etc/profile.d/vim.sh: Set alias Vi=vim
/etc/profile.d/which-2.sh: Set alias which= ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde '
########################################################






########################################################
Part IV:
Line 37th Open ("/ceno/product/imeg/etc/profile", o_rdonly| O_largefile) = 3, where the user's own environment setting script is loaded.
########################################################






########################################################
Part V:
Line 38th open ("/home/alice/.bash_profile", o_rdonly| O_largefile) = 3
Line 39th Open ("/HOME/ALICE/.BASHRC", o_rdonly| O_largefile) = 3
Line 40th Open ("/ETC/BASHRC", o_rdonly| O_largefile) = 3




First step: Bash opens the/home/alice/.bash_profile file,
Step two:. bash_profile file again to determine if there is no/HOME/ALICE/.BASHRC if there is a load. bashrc file
Step three: Finally load the/ETC/BASHRC file through the. bashrc file
########################################################






########################################################
Part VI:
In line 41 open ("/home/alice/.bash_history", o_rdonly| O_largefile) = 3
In line 42 open ("/home/alice/.bash_history", o_rdonly| O_largefile) = 3
In line 43 open ("/etc/termcap", o_rdonly) = 3
In line 44 open ("/etc/inputrc", o_rdonly| O_largefile) = 3


The first step opens the. bash_history File Preparation Record command
Step two open the termcap file
The Terminfo database is used to define the properties and functions of the terminal and printer, including the number of rows and columns for each device (for example, terminal and printer) and the properties of the text to be sent to the device
Step three, open inputrc.
The Inputrc file handles the keyboard mapping for a specific situation, the file is Readline as a startup file, and Readline is the input-related library used by Bash and most other shells
########################################################






########################################################
Part VII:
Line 46th Open ("/home/alice/.bash_logout", o_rdonly| O_largefile) = 3
Line 47th Open ("/home/alice/.bash_history", o_wronly| o_append| O_largefile) = 3
Line 48th Open ("/home/alice/.bash_history", o_rdonly| O_largefile) = 3


This is the performance of the user exiting with logout or exit. If you close the terminal directly, the. Bash_logout and writeback. bash_history files are not executed.
The. Bash_logout script defaults to call clear to clear the screen

########################################################

Reprint website: http://blog.sina.com.cn/s/blog_4b2169480100sklr.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.