
Linux系統(tǒng)配置文件詳解
區(qū)別
首先先總體看一下區(qū)別
============
/etc/profile
此文件為系統(tǒng)的每個(gè)用戶設(shè)置環(huán)境信息,對(duì)所有用戶有效
===========
/etc/bashrc (ubuntu為 /etc/bash.bashrc)
為每一個(gè)運(yùn)行bash shell的用戶執(zhí)行此文件.對(duì)所有用戶有效
===============
~/.bash_profile (ubuntu為 ~/.profile)
類似/etc/profile,但僅僅針對(duì)當(dāng)前用戶有效
=========
~/.bashrc
類似/etc/bashrc,但僅僅針對(duì)當(dāng)前用戶有效
==============
~/.bash_logout
當(dāng)每次退出系統(tǒng)(退出bash shell)時(shí),執(zhí)行該文件.
- Linux的Shell種類眾多,常見(jiàn)的有:
Bourne Shell(/usr/bin/sh或/bin/sh)、
Bourne Again Shell(/bin/bash)、
C Shell(/usr/bin/csh)、
K Shell(/usr/bin/ksh)、
Shell for Root(/sbin/sh)等等。- 不同的Shell語(yǔ)言的語(yǔ)法有所不同,所以不能交換使用。每種Shell都有其特色之處,基本上,掌握其中任何一種 就足夠了。在本文中,我們關(guān)注的重點(diǎn)是Bash,也就是Bourne Again Shell,由于易用和免費(fèi),Bash在日常工作中被廣泛使用;同時(shí),Bash也是大多數(shù)Linux系統(tǒng)默認(rèn)的Shell。
login和non login
login和non login指的是用登錄或非登錄的方式打開(kāi)bash shell,不同的方式的讀取的配置文件不同,可以歸納為下表:
| login | non login | |
|---|---|---|
| 全局 | /etc/profile | /etc/bashrc |
| 單用戶 | ~/.bash_profile | ~/.bashrc |
執(zhí)行順序
登錄Linux時(shí)執(zhí)行
在 剛登錄Linux時(shí),首先啟動(dòng) /etc/profile文件,然后再啟動(dòng)用戶目錄下的 ~/.bash_profile
再執(zhí)行用戶的bash設(shè)置:
如果~/.bash_profile文件存在的話,會(huì)執(zhí)行用戶的 ~/.bashrc文件。
#if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
同樣~/.bashrc中,一般還會(huì)在文件的前面有以下代碼,來(lái)執(zhí)行/etc/bashrc
if [ -f /etc/bashrc ] ; then
. /etc/bashrc
所以,~/.bashrc會(huì)調(diào)用/etc/bashrc文件。最后,在退出shell時(shí),還會(huì)執(zhí)行 ~/.bash_logout文件。
執(zhí)行順序?yàn)?
- /etc/profile
- ~/.bash_profile | ~/.bash_login | ~/.profile
- ~/.bashrc
- /etc/bashrc
- ~/.bash_logout
區(qū)別和聯(lián)系
- 在 /etc目錄是系統(tǒng)級(jí)(全局)的配置文件,當(dāng)在用戶主目錄下找不到~/.bash_profile 和~/.bashrc時(shí),就會(huì)讀取這兩個(gè)文件。
- /etc/profile 中設(shè)定的變量(全局)的可以作用于任何用戶,而 ~/.bashrc 中設(shè)定的變量(局部)只能繼承 /etc/profile 中的變量,他們是“父子”關(guān)系。
- ~/.bash_profile 是交互式、login 方式進(jìn)入 bash 運(yùn)行的; ~/.bashrc 是交互式 non-login 方式進(jìn)入 bash 運(yùn)行的。通常二者設(shè)置大致相同,所以通常前者會(huì)調(diào)用后者。設(shè)置生效:可以重啟生效,也可以使用命令:source。
- ~/.bash_history是bash shell的歷史記錄文件,里面記錄了你在bash shell中輸入的所有命令??赏ㄟ^(guò)HISSIZE環(huán)境變量設(shè)置在歷史記錄文件里保存記錄的條數(shù)。
其他
下面是幾個(gè)例子:
- 圖形模式登錄時(shí),順序讀取:
/etc/profile和~/.profile - 圖形模式登錄后,打開(kāi)終端時(shí),順序讀?。?code>/etc/bash.bashrc和
~/.bashrc - 文本模式登錄時(shí),順序讀?。?code>/etc/bash.bashrc,
/etc/profile和~/.bash_profile - 從其它用戶su到該用戶,則分兩種情況:
(1)如果帶-l參數(shù)(或-參數(shù),–login參數(shù)),如:su -l username,則bash是lonin的,它將順序讀取以下配置文件:/etc/bash.bashrc,/etc/profile和~ /.bash_profile。
(2)如果沒(méi)有帶-l參數(shù),則bash是non-login的,它將順序讀?。?code>/etc/bash.bashrc和~/.bashrc - 注銷時(shí),或退出su登錄的用戶,如果是longin方式,那么bash會(huì)讀?。?code>~/.bash_logout
- 執(zhí)行自定義的shell文件時(shí),若使用“bash -l a.sh”的方式,則bash會(huì)讀取行:
/etc/profile和~/.bash_profile,若使用其它方式,如:bash a.sh, ./a.sh,sh a.sh(這個(gè)不屬于bash shell),則不會(huì)讀取上面的任何文件。
分類: [Linux]
標(biāo)簽: [Linux]
在這里插入圖片描述