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. elasticsearch学习之根据发布时间设置衰减函数

    衰减函数decay functionion 高斯衰减 GET _search { "_source": ["title","release_date& ...

  2. Java学习笔记32(集合框架六:Map接口)

    Map接口与Collection不同: Collection中的集合元素是孤立的,可理解为单身,是一个一个存进去的,称为单列集合 Map中的集合元素是成对存在的,可理解为夫妻,是一对一对存进去的,称为 ...

  3. Docker 与 虚拟机比较

    1, 更高效的利用系统资源2,更快速的启动时间3,一致的运行环境4,持续交付(Continuous Integration)和部署(Continuous Delivery) 5, 更轻松的迁移 6,更 ...

  4. harbor镜像仓库-02-https访问配置

    harbor镜像仓库-02-https访问配置 harbordockerhttps harbor搭建部署参考上一章节 harbor镜像仓库-01-搭建部署 Harbor默认使用http,给harbor ...

  5. [EXP]CVE-2018-2628 Weblogic GetShell Exploit

    漏洞简介 漏洞威胁:RCE--远程代码执行 漏洞组件:weblogic 影响版本:10.3.6.0.12.1.3.0.12.2.1.2.12.2.1.3 代码: # -*- coding: utf-8 ...

  6. RPC的原理总结

    一.RPC的引入 早期单机时代,一台电脑上运行多个进程,大家各干各的,老死不相往来.假如A进程需要一个画图的功能,B进程也需要一个画图的功能,程序员就必须为两个进程都写一个画图的功能.这不是整人么?于 ...

  7. python列表常用方法

    list是一个类,用中括号括上,逗号分隔,元素可以是数字,字符,字符串,也可以是嵌套列表,布尔类型. 1.通过索引取值 li=[1,12,9,'age',['wangtianning',[19,'10 ...

  8. 在Hadoop集群上的HBase配置

    之前,我们已经在hadoop集群上配置了Hive,今天我们来配置下Hbase. 一.准备工作 1.ZooKeeper下载地址:http://archive.apache.org/dist/zookee ...

  9. centos下如何使用sendmail发送邮件

    最近在实施服务端日志监控脚本,需要对异常情况发送邮件通知相关责任人,记录下centos通过sendmail发送邮件的配置过程. 一.安装sendmail与mail 1.安装sendmail:  1) ...

  10. 可重入锁 & 不可重入锁

    可重入锁指同一个线程可以再次获得之前已经获得的锁,避免产生死锁. Java中的可重入锁:synchronized 和 java.util.concurrent.locks.ReentrantLock. ...