歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> /etc/bashrc和/etc/profile傻傻分不清楚?

/etc/bashrc和/etc/profile傻傻分不清楚?

日期:2017/3/3 17:47:43   编辑:學習Linux

/etc/bashrc和/etc/profile傻傻分不清楚?

/etc/bashrc和/etc/profile傻傻分不清楚?



導讀在一般的 linux 或者 unix 系統中, 都可以通過編輯 bashrc 和 profile來設置用戶的工作環境, 很多文章對於 profile 和 bashrc 也都有使用, 但究竟每個文件都有什麼作用和該如何使用呢?

首先我們來看系統中的這些文件, 一般的系統可能會有

/etc/profile/etc/bashrc~/.bashrc~/.profile

而如果系統是 ubuntu 或者 debian 的話, 就不會有 /etc/bashrc 而會有 /etc/bash.bashrc 文件.
以上這些就是常用 profile 和 bashrc 文件了. 要理解這些文件之前還需要了解 Shell, Shell 的 login(登入) 和 interactive(交互式) 模式.

Shell 的分類

系統的 shell 有很多種, 比如 bash, sh, zsh 之類的, 如果要查看某一個用戶使用的是什麼 shell 可以通過 finger [USERNAME] 命令來查看. 我們這裡只說 shell 是 bash 的情況, 因為如果是 sh 或者其他 shell 顯然不會運行 bashrc 的.

login shell 和 no-login shell

“login shell” 代表用戶登入, 比如使用 “su -“ 命令, 或者用 ssh 連接到某一個服務器上, 都會使用該用戶默認 shell 啟動 login shell 模式.
該模式下的 shell 會去自動執行 /etc/profile 和 ~/.profile 文件, 但不會執行任何的 bashrc 文件, 所以一般再 /etc/profile 或者 ~/.profile 裡我們會手動去 source bashrc 文件.
而 no-login shell 的情況是我們在終端下直接輸入 bash 或者 bash -c “CMD” 來啟動的 shell.
該模式下是不會自動去運行任何的 profile 文件.

interactive shell 和 non-interactive shell

interactive shell 是交互式shell, 顧名思義就是用來和用戶交互的, 提供了命令提示符可以輸入命令.
該模式下會存在一個叫 PS1 的環境變量, 如果還不是 login shell 的則會去 source /etc/bash.bashrc 和 ~/.bashrc 文件
non-interactive shell 則一般是通過 bash -c “CMD” 來執行的bash.

該模式下不會執行任何的 rc 文件, 不過還存在一種特殊情況這個我之後詳細講述

在可能存在的模式組合中 RC 文件的執行

SSH login, sudo su - [USER] 或者 mac 下開啟終端
ssh 登入和 su - 是典型的 interactive login shell, 所以會有 PS1 變量, 並且會執行

/etc/profile~/.profile
在命令提示符狀態下輸入 bash 或者 ubuntu 默認設置下打開終端

這樣開啟的是 interactive no-login shell, 所以會有 PS1 變量, 只會執行

/etc/bash.bashrc~/.bashrc

通過 bash -c “CMD” 或者 bash BASHFILE 命令執行的 shell

這些命令什麼都不會執行, 也就是設置 PS1 變量, 不執行任何 RC 文件
最特殊! 通過 “ssh server CMD” 執行的命令 或 通過程序執行遠程的命令

這是最特殊的一種模式, 理論上應該既是 非交互 也是 非登入的, 但是實際上他不會設置 PS1, 但是還會執行

/etc/bash.bashrcbashrc

這裡還有一點值得注意的是 /etc/bashrc 任何情況下都不會執行.

bashrc 和 profile 的區別

看了之前那麼多種狀態組合, 最關鍵的問題是, 究竟 bashrc 和 profile 有什麼區別呢?

profile

其實看名字就能了解大概了, profile 是某個用戶唯一的用來設置環境變量的地方, 因為用戶可以有多個 shell 比如 bash, sh, zsh 之類的, 但像環境變量這種其實只需要在統一的一個地方初始化就可以了, 而這就是 profile.

bashrc

bashrc 也是看名字就知道, 是專門用來給 bash 做初始化的比如用來初始化 bash 的設置, bash 的代碼補全, bash 的別名, bash 的顏色. 以此類推也就還會有 shrc, zshrc 這樣的文件存在了, 只是 bash 太常用了而已.
期望的執行順序
=> 代表 在文件內部 source, 換行的 => 代表自身執行結束以後在 source, 同一行表示先 source 在執行自身

普通 login shell
/etc/profile   => /etc/bash.bashrc~/.profile  => ~/.bashrc => /etc/bashrc
終端種直接運行 bash
/etc/bash.bashrc~/.bashrc => /etc/bashrc
bash -c “CMD”什麼都不執行
ssh server “CMD”/etc/bash.bashrc => /etc/profile~/.bashrc => | /etc/bashrc => /etc/profile             | ~/.profile

這裡會有點小混亂, 因為既有 /etc/bash.bashrc 又有 /etc/bashrc, 其實是這樣的 ubuntu 和 debian 有 /etc/bash.bashrc 文件但是沒有 /etc/bashrc, 其他的系統基本都是只有 /etc/bashrc 沒有 /etc/bash.bashrc.

最終修改

為了達到上述我們需要的執行流程, 那必須對系統的 rc 文件做修改. 我們拿 Ubuntu 舉例
首先 我們編輯 /etc/profile 在文件頭部加入

export system_profile_loaded=1

這樣其他文件就可以根據 $system_profile_loaded 來判斷是否已經載入過 profile, 接著我們可以看到

  unset ifiif [ "$PS1" ]; then  if [ "$BASH" ]; then    PS1='\u@\h:\w\$ '    if [ -f /etc/bash.bashrc ]; then    . /etc/bash.bashrc    fi  else    if [ "`id -u`" -eq 0 ]; then      PS1='# '    else      PS1='$ '    fi  fifiumask 022

按照我們剛才的方案, 應該是不管任何情況都應該在文件末尾去載入 bashrc, 所以我們修改成

  unset ifiumask 022if [ "$BASH" ]; then  if [ "$PS1" ]; then    PS1='\u@\h:\w\$ '  fi  if [ -f /etc/bash.bashrc ]; then    . /etc/bash.bashrc  fielse  if [ "`id -u`" -eq 0 ]; then    PS1='# '  else    PS1='$ '  fifi

當然也可以有其他該法,只要符合在文件末尾載入 bashrc 就可以了.
接著我們修改 /etc/bash.bashrc, 我們需要在文件頭加入

[ -n "${system_bashrc_running}" ] && returnsystem_bashrc_running=1[ -z "${system_profile_loaded}" ] && source /etc/profileunset system_bashrc_runningsystem_bashrc_runned=1

其中 system_bashrc_running 這樣的變量都是為了防止2次反復調用而加入的.
這樣系統級別的 rc 文件基本修改好了, 最好還可以再修改一下本地的rc文件, 所以我們編輯 “~/.profile”, 發現起內容是

# ~/.profile: executed by Bourne-compatible login shells.if [ -n "$BASH" ]; then  if [ -f ~/.bashrc ]; then    . ~/.bashrc  fifimesg n

而按照上述的修改規則只需要替換成

export local_profile_loaded=1if [ -n "$BASH" ]; then  if [ -f ~/.bashrc ]; then    . ~/.bashrc  fifi

這樣就始終再載入完 profile 以後去載入 bashrc 了, 接著我們像編輯 /etc/bash.bashrc 一樣的去修改 ~/.bashrc, 文件頭上加入

[ -n "${local_bashrc_running}" ] && returnlocal_bashrc_running=1[ -r /etc/bashrc -a -z "${system_bashrc_runned}" ] && source /etc/bashrc[ -r ~/.profile -a -z "${local_profile_loaded}" ] && source ~/.profileunset local_bashrc_running

用來防止反復加載 profile, 並且這裡需要特殊注明的是

[ -r /etc/bashrc -a -z "${system_bashrc_runned}" ] && source /etc/bashrc

/etc/bashrc 這個文件只有在 mac 之類的系統下才有, 所以 ubuntu 這裡這行可以不加, 不過有判斷是否存在所以加了也沒關系.
到這裡基本上就可以比較完美的解決不通的 shell 加載順序問題了, 當然比如這個用戶用的是 zsh 之類的也需要按照類型的原理來修改.
另外, 在用戶目錄下 可能會存在 ~/.bash_profile, ~/.bash_login 這樣的文件, 但如果有這些文件 bash 就不會去載入 ~/.profile 了, 所以如果存在的話需要刪除 這些文件並把內容合並進 ~/.profile 和 ~/.bashrc 才行.

原文來自:http://www.linuxeye.com/Linux/bashrc-profile.html

本文地址:http://www.linuxprobe.com/diff-bashrcprofile.html


http://xxxxxx/Linuxjc/1184706.html TechArticle

Copyright © Linux教程網 All Rights Reserved