Linux User Profiles: .bashrc, .bash_profile, .profile, /etc/profile
Adjusting the User Work Environment in Linux
During the user logon process, when the bash shell starts, it executes the script /etc/profile. This script can be customized and differs in each Linux distribution. Its function is to configure some environment variables and tune the system for users.
To adjust the User Profile in Linux, bash reads the configuration from several files. The content of these files may vary in each distribution, and not every distribution uses all of them, but the general idea remains.
Bash also looks for the file /etc/bash.bashrc which also has a similar function to profile, but bashrc is executed every time bash is run.
Each user can also create their initialization scripts to be executed during logon. These files need to be located in the users' home directory with the names:
- .profile
- .bash_profile
- .bash_login
- .bashrc
- .bash_logout
The dot "." before the file name gives it the hidden attribute, only being listed with the "ls –lga" command.
Bash Profile Files
Bash enables functions, variables, and aliases to be saved in some files so they can be loaded again when the system is started again, or a new bash execution is done.
The files read by bash are:
/etc/profile
Global Profile configuration file for all users. Defines global variables and is executed during the user authentication process. This script also usually loads with the source command the files contained in the directory /etc/profile.d.
/etc/profile.d
This directory contains one or more scripts that are loaded by /etc/profile.
$ ls -l /etc/profile.d-rw-r--r-- 1 root root 1606 jul 31 2018 colorls.sh-rw-r--r-- 1 root root 2703 ago 2 2018 lang.sh-rw-r--r-- 1 root root 121 jul 31 2018 less.sh-rw-r--r-- 1 root root 248 jul 17 14:46 vim.sh
/etc/bashrc or /etc/bash.bashrc
Global Profile configuration file, which defines important variables and is executed every time Bash is loaded. In some distributions it appears as /etc/bashrc and in others as /etc/bash.bashrc.
~/.bash_profile
Individual Profile file for each user that is executed immediately after /etc/profile. Its content is read at each Bash execution and each user has their own in the HOME directory.
~/.bash_login
If the ~/.bash_profile file does not exist, it is executed right after the logon process. Each user has their own;
~/.profile
If the .bash_profile and .bash_login files do not exist, it is executed right after logon. Each user has their own;
~/.bashrc
It is automatically executed when the Bash process is started. Each user has their own;
~/.bash_logout
It is executed during the logout process;
Remembering that "~/" indicates the HOME directory of the logged in user.
You should look at these files and analyze their content. Each distribution may vary the content of these scripts. You may even notice that one script calls another.
It is very important to know the function and when each file is executed. In general, keep in mind:
- The files that are in the /etc directory are global, and apply to all users;
- The files that are in the HOME directory are individual for each user;
- The files that have profile in the name are loaded during the login process, only once;
- The files that have bash in the name are loaded every time Bash is executed.
Another important detail is that these files are read and executed in the order described above: first the Profiles, then the Bash. Not all distributions use all of these files.
The figure below illustrates the order of the loading process of these scripts:
Login Scripts Reading Order |
Notice that the nodes in yellow are global scripts, that apply to all users. The ~/.bashrc script is loaded every time Bash is executed. And the ~/.bash_logout script every time Bash ends.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.
Login Scripts Reading Order