bash的環(huán)境配置文件加載原理

一、環(huán)境配置文件概述

在Linux上開發(fā)或者部署應(yīng)用時,免不了要設(shè)置配置文件,比如安裝JDK,需要為java可執(zhí)行文件配置環(huán)境變量。
大多數(shù)時候我們不需要關(guān)注shell,但是當(dāng)你執(zhí)行"sudo su" 命令時,發(fā)現(xiàn)并不能同時把環(huán)境變量切換到root的環(huán)境;當(dāng)你執(zhí)行遠(yuǎn)程shell文件-"ssh who@host file.sh",發(fā)現(xiàn)不能加載環(huán)境配置文件時,那么你就要搞清楚bash的環(huán)境配置文件加載原理來搞定這些問題。
本文全部是基于CentOS系統(tǒng)寫的,其它Linux發(fā)行版本可能略有差異。

  1. 圖1-1展示了CentOS系統(tǒng)login shell 配置文件的讀取流程
    Paste_Image.png

    實線的方向是主線流程,虛線的方向是被文件/etc/profile或者~/.bash_profile調(diào)用的配置文件。
  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.

翻譯如下:

bash允許.bash_profile 有兩個同義詞:

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

當(dāng)你登錄shell的時候,這三個文件中僅有一個會被讀取。如果.bash_profile在你的用戶主目錄(home)下不存在,那么bash將會查找.bash_login。如果.bash_login不存在,那么bash將會查找.profile。

bash能夠查找任意一個同義文件這種能力的優(yōu)勢就是--如果你使用了Bourne shell,能夠保留你的.profile。如果你需要添加具體bash的命令,你可以把它們放進(jìn).bash_profile文件,并且執(zhí)行命令source .profile。
如果你想切換到Bourne shell,你不必修改你已存在文件。對于.bash_loginC shell .login也是相同的,但是由于shell的基礎(chǔ)語法存在差異,這么做并不推薦。

根本原因是為了兼容各種shell。

二、環(huán)境配置文件的加載順序

讀取環(huán)境配置文件之前,需要先區(qū)分login shell和non-login shell,因為這兩種shell讀取的配置文件不一樣。

  • login shell
    取得bash時需要完整的登錄流程,就稱為login shell。舉例來說,你要由tty1~tty6登錄,需要輸入用戶的賬號和密碼,此時取得的就稱為login shell
  1. /etc/profile 這是系統(tǒng)整體的設(shè)置,你最好不要修改這個文件。登錄時首先會讀取這個配置文件。
  2. /.bash_profile或/.bash_login或/.profile:屬于用戶個人設(shè)置,你要該自己的數(shù)據(jù),就寫入這個文件。**登錄時shell會按照以上順序,僅讀取其中一個,如果/.bash_profile存在,則僅讀取/.bash_profile;否則如果/.bash_login存在,則讀取/.bash_login;否則讀取/.profile。**
    圖1-1展示了CentOS系統(tǒng)login shell加載環(huán)境配置文件的順序:
    /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接口的方法不需要重復(fù)登錄舉動,舉例來說,你以X Window登錄Linux后,再以X的圖形界面啟動終端機(jī),此時那個終端接口并沒有需要再次輸入賬號與密碼,那個bash的環(huán)境就稱為non-login shell。你在原本的bash環(huán)境下再次執(zhí)行bash這個命令,同樣也沒有輸入賬號密碼,那第二個bash(子進(jìn)程)也是non-login shell。
    non-login shell 僅會讀取~/.bashrc~/.bashrc會按照圖1-1的虛線部分依次讀取配置文件。

基本原理到此已經(jīng)結(jié)束,如果你還意猶未盡,再繼續(xù)看CentOS系統(tǒng)/etc/profile文件和~/.bash_profile文件的內(nèi)容:

  • /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

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

  1. 執(zhí)行"sudo su" 命令無法同時切換到root的配置環(huán)境,命令更正為"sudo su -l"就能把賬戶和環(huán)境同時切換到root,原因是"-l"這個這個參數(shù)告訴命令要重新登錄,即上述login shell。
  2. 執(zhí)行"sudo ssh who@host file.sh"遠(yuǎn)程命令無法加載環(huán)境配置文件,這需要同時搞清楚shell的模式和ssh的模式才能順利解決此問題,這可以參考下面參考資料中的博客,其中有非常詳細(xì)的描述。

參考系統(tǒng)- CentOS
參考資料:

  1. 鳥哥的私房菜
  2. 博客1
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,428評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,024評論 3 413
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 175,285評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,548評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,328評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,878評論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,971評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,098評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,616評論 1 331
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,554評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,725評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,243評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 43,971評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,361評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,613評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,339評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,695評論 2 370

推薦閱讀更多精彩內(nèi)容