Build a Better Process

How Do Linux Configuration Files Work?

Here we end the confusion about interactive vs non-interactive sessions, and login vs non-login sessions.
  1. Confusion - Break the confusion into four quadrants.
  2. Resources - Find other resources to help.
  3. Session types - Describe the four session types.
  4. Config files - List configuration files run for each case.
  5. Next: packages - Learn to install software packages.
face pic by Paul Alan Davis, CFA
Updated: February 21, 2021
Oddly this is a frequently misunderstood topic for which a lively debate exists. Keep reading.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / linux-configuration.html

An ad-free and cookie-free website.

Understand Interactive and Login Sessions in Linux


Video Tutorial

Videos can also be accessed from our Full Stack Playlist 2 on YouTube.

How do Linux configuration files work? Interactive and login sessions | Linux Tutorial for Beginners (4:58)

Code Examples and Video Script

Welcome. Today's question: Which Linux configuration files work?

I'm Paul, and you're here, so you can relate to how frustrating the Linux startup process is.

I've spent a lot of time scratching my head, and gauging by the hundreds of questions on Stack Exchange and other sites, I'm not the only confused one.

Step 1 - The Confusion of Linux Config Files

So here, I will start with what is confusing.

Where we're headed, even though we would like to ignore the startup process and configuration files, we just can't.

Here, I will point you to supplemental third-party resources.

We will break the problem into four quadrants, depicting the type of shell session which in turn dictates the order in which configuration files are run.

  Interactive Non-Interactive
Login 1 3
Non-Login 2 4


(Here are other commands we will practice with).

Following this video we'll learn how to install software packages.

Let's start by logging in to a local Linux test server sitting in my office on this clear day in California, viewing commands that we'll use throughout.

login as: paul@'s password: The programs included with the Debian GNU/Linux system are free software: the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Thu Feb 23 19:46:31 2017 from paul@fullstack:~$ whatis echo whatis less man echo (1) - display a line of text whatis (1) - display one-line manual page descriptions less (1) - opposite of more man (1) - an interface to the on-line reference manuals man (7) - macros to format man pages paul@fullstack:~$ less /etc/profile

I should mention, we're currently tackling beginner topics in Linux, and the startup process gets advanced pretty quickly, as evidenced by configuration files.

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). if [ "`id -u`" -eq 0 ]; then PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" else PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games" fi export PATH if [ "$PS1" ]; then if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then # The file bash.bashrc already sets the default PS1. # PS1='\h:\w\$ ' if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc fi else if [ "`id -u`" -eq 0 ]; then PS1='# ' else PS1='$ ' fi fi fi if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i fi /etc/profile

Interpreting these requires an understanding of if/then statements, the path, prompts and lots of shell builtin commands from tutorial 15.

paul@fullstack:~$ less /etc/bash.bashrc
# System-wide .bashrc file for interactive bash(1) shells. # To enable the settings / commands in this file for login shells as well, # this file has to be sourced in /etc/profile. # If not running interactively, don't do anything [ -z "$PS1" ] && return # check the window size after each command and, if necessary, # update the values of LINES and COLUMNS. shopt -s checkwinsize # set variable identifying the chroot you work in (used in the prompt below) if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then debian_chroot=$(cat /etc/debian_chroot) fi # set a fancy prompt (non-color, overwrite the one in /etc/profile) PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' # Commented out, don't overwrite xterm -T "title" -n "icontitle" by default. # If this is an xterm set the title to user@host:dir #case "$TERM" in #xterm*|rxvt*) # PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"' # ;; #*) # ;; #esac /etc/bash.bashrc
paul@fullstack:~$ less .bashrc
# ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for examples # If not running interactively, don't do anything case $- in *i*) ;; *) return;; esac # don't put duplicate lines or lines starting with space in the history. # See bash(1) for more options HISTCONTROL=ignoreboth # append to the history file, don't overwrite it shopt -s histappend # for setting history length see HISTSIZE and HISTFILESIZE in bash(1) HISTSIZE=1000 HISTFILESIZE=2000 # check the window size after each command and, if necessary, # update the values of LINES and COLUMNS. shopt -s checkwinsize # If set, the pattern "**" used in a pathname expansion context will # match all files and zero or more directories and subdirectories. #shopt -s globstar .bashrc

Here's my favorite, "if not running interactively, don't do anything." So, yes, confusing.

Step 2 - Find Help with Other Resources

See INVOCATION in the bash manual

As for other resources, you can access the bash man page.

paul@fullstack:~/notes$ man bash
BASH(1) General Commands Manual BASH(1) NAME bash - GNU Bourne-Again SHell SYNOPSIS bash [options] [command_string | file] COPYRIGHT Bash is Copyright (C) 1989-2013 by the Free Software Foundation, Inc. DESCRIPTION Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incorporates useful features from the Korn and C shells (ksh and csh). Bash is intended to be a conformant implementation of the Shell and Utilities por tion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash can be configured to be POSIX-conformant by default. OPTIONS All of the single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked. In addition, bash interprets the following options when it is invoked: -c If the -c option is present, then commands are read from the first non- option argument command_string. If there are arguments after the com mand_string, they are assigned to the positional parameters, starting with

Remember it is 5,000 lines long, so use /INVOCATION, and n a couple of times for the next instance, and here you'll see a full description. And at the bottom relevant files are defined.

INVOCATION A login shell is one whose first character of argument zero is a -, or one started with the --login option. An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state. The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be read, bash reports an error. Tildes are expanded in filenames as described below under Tilde Expansion in the EXPANSION section. When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and exe cutes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior. When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists. When an interactive shell that is not a login shell is started, bash reads and exe cutes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.
The GNU Bash Manual

Also, at the link provided in the Description on YouTube, find the GNU Bash Manual in a variety of formats, explaining interactive and login shells.

Step 3 - Linux Session Types

Next, let's simplify the quadrants, viewing a file in less and define each term.

paul@fullstack:~$ less notes/video0017.txt
Linux configuration files (video 17) ------------------------------------------- | | Interactive | Non-Interactive | ------------------------------------------- | Login | 1 | 3 | ------------------------------------------- | Non-Login | 2 | 4 | ------------------------------------------- Definitions ----------- Interactive - command prompt Non-Interactive - bash scripts Login - login first, ssh session Non-Login - prompt first, GUI Terminal/Konsole Startup ------- 1 Interactive Login - /etc/profile - ~/.bash_profile ~/.bash_login ~/.profile - ~/.bashrc 2 Interactive Non-Login - /etc/bash.bashrc - ~./bashrc 3 Non-Interactive Login - BASH_ENV 4 Non-Interactive Non-Login - BASH_ENV notes/video0017.txt (END)

Interactive means you type at the command prompt and Linux responds.

Non-Interactive occurs when you run a script that needs no interaction from a user, like system scripts.

Login refers to the start of the shell session and whether you log in or not. So you won't see a command prompt until after you log in. An example is an SSH session like the one I started earlier.

We can verify this by running echo $0 and if it starts with dash, then it's a login session, followed by the shell name bash.

paul@fullstack:~$ echo $0 -bash

A Non-Login session occurs for example if you have a Graphical User Interface (GUI) on your Linux computer, and you access the command prompt using a program like Terminal or Konsole, so you're logged in to the GUI and not required to do it again.

Step 4 - Which Configuration Files Are Run?

Quadrant 1 - Interactive Login Session

So which configuration files are loaded in Quadrant 1?

First it executes commands in the systemwide file /etc/profile, then it looks for three files in the user's home directory successively, and executes the first one found.

  1. ~/.bash_profile
  2. ~/.bash_login
  3. ~/.profile

On my Debian system, only the third file exists, ~/.profile, and in it you'll find environment variables not related to bash, and inside, it sources, or runs, a file called ~/.bashrc.

Quadrant 2 - Interactive Non-Login Session

In Quadrant 2, a Interactive Non-Login session starts with systemwide /etc/bash.bashrc and that same ~/.bashrc.

So .bashrc is run here as well and it's where bash-specific settings are placed, so it is very important. I suggest looking at that file later as well.

Quadrant 3 and Quadrant 4 - Both Non-Interactive

For Quadrant 3 and Quadrant 4, when bash is invoked non-interactively, like in a script, it accesses the BASH_ENV environment variable and runs a script there, meaning it won't read startup files unless you tell it to.

Step 5 - Next: Install Software Packages

The takeaway is not to memorize all of this, but instead to have a tutorial to return to, should you forget, and we will see this again in later tutorials.

(Here is the beginning of our software stack)

Next, we will cover installation of software packages as we close out the Linux for Beginners project and hop over to the software layer and play with Math in Python.

Have a nice day.

What's Next?

Have you seen our presence on YouTube? It offers more free chances to learn.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / linux-configuration.html

linux session types
linux interactive session
linux login session
linux non-login session
linux non-interactive session
linux configuration files
bash profile
bash invocation
linux tutorial

A newly-updated free resource. Connect and refer a friend today.