bash的环境配置文件加载原理

一、环境配置文件概述

在Linux上开发或者部署应用时,免不了要设置配置文件,比如安装JDK,需要为java可执行文件配置环境变量。
大多数时候我们不需要关注shell,但是当你执行"sudo su" 命令时,发现并不能同时把环境变量切换到root的环境;当你执行远程shell文件-"ssh who@host file.sh",发现不能加载环境配置文件时,那么你就要搞清楚bash的环境配置文件加载原理来搞定这些问题。
本文全部是基于CentOS系统写的,其它Linux发行版本可能略有差异。

  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.

翻译如下:

bash允许.bash_profile 有两个同义词:

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

当你登录shell的时候,这三个文件中仅有一个会被读取。如果.bash_profile在你的用户主目录(home)下不存在,那么bash将会查找.bash_login。如果.bash_login不存在,那么bash将会查找.profile

bash能够查找任意一个同义文件这种能力的优势就是--如果你使用了Bourne shell,能够保留你的.profile。如果你需要添加具体bash的命令,你可以把它们放进.bash_profile文件,并且执行命令source .profile
如果你想切换到Bourne shell,你不必修改你已存在文件。对于.bash_loginC shell .login也是相同的,但是由于shell的基础语法存在差异,这么做并不推荐。

根本原因是为了兼容各种shell。

二、环境配置文件的加载顺序

读取环境配置文件之前,需要先区分login shell和non-login shell,因为这两种shell读取的配置文件不一样。

  • 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文件的内容:

  • /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. 执行"sudo su" 命令无法同时切换到root的配置环境,命令更正为"sudo su -l"就能把账户和环境同时切换到root,原因是"-l"这个这个参数告诉命令要重新登录,即上述login shell
  2. 执行"sudo ssh who@host file.sh"远程命令无法加载环境配置文件,这需要同时搞清楚shell的模式和ssh的模式才能顺利解决此问题,这可以参考下面参考资料中的博客,其中有非常详细的描述。

参考系统- CentOS
参考资料:

  1. 鸟哥的私房菜
  2. 博客1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容