Magazine

User Initialization Scripts in Linux

Posted on the 07 May 2021 by Satish Kumar @satish_kumar86

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 theprofile.dfolder 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.bashrcscript 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 theCtrl+Alt+Tkey 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 theCLIprompt for entering the commands to execute.

Whenever we create an interactive Bash Terminal, shell scripts from/etc/profileand similar are not called, only the~/.bashrcscript 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.bashrcscript from the home folder of the user.

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

  • The.bashrcscript is setting the prompt
  • It initializes the environmental variables,HISTCONTROL,HISTSIZE, andHISTFILESIZE
  • It customizes the output of thelesscommand
  • It creates various alias commands such asgrep,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.bashrcscript 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*.dfolder scripts, or they may have to modify the/etc/rc.localscript.

If the administrator wants to modify the environment for all users, then they will have to modify the/etc/profileand/etc/bash_bashrcscripts.

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_loginscripts, should be modified.

If the user wants to customize onlytheinteractive shell Terminal environment, then they will have to customize the$HOME/.bashrcscript.

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


Back to Featured Articles on Logo Paperblog