Linux系统工具使用指南《Linux/Mac/vim相关知识》收集

linux下的/etc/profile、/etc/bashrc、

2018-09-18  本文已影响45人  peerless_1024

一、介绍

/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)时,执行该文件。可把一些清理工作的命令放到这文件中。

二、区别和联系

三、执行顺序

关于登录linux时,/etc/profile、~/.bash_profile等几个文件的执行过程。

①在刚登录Linux时,

其中~/.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

四、其他

  1. 图形模式登录时,顺序读取:/etc/profile和~/.profile
  2. 图形模式登录后,打开终端时,顺序读取:/etc/bash.bashrc和~/.bashrc
  3. 文本模式登录时,顺序读取:/etc/bash.bashrc,/etc/profile和~/.bash_profile
  4. 从其它用户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
  5. 注销时,或退出su登录的用户,如果是longin方式,那么bash会读取:~/.bash_logout
  6. 执行自定义的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/的区别

上一篇下一篇

猜你喜欢

热点阅读