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

How to analyze profile and bashrc

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to analyze profile and bashrc. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

/ etc/profile: this file sets the environment information for each user of the system and is executed when the user logs in for the first time. And collect shell settings from the configuration file in the / etc/profile.d directory.

/ etc/bashrc: execute this file for each user running bash shell. When bash shell is opened, the file is read.

~ / .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: this file contains bash information specific to your bash shell, which is read when you log in and every time you open a new shell.

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

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

~ / .bash_profile is interactive and login enters bash to run.

~ / .bashrc is entered into bash by interactive non-login.

Usually the settings are roughly the same, so the former usually calls the latter.

* .bash _ profile is executed each time bash starts as login shell.

There are mainly (as far as I know) the following situations:

A) shell launched by default each time you log in to the server

B) shell entered at "su-l [USER]"

C) shell entered by "bash-login"

* .bashrc is executed every time bash is started as a normal interactive shell (interactive shell)

Common ones are:

A) shell entered by "su [USER]"

B) run the "bash" command directly to enter the shell.

* * Note

1. Bash started by "#! / usr/bin/bash" in the shell script does not execute .bashrc. Because the bash here is not interactive shell.

2. Bash does not execute .bashrc when it starts as login shell (login bash). Although the shell is also an interactive shell, it is not an ordinary shell.

* .bashrc is usually called in .bash _ profile

Although .bashrc is not automatically executed when login bash starts, it is conventionally explicitly called in .bash _ profile.

,-

| | if [- f ~ / .bashrc]; then |

|. ~ / .bashrc

| | fi |

`--

.bash _ profile displays as follows:

# .bash _ profile

# Get the aliases and functions

If [- f ~ / .bashrc]; then

. ~ / .bashrc

Fi

# User specific environment and startup programs

PATH=.:$PATH:$HOME/bin

BASH_ENV=$HOME/.bashrc

USERNAME= "root"

Export USERNAME BASH_ENV PATH

The process for loading profile and bashrc after a user logs in is as follows:

1) / etc/profile--- > / etc/profile.d/*.sh

2) $HOME/.bash_profile--- > $HOME/.bashrc--- > / etc/bashrc

Description:

Bash first executes / etc/profile script, / etc/profile script executes / etc/profile.d/*.sh first, then bash executes .bashrc script in user's home directory, .bashrc script executes .bashrc script in user's home directory, and .bashrc script executes / etc/bashrc script, so all environment variables and initialization settings have been loaded.

Bash then calls terminfo and inputrc to complete the terminal properties and keyboard mapping settings.

The variable PATH has a special description:

In the case of a superuser login, PATH has set the following path before executing / etc/profile:

/ usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

For normal users, PATH sets the following path before / etc/profile executes:

/ usr/local/bin:/bin:/usr/bin

It should be noted here that when the user switches and loads variables, such as su -, if the user switches himself, such as root and then uses su-root to switch, the loaded PATH is different from the above.

To be exact, it is not always the same. So, in the / etc/profile script, the configuration is as follows:

If [`id-u` = 0]; then

Pathmunge / sbin

Pathmunge / usr/sbin

Pathmunge / usr/local/sbin

Fi

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

For normal user login, in the / etc/profile.d/krb5.sh script, add / usr/kerberos/bin to the front of the search path of the PATH variable

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

Take root users as an example, the final PATH will look like this (without other customizations):

/ 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 users (ordinary users) 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

# # #

The files opened are 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

10 open ("/ etc/group", O_RDONLY) = 3

11 open ("/ etc/ld.so.cache", O_RDONLY) = 3

12 open ("/ lib/libtermcap.so.2", O_RDONLY) = 3

13 open ("/ lib/libdl.so.2", O_RDONLY) = 3

14 open ("/ lib/tls/libc.so.6", O_RDONLY) = 3

15 open ("/ dev/tty", O_RDWR | O_NONBLOCK | O_LARGEFILE) = 3

16 open ("/ etc/mtab", O_RDONLY) = 3

17 open ("/ proc/meminfo", O_RDONLY) = 3

18 open ("/ etc/nsswitch.conf", O_RDONLY) = 3

19 open ("/ etc/ld.so.cache", O_RDONLY) = 3

20 open ("/ lib/libnss_files.so.2", O_RDONLY) = 3

21 open ("/ etc/passwd", O_RDONLY) = 3

22 open ("/ etc/profile", O_RDONLY | O_LARGEFILE) = 3

23 open ("/ etc/profile.d/", O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY) = 3

24 open ("/ etc/profile.d/colorls.sh", O_RDONLY | O_LARGEFILE) = 3

25 open (".", O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY) = 3

26 open ("/ etc/profile.d/glib2.sh", O_RDONLY | O_LARGEFILE) = 3

27 open ("/ etc/profile.d/gnome-ssh-askpass.sh", O_RDONLY | O_LARGEFILE) = 3

28 open ("/ etc/profile.d/krb5.sh", O_RDONLY | O_LARGEFILE) = 3

29 open ("/ etc/profile.d/lang.sh", O_RDONLY | O_LARGEFILE) = 3

30 open ("/ etc/sysconfig/i18n", O_RDONLY | O_LARGEFILE) = 3

31 open ("/ usr/lib/locale/locale-archive", O_RDONLY | O_LARGEFILE) = 3

32 open ("/ usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3

33 open ("/ etc/profile.d/less.sh", O_RDONLY | O_LARGEFILE) = 3

34 open ("/ etc/profile.d/qt.sh", O_RDONLY | O_LARGEFILE) = 3

35 open ("/ etc/profile.d/vim.sh", O_RDONLY | O_LARGEFILE) = 3

36 open ("/ etc/profile.d/which-2.sh", O_RDONLY | O_LARGEFILE) = 3

37 open ("/ ceno/product/imeg/etc/profile", O_RDONLY | O_LARGEFILE) = 3

38 open ("/ home/alice/.bash_profile", O_RDONLY | O_LARGEFILE) = 3

39 open ("/ home/alice/.bashrc", O_RDONLY | O_LARGEFILE) = 3

40 open ("/ etc/bashrc", O_RDONLY | O_LARGEFILE) = 3

41 open ("/ home/alice/.bash_history", O_RDONLY | O_LARGEFILE) = 3

42 open ("/ home/alice/.bash_history", O_RDONLY | O_LARGEFILE) = 3

43 open ("/ etc/termcap", O_RDONLY) = 3

44 open ("/ etc/inputrc", O_RDONLY | O_LARGEFILE) = 3

45 open (., O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY) = 3

46 open ("/ home/alice/.bash_logout", O_RDONLY | O_LARGEFILE) = 3

47 open ("/ home/alice/.bash_history", O_WRONLY | O_APPEND | O_LARGEFILE) = 3

48 open ("/ home/alice/.bash_history", O_RDONLY | O_LARGEFILE) = 3

# # #

Part one:

Lines 1-21 are basically opening dynamic link library files and authentication files.

The second part:

Line 22 opens the / etc/profile file, as follows:

# # #

# / etc/profile

# System wide environment and startup programs, for login setup

# Functions and aliases go in / etc/bashrc

# # #

# define pathmunge function

# echo $PATH | / bin/egrep-Q "(^ |:) $1 ($|:)" is used to filter the executable directory in $PATH. If egrep is returned to the batch item, the returned value can be reversed.

# that is, without adding the execution directory to $PATH, the option-Q forbids all output to standard output, regardless of matching lines. If the input line is selected, exit at 0

# here multiple regular expressions separated by | (vertical bar) or newline characters will match the string that matches any one of the regular expressions

# A regular expression can be grouped in "()" (parentheses)

Pathmunge () {

If! Echo $PATH | / bin/egrep-Q "(^ |:) $1 ($|:)"; then

If ["$2" = "after"]; then

PATH=$PATH:$1

Else

PATH=$1:$PATH

Fi

Fi

}

# if the user whose uid is 0, / sbin/,/usr/sbin,/usr/local/sbin three directories will be added to the $PATH variable-Q "(^ |:) ($|:)"

# because the pathmunge function is called without the after parameter, 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

# call the pathmunge function and add / usr/X11R6/bin after $PATH

Pathmunge / usr/X11R6/bin after

Unset pathmunge

# ulimit sets-S to soft control and-c to core file file size. There is no limit here.

# No core files by default

Ulimit-S-c 0 > / dev/null 2 > & 1

# id-un is the current user name of the printout, for example: root

# defines LOGNAME variables and MAIL variables, which will be used by programs

USER= "`id-un`"

LOGNAME=$USER

MAIL= "/ var/spool/mail/$USER"

# get the hostname through / bin/hostname

# define the number of records in history as 1000

HOSTNAME= `/ bin/ hostname`

HISTSIZE=1000

# if $INPUTRC is not defined and the $HOME/.inputrc file does not exist

# define the value of the variable INPUTRC as / 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

# execute all scripts under / etc/profile.d/,-r is to make sure 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 [32trans1m\] [\ u@\ h]\ [\ e [33trans1m\]:\ [\ e [31ET1m\]\ w >\ $\ [\ e [\ e [0m]"

The third part:

Lines 24 to 36 execute all scripts under / etc/profile.d/, which is defined in / etc/profile.

See script analysis above in / etc/profile.

The following is a brief description of the script under / etc/profile.d/, mainly setting environment variables (e.g. PATH), alias, etc.

/ etc/profile.d/colorls.sh: extract / etc/DIR_COLORS, set it with dircolors, and finally define some alias of ls

/ 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: to set the locale, the environment variable (LANG,SUPPORTED,SYSFONT) from / etc/sysconfig/i18n will be loaded into shell first

Redefine the locale support subset according to the above variables, 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 37 open ("/ ceno/product/imeg/etc/profile", O_RDONLY | O_LARGEFILE) = 3, where the user's own environment setting script is loaded.

Part V:

Line 38 open ("/ home/alice/.bash_profile", O_RDONLY | O_LARGEFILE) = 3

Line 39 open ("/ home/alice/.bashrc", O_RDONLY | O_LARGEFILE) = 3

Line 40 open ("/ etc/bashrc", O_RDONLY | O_LARGEFILE) = 3

Step 1: bash Open / home/alice/.bash_profile File

Step 2:. Bash_profile file to determine whether there is / home/alice/.bashrc, if there is a load .bashrc file

Step 3: finally load the / etc/bashrc file through the .bashrc file

Part VI:

On line 41, open ("/ home/alice/.bash_history", O_RDONLY | O_LARGEFILE) = 3

On line 42, open ("/ home/alice/.bash_history", O_RDONLY | O_LARGEFILE) = 3

At line 43 open ("/ etc/termcap", O_RDONLY) = 3

On line 44, open ("/ etc/inputrc", O_RDONLY | O_LARGEFILE) = 3

The first step is to open the .bash _ history file to prepare the record command

Step 2: open the termcap file

The terminfo database is used to define the properties and functions of terminals and printers, including the number of rows and columns for each device (for example, terminals and printers) and the properties of text to be sent to that device

Step 3: open inputrc

The inputrc file, which handles keyboard mapping for specific cases, is used by Readline as the startup file, and Readline is the input-related library used by Bash and most other shell

Part VII:

Line 46 open ("/ home/alice/.bash_logout", O_RDONLY | O_LARGEFILE) = 3

Line 47 open ("/ home/alice/.bash_history", O_WRONLY | O_APPEND | O_LARGEFILE) = 3

Line 48 open ("/ home/alice/.bash_history", O_RDONLY | O_LARGEFILE) = 3

Here is the user's exit with logout or exit. If terminal is directly closed, the .bash _ logout and writeback .bash _ history files will not be executed.

The .bash _ logout script calls clear by default to clear the screen.

The above is the editor for you to share how to carry out profile and bashrc analysis, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report