linux 用户启动后加载文件

来源:互联网 发布:四川网络电视台 编辑:程序博客网 时间:2024/06/10 17:43

shell会涉及两个参数,交互/非交互;登录/分登陆;他们是可以交叉组合的,比如你是交互,可以登陆,也可以不登录...


由于他们的关系可以相互组合,为了清楚以下先针对单独情况讨论。


non-interactive 非交互用户 默认无输入输出。一般是由程序产生的。命令需指定fullpath。(A调用B,简写成A-->B)
非交互可指定登陆,可以指定输入输出。不会加载~/.bashrc,而会加载 bash_env /etc/bashrc --> /etc/profile.d/*sh

interactive 交互用户 带输入输出的。交互系统会加载prompt变量,如PS1(也就是你的command前缀)。如要求使用interactive环境,可用[ -z "$PS1" ] && return 进行校验。
使用交互的情况,即平常的shell交互界面,如 Xshell虚拟终端,或Gnome可视环境下的终端(可以登陆或不登陆),默认会读取~/.bashrc-->/etc/bashrc --> /etc/profile.d/*sh

non-login 非登陆用户。在gome下,你可以打开一个非登陆的终端,或者在su用户的时候不加 "-" ,他们都属于交互式非登陆bash。
非登陆不会读取带profile的文件,如/etc/profile或~/.bash_profile,但会读取~/.bashrc-->/etc/bashrc --> /etc/profile.d/*sh


login 登陆用户 这可能最常见的,需要验证登陆的用户,也是加载最全的 
/etc/profile, ~/.profile, ~/.bash_login 和 ~/.bash_profile -->~/.bashrc -->/etc/bashrc
登出时 ~/.bash_logout


为了方便理解,在此以对比的方法进行区别,如有疑问欢迎指出讨论

加载比较
登录vs非登陆,多加载一些profile:/etc/profile,~/.profile, ~/.bash_(login/logout)
交互vs非交互,多加载一个bashrc:~/.bashrc


两种文件的类型
观察下加载文件一般是bash和profile的各种组合,那么问题来了,profile和bashrc是干啥的?
bashrc rc=resource,bashrc就是bash专用rc,如果其他版本的shell就调用其他的,如cshrc
profile 用户设置环境变量的地方,你可以有多个shell(bash,zsh,csh)但是profile是跟用户的


调用关系 
~/.bash_profile --> ~/.bashrc
~/.bashrc --> /etc/bashrc

/etc/bashrc --> /etc/profile.d/*sh

/etc/profile --> /etc/profile.d/*sh


而A能调用B,那么说明A的加载内容多于B,即执行A的要求更高
对于~/.bash_profile --> ~/.bashrc
~/.bash_profile 需要登陆,不登录执行的是  ~/.bashrc
对于~/.bashrc --> /etc/bashrc

~/.bashrc 需要交互,非交互式执行的是 /etc/bashrc

对于/etc/profile.d/*sh,每个环境最终都会调用执行

进入后可以看到,是一些以基础命令.sh的环境变量



系统区别
以上均基于REHL,对于debian会有区别,已知的是:
/etc/bashrc(RHEL系)=/etc/bash.bashrc (debian系) 
/etc/bashrc  调用 /etc/profile.d/*sh (RHEL系)
/etc/profile 调用 /etc/bashrc (debian系)


因此,更改配置更推荐写入~/.bashrc,登陆用户会在~/.bash_profile中读取 ~/.bashrc,同时保证non-login也能加载,而不是加在/etc/profile

参考
http://www.cnblogs.com/qcly/p/3273373.html
https://wido.me/sunteya/understand-bashrc-and-profile

原创粉丝点击