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. Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!

    前几天写了一篇 Java 8 即将在 2019 年停止免费向企业提供更新的文章,企图迫使用户向更新一代的 Java 版本升级,但让人遗憾的是,小编今天收到了 Oracle Java 版本的升级推送,装 ...

  2. C# 线程中使用delegate对控件进行操作

    如果在线程中想改变控件的值是不可以的,会报出以下错误. 那么,如何在线程中改变控件上的值呢?第一个想到的就是委托. 委托定义:委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行 ...

  3. mysql的binlog进行数据恢复

    什么是binlog? binlog,也称为二进制日志,记录对数据发生或潜在发生更改的SQL语句,并以二进制的形式保存在磁盘中,可以用来查看数据库的变更历史(具体的时间点所有的SQL操作).数据库增量备 ...

  4. logrotate实现Mysql慢日志分割

    MySQL慢日志? MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询 ...

  5. 第六章:四大组件之Activity

    tivityActivity作为Android四大组件之一,也是其中最重要的一个组件.作为一个与用户交互的组件,我们可以把Activity比较成为windows系统上的一个文件夹窗口,是一个与用户交互 ...

  6. 全网最详细的IDEA、Eclipse和MyEclipse之间于Java web项目发布到Tomcat上运行成功的对比事宜【博主强烈推荐】【适合普通的还是Maven方式创建的】(图文详解)

    不多说,直接上干货! IDEA [适合公司业务]全网最详细的IDEA里如何正确新建[普通或者Maven]的Java web项目并发布到Tomcat上运行成功[博主强烈推荐](类似eclipse里同一个 ...

  7. Percona 数据库

    1. Percona介绍 Percona Server由领先的MySQL咨询公司Percona发布. Percona Server是一款独立的数据库产品,其可以完全与MySQL兼容,可以在不更改代码的 ...

  8. IDEA之Git分支以及Stash使用

    访问我的博客 随着公司开发人员的增加,以及多需求的并行开发,功能上线就会碍手碍脚:害怕自己没写完的代码被别人部署到线上,害怕别人代码没写完被自己部署到线上:总之功能上线之前还要和所有开发沟通,能不能部 ...

  9. [PKUWC 2018]随机游走

    Description 题库链接 给定一棵 \(n\) 个结点的树,你从点 \(x\) 出发,每次等概率随机选择一条与所在点相邻的边走过去. 有 \(Q\) 次询问,每次询问给定一个集合 \(S\) ...

  10. [Luogu 3613] 睡觉困难综合征

    Description 给定一棵 \(n\) 个点的树,每个点上有位运算 \(opt\) 和一个权值 \(x\),位运算有 &,|,^ 三种. 要求支持: 修改点 \(v\) 的 \(opt\ ...