User initialization scripts in Linux

So far, we have seen different scripts that initialize the operating system prior to a user login. Once the basic operating system is initialized, the user login process starts. This process is explained in the following topics.

System-wide setting scripts

In the /etc/ folder, the following files are related to the user level initialization:

  • /etc/profile: A few distributions will have an additional folder called /etc/profile.d/. All the scripts from the profile.d folder will be executed.
  • /etc/bash.bashrc.

The preceding scripts are called by every user, including root and normal users. Initially, the /etc/profile script will be called. This script initializes system-wide environment settings. A few distributions will have the /etc/profile.d/ folder. SUSE Linux has an additional /etc/profile.local script. The scripts in this folder will also be called. Then, the /etc/bash.bashrc script will be executed.

User level settings – default files

Scripts in the /etc/ folder will be called for all users. Particular, user-specific initialization scripts are located in the HOME folder of each user. These are as follows:

  • $HOME/.bash_profile: This contains user-specific bash environment default settings. This script is called during the login process.
  • $HOME/.bash_login: This contains the second user environment initialization script called during the login process.
  • $HOME/.profile: If present, this script internally calls the .bashrc script file.
  • $HOME/.bashrc: This is an interactive shell or terminal initialization script.

All the preceding script names start with a dot. These are hidden files. We will need to give the ls -a command to view these files.

  • Non-login shells

Whenever we create a new shell Terminal, such as when we press the Ctrl + Alt + T key combination, or we start a Terminal from the applications tab, then the Terminal that is created is called the interactive shell Terminal. We use this Terminal to interact with the operating system. This is not the login shell, which is created during the boot-up process, but this is an interactive shell Terminal that gives us the CLI prompt for entering the commands to execute.

Whenever we create an interactive Bash Terminal, shell scripts from /etc/profile and similar are not called, only the ~/.bashrc script is called. This happens every time we create a new interactive shell terminal. If we want environment customization for every newly created interactive shell Terminal, we need to customize the .bashrc script from the home folder of the user.

If you check the content of $HOME/.bashrc, you will observe the following:

  • The .bashrc script is setting the prompt
  • It initializes the environmental variables, HISTCONTROL, HISTSIZE, and HISTFILESIZE
  • It customizes the output of the less command
  • It creates various alias commands such as grep, fgrep, egrep, ll, la, l, and similar

If we customize .bashrc, such as adding new alias commands or declaring a new function or environment variables, then we should execute .bashrc for it to take effect. The following are two ways to run the .bashrc script so that the environment of the current shell will also be updated as per the customization done in the .bashrc script:

$ source .bashrc$ . .bashrc

Please note the usage of .(dot) two occasions, the first time for the command .(dot) and the second time in the script name, which we want to call.

With these two techniques, the child shell is not created but the .bashrc script runs in the current shell environment. Therefore, all updated or newly created environment variables become part of the current shell environment.

Every user’s home folder has one more script called .bash_logout. This script is called or executed when the user exits from the login shell.

If the system user is an embedded system developer, and is interested in adding or modifying the device’s driver-related commands, then they will have to make changes in the /etc/rc*.d folder scripts, or they may have to modify the /etc/rc.local script.

If the administrator wants to modify the environment for all users, then they will have to modify the /etc/profile and /etc/bash_bashrc scripts.

If we want to customize the environment related to a particular user, then the scripts located in the user’s home folder, such as $HOME/.profile, $HOME/bash_profile, and $HOME/bash_login scripts, should be modified.

If the user wants to customize only the interactive shell Terminal environment, then they will have to customize the $HOME/.bashrc script.

If you are working in system administration, then I would suggest you learn about the /etc/fstab file and its editing. This file is used for configuring mount points and how file systems are mounted.

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur


Submit a Comment

Your email address will not be published. Required fields are marked *

four × four =