profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 1

 

by:授客 QQ1033553122

 

(CentOS-6.0-x86_64-bin-DVD1.iso为例)

1、  普通用户

[root@localhost home]# useradd test

[root@localhost home]# passwd test

...

passwd: all authentication tokens updated successfully.

[test@localhost ~]$ cd /home/test

[test@localhost ~]$ ll -a

total 28

...

-rw-r--r--. 1 test test   18
May 30  2011
.bash_logout

-rw-r--r--. 1 test test  176 May 30  2011
.bash_profile

-rw-r--r--. 1 test test  124 May 30  2011
.bashrc

...

可见用户主目录/home/username/目录下包含了以上3个文件(及其它本文不关注的文件)

#查看.bash_profile文件

[root@localhost test]# cat .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

代码浅析:

-f ~/.bashrc:如果/home/test/.bashrc为普通文件,那么返回真,即if
[ true ],

if [ -f ~/.bashrc ]:then

. ~/.bashrc 
#等同source
~/.bashrc

fi 
#结束if语句

如果-f
~/.bashrc返回真,那么执行/home/test/.bashrc脚本

# User specific environment and startup programs
#用户特定的环境和启动程序

PATH=$PATH:$HOME/bin

export PATH

#查看.bashrc文件

[root@localhost test]# cat .bashrc

# .bashrc

# Source global definitions

if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi

# User specific aliases and functions

代码浅析:

if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi

如果-f
/etc/bashrc返回真,那么执行/etc/.bashrc脚本

# User specific aliases and functions #用户特定的别名和函数

#查看.bash_logout文件

[root@localhost test]# cat .bash_logout

# ~/.bash_logout


2、 

超级用户

[root@localhost test]# cd /root/

[root@localhost ~]# ll -a

total 172

...

-rw-r--r--. 
1 root root   
18 May 20 
2009 .bash_logout

-rw-r--r--. 
1 root root   311
Sep  3 22:42
.bash_profile

-rw-r--r--. 
1 root root   176
Sep 23  2004
.bashrc

...

如上,和普通用户一样,包含了以上3个文件(及本文不关注的其它文件)

#查看.bash_profile文件

[root@localhost ~]# cat .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文件

[root@localhost ~]# cat .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

#查看.bash_logout文件

[root@localhost ~]# cat .bash_logout

# ~/.bash_logout


对比
root和普通用户,我们可以得出:

1. 
linux用户主目录(超级用户主目录:~为/root

普通用户主目录:~为/home/username)下包含以3个文件

~/.bash_profile

~/.bashrc

~/.bash_logout

默认情况下,这些文件的设置,仅对单一用户起作用

2. 
~/.bash_logout文件默认啥都不做

3. 
~/.bash_profile:该文件用于为单个用户自身设置特定的局部环境(比如path环境变量)和启动程序,某些情况下,还用于执行~/.bashrc文件

4. 
~./bashrc:该文件用于为单个运行bash
shell的用户自身设置特定的资源(比如命令别名和函数,本地变量),某些情况下,还用于执行/etc/bashrc文件(注:rc的含义是resource
configuration,该文件是针对bash
shell的,这个角度来说它的作用域也是局部的,因为其它shell有其它shell自己的rc文件)

5. 
当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。


3、 

/etc/profile && /etc/bashrc

#etc目录下的配置文件

[root@localhost ~]# cd /etc/

[root@localhost etc]# ll -a | grep profile

-rw-r--r--. 
1 root root   1459
Nov 12  2010
profile

drwxr-xr-x. 
2 root root   4096
Sep  4 14:06
profile.d

[root@localhost etc]# ll -a | grep rc

-rw-r--r--. 
1 root root   2620
Nov 12  2010
bashrc

-rw-r--r--. 
1 root root   1596
Jan 12  2010
csh.cshrc

lrwxrwxrwx. 
1 root root    
11 Sep  3
00:46 init.d -> rc.d/init.d

...

-rw-r--r--. 
1 root root    715
Nov 12  2010
kshrc

lrwxrwxrwx. 
1 root root     
7 Sep  3
00:46 rc -> rc.d/rc

lrwxrwxrwx. 
1 root root    
10 Sep  3
00:46 rc0.d -> rc.d/rc0.d

lrwxrwxrwx. 
1 root root    
10 Sep  3
00:46 rc1.d -> rc.d/rc1.d

lrwxrwxrwx. 
1 root root    
10 Sep  3
00:46 rc2.d -> rc.d/rc2.d

lrwxrwxrwx. 
1 root root    
10 Sep  3
00:46 rc3.d -> rc.d/rc3.d

lrwxrwxrwx. 
1 root root    
10 Sep  3
00:46 rc4.d -> rc.d/rc4.d

lrwxrwxrwx. 
1 root root    
10 Sep  3
00:46 rc5.d -> rc.d/rc5.d

lrwxrwxrwx. 
1 root root    
10 Sep  3
00:46 rc6.d -> rc.d/rc6.d

drwxr-xr-x. 10 root root   4096
Sep  3 00:46
rc.d

lrwxrwxrwx. 
1 root root    
13 Sep  3
00:46 rc.local -> rc.d/rc.local

lrwxrwxrwx. 
1 root root    
15 Sep  3
00:46 rc.sysinit -> rc.d/rc.sysinit

...

#查看/etc/profile文件

[root@localhost etc]# cat profile

# /etc/profile

# System wide environment and startup programs, for login
setup

# Functions and aliases go in /etc/bashrc

# It's NOT good idea to change this file unless you know what
you

# are doing. Much better way is to create custom.sh shell script
in

# /etc/profile.d/ to make custom changes to environment. This
will

# prevent need for merging in future updates.

...

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE
HISTCONTROL

for i in /etc/profile.d/*.sh ; do

if [ -r "$i" ]; then

if [ "$PS1" ]; then

. $i

else

. $i >/dev/null 2>&1

fi

fi

done

unset i

unset pathmunge

说明:

# System wide environment and startup programs, for login
setup

#为登录而设置的系统全局环境和启动程序,

# Functions and aliases go in /etc/bashrc

#函数和别名放在/etc/bashrc

[root@localhost etc]# cat bashrc

# /etc/bashrc

# System wide functions and aliases

# Environment stuff goes in /etc/profile

# It's NOT good idea to change this file unless you know what
you

# are doing. Much better way is to create custom.sh shell script
in

# /etc/profile.d/ to make custom changes to environment. This
will

# prevent need for merging in future updates.

# By default, we want this to get set.

# Even for non-interactive, non-login shells.

...

说明:

# System wide functions and aliases

#
系统全局函数和别名

# Environment stuff goes in /etc/profile

#
环境的配置放在/etc/profile


综上,我们可以得出

1. 
linux /etc主目录下包含以下2个文件,1个目录(及其它本文不关注的文件)

/etc/profile

/etc/bashrc

/etc/pfofile.d/目录

默认情况下,这些文件的设置,仅对系统中所有用户起作用,即作用域为全局

2. 
/etc/profile:系统为所有用户设置全局缺省环境(比如path环境变量)和启动程序的配置文件,并从/etc/profile.d目录的配置文件中搜集shell的设置

3. 
/bashrc:系统为所有运行bash
shell的用户设置全局资源

(比如系统全局函数,变量和命令别名等)的配置文件(注意:该文件是针对bash
shell的,这个角度来说它的作用域也是局部的,因为其它shell有其它shell自己的rc文件)


4、 

文件执行顺序

/etc目录:/etc/profile,/etc/bashrc

~主目录:~/.bash_pfoile,~/.bash_login,~/.profile,~/.bashrc,~/.bash_logout

注:不一定每个linux操作系统的~主目录中都存在上述列出的所有文件

这些文件具体的执行顺序是???执行顺序和bash
shell的类型有关,所以先来了解下bash
shell的类型

login
shell

以下情形中的取得的bash,称为login
shell:

1. 
字符界面下,在终端tty1~tty6,通过输入帐号,密码登录,取得的bash

2. 
通过ssh,xshell,putty等工具连接系统,取得的bash

3. 
字符界面下,通过命令su
-、su
–l、su
--login等取得的bash

interactive
shell

以下情形中的取得的bash,称为interactive
shell:

1. 
可视桌面下,通过新建一个终端(Applications->System
Tools->Terminal)取得的bash

2. 
字符界面下,通过命令bash

取得的bash

3. 
字符界面下,通过命令su、su
username取得的bash(注意:此处su不带


选项)


为何要分
login
shell
interactive
shell?

最初的设计是这样考虑的,如果从字符终端登录或者远程登录,那么login
Shell是该用户的所有其它进程的父进程,也是其它子Shell的父进程,所以环境变量在login
Shell的启动脚本里设置一次就可以作用于其它非login
Shell里,但是login
Shell的本地变量、函数、别名等设置没有办法作用于子Shell,需要每次启动非login
Shell时设置一遍,所以就需要有非login
Shell的启动脚本

非login
shell
有它特定的用途,比如一个用Linux搭建一个ftp服务器,并且创建了很多的ftp用户,那么就可以将这些用户的默认shell改为nologin,这样一来,这些虽然是Linux上的用户可是却无法登录进Linux主机,只能登录ftp服务器了


怎么区分
login
shell
interactive
shell

[root@localhost ~]# su -

[root@localhost ~]# echo $0

-bash

[root@localhost ~]# bash

[root@localhost ~]# echo $0

bash

说明:echo
$0 –>输出结果:-bash

表明此时的bash为login
shell,bash

则表明此时的bash为interactive
shell


login shell
下的文件执行顺序


A.  


bashlogin
shell
的方式启动时:

1、如果存在/etc/profile文件,它先读取文件/etc/profile,并执行该文件中的命令

2、然后查找~/.bash_profile,
~/.bash_login,

~/.profile,
按~/.bash_profile
-> ~/.bash_login -> ~/.profile的顺序,从第一个存在且可读的文件中读取命令并执行(注意:仅在3个文件按顺序读取一个).

注意:

通过--noprofile选项可强制bash不读取上面的任意文件

Linux profile1,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part1的更多相关文章

  1. profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 2

    profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 2   by:授客 QQ:103355312 ...

  2. Linux环境变量文件environment, profile, bashrc含义

    转自:http://www.th7.cn/system/lin/201508/127503.shtml (1)/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件 ...

  3. profile bashrc bash_profile之间的区别和联系

    profile bashrc bash_profile之间的区别和联系 博客分类: Linux   执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bas ...

  4. profile bashrc bash_profile 之间的区别和联系

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...

  5. profile, bashrc, source, setup.*sh

    一. source: 命令是使该文件立刻执行,这样刚才做的修改就可以立即生效了,否则要重新启动系统修改才能生效.(执行其后命令使之立即生效,不用重启) 二.bashrc: 1.linux系统:/etc ...

  6. /etc/rc.local ; /etc/init.d ;/etc/profile;/etc/bashrc;~/.bash_profile;~/.bashrc;~/.bash_logout

    1. /etc/rc.local 这是使用者自订开机启动程序,把需要开机自动运行的程序写在这个脚本里. 把脚本程序写在/etc/rc.d/init.d/目录下也可以  在完成 run level 3 ...

  7. Linux下文件 ~/.bashrc 和 ~/.bash_profile 和 /etc/bashrc 和 /etc/profile 的区别 | 用户登录后加载配置文件的顺序

    转自 https://blog.csdn.net/secondjanuary/article/details/9206151 文件说明: /ect/profile 此文件为系统的每个用户设置环境信息, ...

  8. linux /etc/profile bashrc bash_profile

    文件: /etc/profile  ~/.bashrc  和  ~/.bash_profile 的使用区别: /etc/profile: 全局 环境变量等,在机器重启后执行一次, 用于设置环境变量,更 ...

  9. Bash: about .bashrc, .bash_profile, .profile, /etc/profile, etc/bash.bashrc and others

    Some interesting excerpts from the bash manpage:When bash is invoked as an interactive login shell, ...

随机推荐

  1. Python环境搭建详解(Window平台)

    前言 Python,是一种面向对象的解释型计算机程序设计语言,是纯粹的自由软件,Python语法简洁清晰,特色是强制用空白符作为语句缩进,具有丰富和强大的库,它常被称为胶水语言. Python是一种解 ...

  2. win10 + gluon + GPU

    1. 下载教程 可以用浏览器下载zip格式并解压,在解压目录文件资源管理器的地址栏输入cmd进入命令行模式. 也可以 git pull https://github.com/mli/gluon-tut ...

  3. vue中子组件通过$parent操作父组件的参数或者方法

    先看一个简单的demo: 父组件添加一个弹框,弹框的内容是另外导入的一个子组件: <Modal v-model="accountDetailsModal" class=&qu ...

  4. Xamarin.Android 使用 Encoding.GetEncoding("GB2312") 报错解决方案

    问题描述:使用byte转换成汉字编码格式,debug模式下可以正常运行,但是release模式下就会出现死机问题. 排除过程:最开始不知道是编码格式转换错误,于是把相关代码块注释掉,然后进行relea ...

  5. call、apply、bind的用法

    数组追加 //用apply拼接 var arr1=[12,'name:foo',2048]; var arr2=['Joe','Hello']; Array.prototype.push.apply( ...

  6. 关于小窗滑动,父级body也跟随滑动的解决方案

    需求:当前页面是信息列表,所以高度由内容自动填充, 所以页面可以上下滑动,加载更多, 但是下发物料一栏又为一个列表 所以做了一个弹窗框,因为是列表所以高度自然又是不可控的,所以给了一个最大高度,当超出 ...

  7. 3.spring环境搭建

    1. 导入jar     1.1 四个核心包一个日志包(commons-logging)

  8. 微信开放平台创建android应用时怎么获取应用签名

    之前微信开放平台中申请创建应用,没有整理,过了好久,又重新百度,今天索性整理了,以供童鞋们备用. 1.微信开发平台注册申请成开发者账号,就此略过 2.在管理中心选择创建移动应用.按照严格要求填写.上传 ...

  9. top命令查看进程下线程信息以及jstack的使用

    转自:https://www.cnblogs.com/shengulong/p/8513652.html top -Hp pid可以查看某个进程的线程信息 -H 显示线程信息,-p指定pid jsta ...

  10. JS nodeList转数组,兼容IE低版本

    一.前言 nodeList转数组貌似很少会这样去操作,但我在做图片懒加载时,我获取了所有需要做懒加载的img元素,也就是一个NodeList对象,打个比方: 对这些元素进行src修改后,我想将此项从N ...