前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux profile1,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part1

Linux profile1,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part1

作者头像
授客
发布2019-09-12 19:09:42
1.5K0
发布2019-09-12 19:09:42
举报
文章被收录于专栏:授客的专栏授客的专栏

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

by:授客

(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. 当bash以login shell的方式启动时:

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

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

注意:

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-11-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档