首先簡單介紹下什麼是登陸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的設置.