在linux中,shell一般分为两种:登陆shell(login shell)和非登陆shell(non-login shell)
login shell: 登陆shell是需要完整登陆流程才能获取的shell,需要输入用户名和密码。
通过tty终端或者ssh登陆,还有使用su -命令切换(su后面不加-取得的是non-login shell)用户都是取得的登 陆shell。
non-login shell:非登陆shell是在不需要重复输入用户名和密码的情况下取得的shell。
在shell下使用bash命令取得的子shell是非登陆shell,使用su(不添加-)获得的shell是非登陆shell。
一般来说可以使用ps -f来查看login shell和non-login shell。login shell 的程序名称一般是-bash,non-login shell是bash。
login shell和non-login shell的配置文件的区别
在获得shell之前,shell会读取关于shell的配置文件,login shell和non-login shell读取的文件是不一样的。
(具体可以参见man bash的INVOCATION部分)
login shell
在登陆系统之前,login shell首先会去读取所有用户的公共/etc/profile设定档。该文件中主要是配置一些重要的环境变量:
- PATH
- USER
- HOSTNAME
- HISTSIZE
/etc/inputrc:bash的按键功能
/etc/profile.d/*.sh:下面包含很多的.sh文件,用来设定bash颜色,ll是ls的别名等。
/etc/sysconfig/i81n:语系的设置文件
接着login shell会读取个人的设定档,位于个人主目录下:
- ~/.bash_profile
- ~/.bash_login
- ~/.profile
其实bash只会读取其中的一个文件,搜寻的顺序按照1,2,3.当读取到一个文件之后立即停止搜寻。
其中实现是表示实际的读取文件,虚线表示使用source或者点(.)导入当前环境中的文件。
non-login shell
non-login shell只会读取~/.bashrc文件。该文件会导入/etc/bashrc到当前环境下。
- 依据不同的UID设置umask的值
- 依据不同的UID设置PS1的值
- 导入 /etc/profile.d/*.sh
http://linux.vbird.org/linux_basic/0320bash.php#settings_bashrc
http://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell