首先简单介绍下什么是登陆shell linux
用户登陆后,要启动一个进程,负责将用户的操做传给内核,这个进程是用户登陆到系统后运行的命令解释器或某个特定的程序,即Shell。Shell是用户与Linux系统之间的接口。Linux的Shell有许多种,每种都有不一样的特色。经常使用的有sh(BourneShell),csh(CShell),ksh(KornShell),tcsh(TENEX/TOPS-20typeCShell),bash(BourneAgainShell)等。系统管理员能够根据系统状况和用户习惯为用户指定某个Shell。若是不指定Shell,那么系统使用sh为默认的登陆Shell,即这个字段的值为/bin/sh。
用户的登陆Shell也能够指定为某个特定的程序(此程序不是一个命令解释器)。利用这一特色,咱们能够限制用户只能运行指定的应用程序,在该应用程序运行结束后,用户就自动退出了系统。有些Linux系统要求只有那些在系统中登记了的程序才能出如今这个字段中。
shell
查看当前的登录shell vim
echo $SHELL
在说说linux 中的 bash
先说明三个概念 测试
登陆shell spa
正常登陆程序启动的shell.既登陆成功后紧接着给登陆用户启动的shell. .net
非登陆交互式shell unix
这个shell的工做方式是交互式的,等用户输入,而后执行,再等用户输入。显然登陆shell就是一个交互式shell。 code
以下,咱们可得到一个交互式非登陆shell: blog
[root@localhost ~]# bash
[root@localhost ~]# pwd
/root
非交互式shell
为运行一个shell脚本启动的shell.
以FC5的bash为例,跟shell环境配置相关的文件如下几个,
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
~/.bash_profile
~/.bashrc
有时你会发现定义一个别名,有时好像在任意一个文件里定义均可以起做用,有时好像又不起做用,那是为何呢?这些配置文件各自互责了什么工做?相互的关系是怎么样的?跟前面介绍的不一样种类的shell的关系是如何的呢?下面对每一个文件单独进行说明。
/etc/profile
Linux规定,当启动一个登陆shell会执行这个脚本. 测试过程以下:
把LIST的定义加到/etc/profile文件的未尾并保存. 以下:
alias LIST='ls -l'
把全部其它shell配置文件或目录更名,这样系统就找不到那些shell脚本了,不会执行,重而避免其它配置文件的干扰。以下:
[root@localhost ~]# mkdir /etc/profile.bak
[root@localhost ~]# mv /etc/profile.d/* -t /etc/profile.bak/
[root@localhost ~]# mv /etc/bashrc /etc/bashrc.bak
[root@localhost ~]# mv ~/.bash_profile ~/.bash_profile.bak
[root@localhost ~]# mv ~/.bashrc ~/.bashrc.bak
交互式shell,并测试过程以下:
[root@localhost ~]# bash
bash-3.1# LIST
bash: LIST: command not found
bash-3.1# exit
exit
[root@localhost ~]#
显然启动一个普通交互式shell的时候, shell配置文件/etc/profile不起做用
非交互式shell, 测试过程以下:
为了验证先写一个测试脚本,以下:
#!/bin/bash
LIST
把这个脚本保存为t.sh并加下可执行权限:
[root@localhost ~]# chmod a x t.sh
[root@localhost ~]# ./t.sh
./t.sh: line 2: LIST: command not found
[root@localhost ~]#
显然启动一个非交互式shell时,shell配置文件/etc/profile不起做用
登陆shell,并测试过程以下:
Last login: Wed Nov 19 10:22:23 2008 from 192.168.0.97
-bash-3.1# LIST
total 160
drwxr-xr-x 2 root root 4096 Aug 14 12:24 Desktop
-rw-r--r-- 1 root root 3211 Nov 6 10:15 Session.vim
drwxr-xr-x 2 root root 4096 Nov 10 10:58 a
-rw-r--r-- 1 root root 126 Nov 12 12:42 a.txt
-rw-r--r-- 1 root root 261 Nov 6 15:23 a.zip
-rw-r--r-- 1 root root 157 Nov 6 15:23 aa.zip
-rw------- 1 root root 1054 Aug 14 11:59 anaconda-ks.cfg
-rw-r--r-- 1 root root 691 Nov 18 10:09 b.txt
-rw-r--r-- 1 root root 31671 Aug 14 11:58 install.log
-rw-r--r-- 1 root root 4155 Aug 14 11:50 install.log.syslog
-rw------- 1 root root 20310 Nov 17 13:51 mbox
drwxr-xr-x 2 root root 4096 Nov 17 17:22 shell
-rwxrwxrwx 1 root root 65 Nov 19 10:11 t.sh
drwxr-xr-x 14 root root 4096 Nov 5 15:34 test
-bash-3.1#
显然启动一个登陆shell时,shell配置文件/etc/profile会起做用
~/.bash_profile
这个文件跟/etc/profile起做用的时机是同样的,都是只在启动一个登陆shell的时候才会被source,跟/etc/profile不一样的是,这里的配置只影响单个用户,不对其它用户产生影响。
/etc/bashrc与~/.bashrc
从字面上咱们能够理解这两个文件应该跟根bash相关,即 只要你启动了一个bash类型的shell这两文件的配置就将发生做用。若是你的shell是sh、csh或是ksh这两个文件将不起做用。按前面的介 绍,可能很会猜想/etc/bashrc与~/.bashrc的关系跟/etc/profile与~/.bash_profile的关系同样,一个是全局 的,一个是针对单个用户的。从结果上看确实是这样的,但实现过程倒是不同的。启动一个bash时直接source ~/.bashrc, 而这~/.bashrc里面会source /etc/bashrc。
/etc/profile.d/*.sh
在fc5下这里的脚本会在/etc/profile里或是~/.bashrc里同时source, 因此这里的设置都是一些不一样分类的全局环境设置。
总结在FC5下一个登陆bash的环境初始全过程是:
/etc/profile
|
--/etc/profile.d/*
~/.bash_profile
|
--~/.bashrc
|
--/etc/bashrc
|
--/etc/profile.d/*
一个普通交互式bash的初始全过程是:
~/.bashrc
|
--/etc/bashrc
|
--/etc/profile.d/*
对于非交互式bash的初始全过程是: 不从新source 任何新的shell脚本,只继承当前shell的设置.