This article was first published inPersonal blog, the original site link is:https://www.panyanbin.com/art…, do not forward and use without permission.

Sometimes we need to execute our own scripts when a Linux user logs in, such as giving a friendly interactive output prompt when logging in. To do this, it is important to understand the order in which Linux executes its internal shell when the user logs in, so that the custom scripts can be put in place to execute.

If you don’t want to see the order in which the built-in scripts execute when a Linux user logs in and just want to see the configuration action, you can go straight to the second paragraph: Custom Scripts Performing Configuration Actions.

Linux users log in to the built-in script execution order

The order in which shell scripts are executed when users log in is shown in the figure below:

Note: The test machine is CentOS, so it will be executed
~/.bash_profile, different operating systems may have different file names (
~/.bash_profile,
~/.bash_login,
~/.profile), the overall logic execution is basically unchanged.

The following is a brief overview of the process.

/etc/profile

Each user (including root) checks the global file /etc/profile before logging in. If this file exists, use source or. Command to execute the file.

Why use the source command? Because I just logged into Linux system, the relevant bash command has not been injected into the shell, so I cannot execute the script through the corresponding bash, but can only use the built-in command to execute it.

Let’s cat the /etc/profile file and just show the relevant information below. More information can be found on your own.

# /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to  your environment, as this # will prevent the need for merging in future updates. ... . for i in /etc/profile.d/*.sh ; do if [ -r "$i" ]; then if [ "${-#*i}" != "$-" ]; then . "$i" else . "$i" >/dev/null fi fi done ...

To view the content of the comment:

  • This is a system-wide environment and launcher, where functions for login Settings and command aliases are placed in /etc/bashrc files (such as Settings)llAlias command substitutionls -l)
  • Modifying the contents of the current file is not recommended. If custom environment variables need to be modified, the best way to do so is in the directory/etc/profile.d/Under create sh script file, the file name of the script can be customized, the content of the script is mainly custom environment variables.

If you don’t know the shell script language, you can understand the general meaning of the code. It is mainly to traverse the sh script file in /etc/profile.d directory, and then use dot (.) on the script file that has readable permissions. Command to execute the script.

At this point, you can see that the purpose of this file is to define some environment variables for all users at the system level, which can be accessed in the shell after the user has logged in. And these environment variables are recommended to be defined in any script sh file in the /etc/profile.d/ directory.

After creating the global environment variable, you only need to re-log into the shell to access the environment variable. If you do not want to log out and re-log in, but want to make its environment variable effective in the shell you are currently logging in, you can use the command source command to make it effective.

source /etc/profile.d/xxx.sh

Let’s take a look at the scripts in /etc/profile.d:

Note: The list of script files in this directory varies according to different systems. The test system for Lao Pan is CentOS.

Next, we inject the bash command into the shell, allowing us to invoke the shell script with the /bin/bash command.

~/.bash_profile

If this file does not exist on your system, check if it is a CentOS. If you can manually create one, if not, check the other file name (
~/.bash_login,
~/.profile)

Next, Linux will look under the user’s home directory to see if the.bash_profile file exists, and if so, use source or again. Command to execute the file.

The root user is at /root/.bash_profile, and the other users are at /home/user/.bash_profile

The purpose of this file is to allow the current user to customize some user-level environment variables. Environment variables defined in this file (not defined in /etc/profile) are not accessible to other users. In addition, the user’s~/.bashrcFile.

The following is the default content for this file:

This file is executed only once when the user logs in. And Linux after the execution of this file program, it will be prompted to login and enter the user command line interaction. In the currently logged-in shell, the subsequent scripts (~/.bashrc and /etc/bashrc) are executed because the code that calls those script files was written within the script content of the file.

~/.bashrc

The main purpose of the ~/.bashrc file is to define some of the current user-defined command aliases and functions.

The file is in when the user logs in~/.bash_profileIn addition, this file is read and executed every time the current user opens a new shell.

A word of caution: never comment default code on a production machine unless you know what happens when your code is commented!!

The following output is from the ~/.bashrc file on the test machine:

As you can see, in addition to being equal to some custom command aliases, there is also a call to execute the global definition script file /etc/bashrc, judging if the file is a normal file.

/etc/bashrc

From the above, we know that the /etc/bashrc file is called by the ~/.bashrc file. If the comment executes the line of code, this file will not be read and executed when the user logs in or opens a new shell.

Once again, never comment default code on a production machine unless you know what will happen if it is commented!!

The content of the file is relatively large, mainly to show the file annotation content:

# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

...

This file is used to define system-level aliases for functions and commands, but if you want to add global custom functions and commands, do not modify this file. Instead, write it to /etc/profile.d/xxxx.sh in the same custom file that defines the global environment variables.

Part of the purpose of the non-interactive-login shell call is to execute the script defined in /etc/profile.d/ to inject the environment variables, functions, and commands that define the global context into the current shell. Because the non-interactive login shell has no steps to read and execute /etc/profile, it only reads the execution ~/.bashrc file. This function can be known, interested can go to the detailed section, generally do not need too much attention.

Perform sequential tests

1. Add test code

To test the order in which the files are executed, I log on to the server as root and do the following, so the ~ directory is /root/ :

  • Create a test.sh file in /etc/profile.d/ and write the following code internally

    echo '/etc/profile.d/test.sh'
  • Add a few lines of test code to the top and bottom of the /root/.bash_profile file and leave the rest unprocessed

    echo '/root/.bash_profile --start' # ... Echo '/root/.bash_profile --end'
  • Add a few lines of test code to the top and bottom of the /root/.bashrc file as well and leave the rest of the code alone

    echo '/root/.bashrc --start' # ... Echo '/root/.bashrc --end'
  • Also add a few lines of test code at the top and bottom of the /etc/bashrc file and leave the rest of the code alone

    echo '/etc/bashrc --start' # ... Echo '/etc/bashrc --end'

2. Verification tests

After adding the above test output code, we will be safe and open a new TAB directly to log in. The original root will not log out in case of operation error causing login failure.

Login using root. After successful login, the following will be printed:

/etc/profile.d/test.sh
/root/.bash_profile --start
/root/.bashrc --start
/etc/bashrc --start
/etc/bashrc --end
/root/.bashrc --end
/root/.bash_profile --end

Log in as a non-root user. After successful login, the following content will be output:

/etc/profile.d/test.sh
/etc/bashrc --start
/etc/bashrc --end

This is because non-root users log in with ~/.bash_profile instead of /root/.bash_profile.

Custom scripts perform configuration operations

According to the above instructions, we can roughly determine the use of several schemes, the following to the user login after greeting as a case.

1. Scripts are executed when all users log in

Go to /etc/profile.d:

cd /etc/profile.d

Create a custom script sh file with any name, such as say-hello.sh

vim say-hello.sh

Write the content of the custom script to execute

echo "Hello, $USER. Welcome to login."

$USERThe variable is the current logged-in user retrieved in the script

You don’t normally write much logical code in this sh file, just configure some environment variables and the execution of business scripts such as

# /etc/profile.d/say-hello.sh

echo "Hello, $USER. Welcome to login."

node /usr/local/xxx/xx.js

This makes it clear that the code for the core implementation is stored in a different directory.

2. Execute the script when a specific user logs in

For a specific user, you can add the execution code for a custom script to the user’s ~/.bashrc file

Create my say-hello.sh file

# ~/say-hello.sh

echo "Hello, $USER. Welcome to login."

The file is then called in ~/.bashrc

# ~/.bashrc

# ...

bash ./say-hello.sh

If there are multiple specific users, we can add the script execution command to the./bashrc file of each user who needs to execute the script, or we can write the script in /etc/profile.d and then decide whether to execute the script based on the currently logged-in user.

summary

There are several configuration files, we generally agree:

  • Define global (used by all users) environment variables, which can be found at/etc/profile.d/The custom sh script file is defined
  • Executing global (executed by all users) command alias/script execution can be done at/etc/profile.d/Called in the custom sh script file of
  • Define or execute specific user-defined command aliases and scripts that can be used in~/.bashrcFile defined or executed
  • Defining specific user-defined environment variables can be found in~/.bash_profile(Note that on different operating systems, the filename may be different.bash_loginor.profile) define

reference

Linux on/off script execution sequence and self-start script practice

Ulimit most,