一、介紹
/etc/profile
此文件為系統的每個用戶設置環境信息,當用戶第一次登錄時,該文件被執行。并從 /etc/profile.d 目錄的配置文件中收集 shell 的設置。如果你有對 /etc/profile 有修改的話必須得 source 一下你的修改才會生效,此修改對每個用戶都生效。
/etc/bashrc(ubuntu為 /etc/bash.bashrc)
為每一個運行 bash shell 的用戶執行此文件。當 bash shell 被打開時,該文件被讀取。如果你想對所有的使用 bash 的用戶修改某個配置并在以后打開的 bash 都生效的話可以修改這個文件,修改這個文件不用重啟,重新打開一個 bash 即可生效。
Ubuntu沒有此文件,與之對應的是/ect/bash.bashrc。
~/.bash_profile(ubuntu為 ~/.profile)
每個用戶都可使用該文件輸入專用于自己使用的 shell 信息,當用戶登錄時,該文件僅僅執行一次!默認情況下,它設置一些環境變量,執行用戶的~/ .bashrc 文件。 此文件類似于 /etc/profile,也是需要需要 source 才會生效,/etc/profile 對所有用戶生效,~/.bash_profile 只對當前用戶生效。~/.profile(由Bourne Shell和Korn Shell使用)和.login(由C Shell使用)兩個文件是.bash_profile的同義詞,目的是為了兼容其它Shell。
- Linux的Shell種類眾多,常見的有:
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語言的語法有所不同,所以不能交換使用。每種Shell都有其特色之處,基本上,掌握其中任何一種 就足夠了。在本文中,我們關注的重點是Bash,也就是Bourne Again Shell,由于易用和免費,Bash在日常工作中被廣泛使用;同時,Bash也是大多數Linux系統默認的Shell。
- 在一般情況下,人們并不區分 Bourne Shell和Bourne Again Shell,所以,在下面的文字中,我們可以看到#!/bin/sh,它同樣也可以改為#!/bin/bash。
~/.bashrc
該文件包含專用于你的 bash shell 的 bash 信息,當登錄時以及每次打開新的 shell 時,該文件被讀取。(每個用戶都有一個 ~/.bashrc 文件,在用戶目錄下) 此文件類似于 /etc/bashrc,不需要重啟就可以生效,重新打開一個 bash 即可生效,/etc/bashrc 對所有用戶新打開的 bash 都生效,但 ~/.bashrc 只對當前用戶新打開的 bash 生效。
~/.bashrc文件會在bash shell調用另一個bash shell時讀取,也就是在shell中再鍵入bash命令啟動一個新shell時就會去讀該文件。這樣可有效分離登錄和子shell所需的環境。但一般 來說都會在/.bash_profile里調用/.bashrc腳本以便統一配置用戶環境。
~/.bash_logout:
當每次退出系統(退出 bash shell)時,執行該文件。可把一些清理工作的命令放到這文件中。
二、區別和聯系
- 在 /etc目錄是系統級(全局)的配置文件,當在用戶主目錄下找不到~/.bash_profile 和~/.bashrc時,就會讀取這兩個文件。
- /etc/profile 中設定的變量(全局)的可以作用于任何用戶,而 ~/.bashrc 中設定的變量(局部)只能繼承 /etc/profile 中的變量,他們是“父子”關系。
- ~/.bash_profile 是交互式、login 方式進入 bash 運行的; ~/.bashrc 是交互式 non-login 方式進入 bash 運行的。通常二者設置大致相同,所以通常前者會調用后者。設置生效:可以重啟生效,也可以使用命令:source。
- ~/.bash_history是bash shell的歷史記錄文件,里面記錄了你在bash shell中輸入的所有命令。可通過HISSIZE環境變量設置在歷史記錄文件里保存記錄的條數。
三、執行順序
關于登錄linux時,/etc/profile、~/.bash_profile等幾個文件的執行過程。
①在剛登錄Linux時,
- 首先啟動 /etc/profile 文件,
- 然后再啟動用戶目錄下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中
的其中一個,執行的順序為:~/.bash_profile、 ~/.bash_login、 ~/.profile
其中~/.bash_profile、 ~/.bash_login和 ~/.profile文件往往只存在一個,這與Linux的發行版本有關。centos中為 ~/.bash_profile,ubuntu則為 ~/.profile。
- 以上兩個文件會在用戶登錄時執行。
②下面開始執行用戶的bash設置
如果 ~/.bash_profile文件存在的話,一般會以這樣的方式執行用戶的 ~/.bashrc文件。
在 ~/.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
同樣~/.bashrc中,一般還會在文件的前面有以下代碼,來執行/etc/bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
所以,~/.bashrc會調用 /etc/bashrc文件。最后,在退出shell時,還會執行 ~/.bash_logout文件。
執行順序為:
- /etc/profile
- ~/.bash_profile | ~/.bash_login | ~/.profile
- ~/.bashrc
- /etc/bashrc
- ~/.bash_logout
四、其他
- 圖形模式登錄時,順序讀取:/etc/profile和~/.profile
- 圖形模式登錄后,打開終端時,順序讀取:/etc/bash.bashrc和~/.bashrc
- 文本模式登錄時,順序讀取:/etc/bash.bashrc,/etc/profile和~/.bash_profile
- 從其它用戶su到該用戶,則分兩種情況:
(1)如果帶-l參數(或-參數,–login參數),如:su -l username,則bash是lonin的,它將順序讀取以下配置文件:/etc/bash.bashrc,/etc/profile和~ /.bash_profile。
(2)如果沒有帶-l參數,則bash是non-login的,它將順序讀取:/etc/bash.bashrc和~/.bashrc - 注銷時,或退出su登錄的用戶,如果是longin方式,那么bash會讀取:~/.bash_logout
- 執行自定義的shell文件時,若使用“bash -l a.sh”的方式,則bash會讀取行:/etc/profile和~/.bash_profile,若使用其它方式,如:bash a.sh, ./a.sh,sh a.sh(這個不屬于bash shell),則不會讀取上面的任何文件。
參考博客:
Linux中profile、bashrc、/.bash_profile、/.bashrc、~/.bash_profile之間的區別和聯系以及執行順序
淺析linux下的/etc/profile、/etc/bashrc、/.bash_profile、/.bashrc文件
Linux 中 profile,bashrc,bash_profile,/etc/profile,/etc/profile.d/的區別