Understanding user profile files in Linux - .bashrc, .bash_profile, /etc/profile, /etc/bashrc

User profiles

Individual users have personal habits and preferences for their Linux work environment, and so a variety of profile file configurations are available to help them customize their experience. These customizations are referred to as profiles. An initial profile is created when the user account is created. There are many files and directories from which information is initially pulled to create a standard profile.

.bashrc file

The .bashrc file enables customization of the user’s own environment. The file is stored in a user’s home directory. Because the .bashrc file is unique to each user, it can be configured to a user’s own specific needs or preferences.

A good example of the use of the .bashrc file is the use of aliases. Users can specify their own abbreviated commands without impacting the experience of any other user on the system. Another common configuration within .bashrc is environment variables. Users can also use the file to customize the command prompt to provide the information they want.

Example Configurations

Other than creating aliases, the .bashrc file is often configured to set the default directory and file permissions for the user. Also, the default command prompt can be altered to provide more information. Most distributions place the user name, system hostname, and current directory in the prompt, but that can be changed.

bashrc file example in Linux

.bash_profile file

The .bash_profile file provides shell configuration for the initial login environment. This differs from the .bashrc file, which provides settings for all of the user’s interactive shells. The .bash_profile file is only read with the first login, while the .bashrc is read with all subsequent logins.

A default .bash_profile can be provided to new users via the /etc/skel directory.

bash_profile example in Linux

Global User Profiles

An administrator may find it desirable to define system-wide settings or to configure initial settings for users. The files and directories that follow give the administrator the flexibility to make a variety of configurations, which may be later customized by a user for their own specific needs.

The /etc/skel/ Directory

The contents of the /etc/skel/ directory are automatically copied into the home directories of new users. Administrators can pre-populate the /etc/skel/ directory with configuration files or other content. When the useradd command is run, the /etc/skel/ directory’s contents are copied to the new user’s home directory, immediately giving them the configurations they might need.

/etc/profile file

The /etc/profile file provides system-wide environment variables. This may be more effective for administrators to configure if there are settings that apply to all users.

During the initial login process for a user, the system reads the /etc/profile file first for Bash shell configurations, and then any user-specific Bash customizations are pulled from the .profile file located in the user’s home directory. The .profile file runs each time a new shell is started, whereas /etc/profile is only run at login. This approach enables administrators to define global shell settings, but still allow user-specific customizations.

Example

An example of a .profile is as follows:

PATH=$PATH:$HOME/bin:/scripts
MAIL=/var/mail/$LOGNAME
EDITOR=emacs
export PATH MAIL EDITOR

The first line defines the paths of executable files; the second line defines the path for where incoming email messages are stored; and the third line defines the default text editor. The last line actually ensures these variables are implemented in the environment.

/etc/profile.d/ Directory

The /etc/profile.d/ directory serves as a storage location for scripts administrators may use to set additional system-wide variables. It is recommended you set the environment variables via scripts contained in /etc/profile.d/ rather than editing the /etc/profile file directly.

/etc/bashrc File

The /etc/bashrc file provides system-wide Bash settings. This is a little different than /etc/profile, which is used for variables.