Linux profile1,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part1
profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 1
by:授客 QQ:1033553122
(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不读取上面的任意文件
Linux profile1,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part1的更多相关文章
- 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 ...
- Linux环境变量文件environment, profile, bashrc含义
转自:http://www.th7.cn/system/lin/201508/127503.shtml (1)/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件 ...
- profile bashrc bash_profile之间的区别和联系
profile bashrc bash_profile之间的区别和联系 博客分类: Linux 执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bas ...
- profile bashrc bash_profile 之间的区别和联系
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...
- profile, bashrc, source, setup.*sh
一. source: 命令是使该文件立刻执行,这样刚才做的修改就可以立即生效了,否则要重新启动系统修改才能生效.(执行其后命令使之立即生效,不用重启) 二.bashrc: 1.linux系统:/etc ...
- /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 ...
- Linux下文件 ~/.bashrc 和 ~/.bash_profile 和 /etc/bashrc 和 /etc/profile 的区别 | 用户登录后加载配置文件的顺序
转自 https://blog.csdn.net/secondjanuary/article/details/9206151 文件说明: /ect/profile 此文件为系统的每个用户设置环境信息, ...
- linux /etc/profile bashrc bash_profile
文件: /etc/profile ~/.bashrc 和 ~/.bash_profile 的使用区别: /etc/profile: 全局 环境变量等,在机器重启后执行一次, 用于设置环境变量,更 ...
- 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, ...
随机推荐
- python中使用queue实现约瑟夫环(约瑟夫问题)求解
约瑟夫问题:是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围. 从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列: 依 ...
- redis epoll 原理梗概
redis 是一个单线程却性能非常好的内存数据库, 主要用来作为缓存系统. redis 采用网络IO多路复用技术来保证在多连接的时候, 系统的高吞吐量.为什么 Redis 中要使用 I/O 多路复用这 ...
- js学习笔记----JavaScript中DOM扩展的那些事
什么都不说,先上总结的图~ Selectors API(选择符API) querySelector()方法 接收一个css选择符,返回与该模式匹配的第一个元素,如果没有找到匹配的元素,返回null ...
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- Gradle 大杂烩
1. 什么是Gradle Gradle是一个项目构建工具,目前支持Java.Groovy.Kotlin.Scala.构建脚本使用Groovy或Kotlin,目前一般用Groovy. 2. Gradle ...
- 从零开始学 Web 之 DOM(六)为元素绑定与解绑事件
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... +-------------------------------------------------------- ...
- Vue + Element UI 实现权限管理系统 前端篇(十六):系统备份还原
系统备份还原 在很多时候,我们需要系统数据进行备份还原.我们这里就使用MySql的备份还原命令实现系统备份还原的功能. 后台接口准备 系统备份还原是对数据库的备份还原,所以必须有后台接口的支持,我们准 ...
- Go 环境变量相关操作
Go语言中os包提供了一些环境变量的操作封装.包括: 设置环境变量:Setenv 获取环境变量:Getenv 删除指定的环境变量:Unsetenv 获取所有环境变量:Environ 清除所有环境变量: ...
- 深度学习论文翻译解析(四):Faster R-CNN: Down the rabbit hole of modern object detection
论文标题:Faster R-CNN: Down the rabbit hole of modern object detection 论文作者:Zhi Tian , Weilin Huang, Ton ...
- 微信小程序https配置
先简单说下什么是https,https与http区别 ,以及https的原理 什么是https 在说HTTPS之前先说说什么是HTTP,HTTP就是我们平时浏览网页时候使用的一种协议.HTTP协议传输 ...