profile与bashrc的深入分析

上传人:第*** 文档编号:31315286 上传时间:2018-02-06 格式:DOC 页数:11 大小:47.50KB
返回 下载 相关 举报
profile与bashrc的深入分析_第1页
第1页 / 共11页
profile与bashrc的深入分析_第2页
第2页 / 共11页
profile与bashrc的深入分析_第3页
第3页 / 共11页
profile与bashrc的深入分析_第4页
第4页 / 共11页
profile与bashrc的深入分析_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《profile与bashrc的深入分析》由会员分享,可在线阅读,更多相关《profile与bashrc的深入分析(11页珍藏版)》请在金锄头文库上搜索。

1、/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行。并从/etc/profile.d 目录的配置文件中搜集 shell 的设置./etc/bashrc:为每一个运行 bash shell 的用户执行此文件.当 bash shell 被打开时,该文件被读取./.bash_profile:每个用户都可使用该文件输入专用于自己使用的 shell 信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc 文件./.bashrc:该文件包含专用于你的 bash shell 的 bash 信息,当登录时以及每次打开新的sh

2、ell 时,该该文件被读取./.bash_logout:当每次退出系统(退出 bash shell)时,执行该文件.另外,/etc/profile 中设定的变量(全局)的可以作用于任何用户,而/.bashrc等中设定的变量(局部)只能继承/etc/profile 中的变量,他们是父子关系. /.bash_profile 是交互式、login 方式进入 bash 运行的/.bashrc 是交互式 non-login 方式进入 bash 运行的通常二者设置大致相同,所以通常前者会调用后者。* 每次 bash 作为 login shell 启动时会执行.bash_profile。主要有(我所知道的)

3、有以下几种情形:a) 每次登录到服务器时默认启动的 shellb) “su -l USER”时进入的 shellc) “bash -login”进入的 shell* 每次 bash 作为普通的交互 shell(interactive shell)启动时会执行.bashrc常见的有:i) “su USER”进入的 shellii) 直接运行“bash”命令进入的 shell。* 注意1, 在 shell 脚本中“#!/usr/bin/bash”启动的 bash 并不执行.bashrc。因为这里的 bash 不是interactive shell。2, bash 作为 login shell(lo

4、gin bash)启动时并不执行.bashrc。虽然该shell 也是 interactive shell,但它不是普通的 shell。* 一般.bash_profile 里都会调用.bashrc尽管 login bash 启动时不会自动执行.bashrc,惯例上会在.bash_profile 中显式调用.bashrc。,-| if -f /.bashrc ; then| . /.bashrc| fi-.bash_profile 显示内容如下:# .bash_profile# Get the aliases and functionsif -f /.bashrc ; then. /.bashr

5、cfi# User specific environment and startup programsPATH=.:$PATH:$HOME/binBASH_ENV=$HOME/.bashrcUSERNAME=rootexport USERNAME BASH_ENV PATH用户登录后加载 profile 和 bashrc 的流程如下:1)/etc/profile-/etc/profile.d/*.sh2)$HOME/.bash_profile-$HOME/.bashrc-/etc/bashrc说明:bash 首先执行/etc/profile 脚本,/etc/profile 脚本先依次执行/et

6、c/profile.d/*.sh随后 bash 会执行用户主目录下的.bash_profile 脚本,.bash_profile 脚本会执行用户主目录下的.bashrc 脚本,而.bashrc 脚本会执行/etc/bashrc 脚本至此,所有的环境变量和初始化设定都已经加载完成.bash 随后调用 terminfo 和 inputrc,完成终端属性和键盘映射的设定.其中 PATH 这个变量特殊说明一下:如果是超级用户登录,在没有执行/etc/profile 之前,PATH 已经设定了下面的路径:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbi

7、n:/usr/bin如果是普通用户,PATH 在/etc/profile 执行之前设定了以下的路径:/usr/local/bin:/bin:/usr/bin这里要注意的是:在用户切换并加载变量,例如 su -,这时,如果用户自己切换自己,比如 root 用户再用 su - root 切换的话,加载的 PATH 和上面的不一样.准确的说,是不总是一样.所以,在/etc/profile 脚本中,做了如下的配置:if id -u = 0 ; thenpathmunge /sbinpathmunge /usr/sbinpathmunge /usr/local/sbinfi如果是超级用户登录,在/etc

8、/profile.d/krb5.sh 脚本中,在 PATH 变量搜索路径的最前面增加/usr/kerberos/sbin:/usr/kerberos/bin如果是普通用户登录,在/etc/profile.d/krb5.sh 脚本中,在 PATH 变量搜索路径的最前面增加/usr/kerberos/bin在/etc/profile 脚本中,会在 PATH 变量的最后增加/usr/X11R6/bin 目录在$HOME/.bash_profile 中,会在 PATH 变量的最后增加$HOME/bin 目录以 root 用户为例,最终的 PATH 会是这样(没有其它自定义的基础上)/usr/kerbe

9、ros/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin以 alice 用户(普通用户)为例/usr/kerberos/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/alice/bin下面是做的脚本解析:#strace -o su -e trace=open su - alice#grep open /etc/su|grep -v null|grep = 3|nl#打开的文件如下:1 open(/

10、etc/ld.so.cache, O_RDONLY) = 32 open(/lib/libcrypt.so.1, O_RDONLY) = 33 open(/lib/tls/libc.so.6, O_RDONLY) = 34 open(/usr/lib/locale/locale-archive, O_RDONLY|O_LARGEFILE) = 35 open(/etc/nsswitch.conf, O_RDONLY) = 36 open(/etc/ld.so.cache, O_RDONLY) = 37 open(/lib/libnss_files.so.2, O_RDONLY) = 38 op

11、en(/etc/passwd, O_RDONLY) = 39 open(/etc/shadow, O_RDONLY) = 310 open(/etc/group, O_RDONLY) = 311 open(/etc/ld.so.cache, O_RDONLY) = 312 open(/lib/libtermcap.so.2, O_RDONLY) = 313 open(/lib/libdl.so.2, O_RDONLY) = 314 open(/lib/tls/libc.so.6, O_RDONLY) = 315 open(/dev/tty, O_RDWR|O_NONBLOCK|O_LARGEF

12、ILE) = 316 open(/etc/mtab, O_RDONLY) = 3 17 open(/proc/meminfo, O_RDONLY) = 318 open(/etc/nsswitch.conf, O_RDONLY) = 319 open(/etc/ld.so.cache, O_RDONLY) = 320 open(/lib/libnss_files.so.2, O_RDONLY) = 321 open(/etc/passwd, O_RDONLY) = 322 open(/etc/profile, O_RDONLY|O_LARGEFILE) = 323 open(/etc/prof

13、ile.d/, O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 324 open(/etc/profile.d/colorls.sh, O_RDONLY|O_LARGEFILE) = 325 open(., O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 326 open(/etc/profile.d/glib2.sh, O_RDONLY|O_LARGEFILE) = 327 open(/etc/profile.d/gnome-ssh-askpass.sh, O_RDONLY|O_LARGEFILE)

14、= 328 open(/etc/profile.d/krb5.sh, O_RDONLY|O_LARGEFILE) = 329 open(/etc/profile.d/lang.sh, O_RDONLY|O_LARGEFILE) = 330 open(/etc/sysconfig/i18n, O_RDONLY|O_LARGEFILE) = 331 open(/usr/lib/locale/locale-archive, O_RDONLY|O_LARGEFILE) = 332 open(/usr/lib/gconv/gconv-modules.cache, O_RDONLY) = 333 open(/etc/profile.d/less.sh, O_RDONLY|O_LARGEFILE) = 334 open(/etc/profile.d/qt.sh, O_RDONLY|O_LARGEFILE) = 335 open(/etc/profile.d/vim.sh, O_RDONLY|O_LARGEFILE) = 336 open(/etc/profile.d/which-2.sh, O_RDONLY|O_LARGEFILE) = 337 open(/ceno/product/imeg/etc/profile, O_RDON

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号