bash的環境配置文件加載原理

1、環境配置文件概述

在Linux上開發或者部署應用時,免不了要設置配置文件,好比安裝JDK,須要爲java可執行文件配置環境變量。
大多數時候咱們不須要關注shell,可是當你執行"sudo su" 命令時,發現並不能同時把環境變量切換到root的環境;當你執行遠程shell文件-"ssh who@host file.sh",發現不能加載環境配置文件時,那麼你就要搞清楚bash的環境配置文件加載原理來搞定這些問題。
本文所有是基於CentOS系統寫的,其它Linux發行版本可能略有差別。java

  1. 圖1-1展現了CentOS系統login shell 配置文件的讀取流程
     
    Paste_Image.png

    實線的方向是主線流程,虛線的方向是被文件/etc/profile或者~/.bash_profile調用的配置文件。
  2. 上圖中有好幾種配置文件,那麼Linux下爲什麼要搞這麼多配置文件呢?搞一個不就好了麼?
    Cameron Newham和Bill Rosenblatt在他們的著做《Learning the bash Shell, 2nd Edition》中解釋了緣由:

bash allows two synonyms for .bash_profile: .bash_login, derived from the C shell’s file named .login, and .profile, derived from the Bourne shell and Korn shell files named .profile. Only one of these three is read when you log in. If .bash_profile doesn’t exist in your home directory, then bash will look for .bash_login. If that doesn’t exist it will look for .profile.
One advantage of bash’s ability to look for either synonym is that you can retain your .profile if you have been using the Bourne shell. If you need to add bash-specific commands, you can put them in .bash_profile followed by the command source .profile. When you log in, all the bash-specific commands will be executed and bash will source .profile, executing the remaining commands. If you decide to switch to using the Bourne shell you don’t have to modify your existing files. A similar approach was intended for .bash_login and the C shell .login, but due to differences in the basic syntax of the shells, this is not a good idea.shell

翻譯以下:bash

bash容許.bash_profile 有兩個同義詞:app

  • .bash_login, 從C shell的.login文件衍生而來。
  • .profile, 從Bourne shellKorn shell.profile文件。

當你登陸shell的時候,這三個文件中僅有一個會被讀取。若是.bash_profile在你的用戶主目錄(home)下不存在,那麼bash將會查找.bash_login。若是.bash_login不存在,那麼bash將會查找.profileless

 

bash可以查找任意一個同義文件這種能力的優點就是--若是你使用了Bourne shell,可以保留你的.profile。若是你須要添加具體bash的命令,你能夠把它們放進.bash_profile文件,而且執行命令source .profile
若是你想切換到Bourne shell,你沒必要修改你已存在文件。對於.bash_loginC shell .login也是相同的,可是因爲shell的基礎語法存在差別,這麼作並不推薦。ssh

根本緣由是爲了兼容各類shell。ide

2、環境配置文件的加載順序

讀取環境配置文件以前,須要先區分login shell和non-login shell,由於這兩種shell讀取的配置文件不同。ui

  • login shell
    取得bash時須要完整的登陸流程,就稱爲login shell。舉例來講,你要由tty1~tty6登陸,須要輸入用戶的帳號和密碼,此時取得的就稱爲login shell
  1. /etc/profile 這是系統總體的設置,你最好不要修改這個文件。登陸時首先會讀取這個配置文件。
  2. /.bash_profile或/.bash_login或/.profile:屬於用戶我的設置,你要該本身的數據,就寫入這個文件。**登陸時shell會按照以上順序,僅讀取其中一個,若是/.bash_profile存在,則僅讀取/.bash_profile;不然若是/.bash_login存在,則讀取/.bash_login;不然讀取/.profile。**
    圖1-1展現了CentOS系統login shell加載環境配置文件的順序:
    /etc/profile -> ~/.bash_profile。
  • /etc/profile 文件會按照圖1-1的虛線依次讀取/etc/inputrc, /etc/profile.d/*.sh
  • ~/.bash_profile 文件會按照圖1-1的虛線依次讀取~/.bashrc,接着~/.bashrc 會讀取/etc/profile.d/.sh*。
  • non-login shell
    取得bash接口的方法不須要重複登陸舉動,舉例來講,你以X Window登陸Linux後,再以X的圖形界面啓動終端機,此時那個終端接口並無須要再次輸入帳號與密碼,那個bash的環境就稱爲non-login shell。你在本來的bash環境下再次執行bash這個命令,一樣也沒有輸入帳號密碼,那第二個bash(子進程)也是non-login shell。
    non-login shell 僅會讀取~/.bashrc~/.bashrc會按照圖1-1的虛線部分依次讀取配置文件。

基本原理到此已經結束,若是你還意猶未盡,再繼續看CentOS系統/etc/profile文件和~/.bash_profile文件的內容:this

  • /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
            *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}

if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`id -u`
        UID=`id -ru`
    fi
    USER="`id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
    pathmunge /sbin after
fi
HOSTNAME=`/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null 2>&1
        fi
    fi
done
unset i
unset -f pathmunge
export GREP_OPTIONS=--color=auto
  • ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

3、解答文章首段提到的問題

  1. 執行"sudo su" 命令沒法同時切換到root的配置環境,命令更正爲"sudo su -l"就能把帳戶和環境同時切換到root,緣由是"-l"這個這個參數告訴命令要從新登陸,即上述login shell
  2. 執行"sudo ssh who@host file.sh"遠程命令沒法加載環境配置文件,這須要同時搞清楚shell的模式和ssh的模式才能順利解決此問題,這能夠參考下面參考資料中的博客,其中有很是詳細的描述。
做者:Lucky_Micky 連接:https://www.jianshu.com/p/3c19279661a6 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。
相關文章
相關標籤/搜索