關於登陸shell的小知識

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







關於登陸linux時,/etc/profile、~/.bash_profile等幾個文件的執行過程。

在登陸Linux時要執行文件的過程以下:
在 剛登陸Linux時,首先啓動 /etc/profile 文件,而後再啓動用戶目錄下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中的其中一個,執行的順序爲:~/.bash_profile、 ~/.bash_login、 ~/.profile。若是 ~/.bash_profile文件存在的話,通常還會執行 ~/.bashrc文件。由於在 ~/.bash_profile文件中通常會有下面的代碼:

if [ -f ~/.bashrc ] ; then
 . ./bashrc
           fi
  ~/.bashrc中,通常還會有如下代碼:
if [ -f /etc/bashrc ] ; then
 . /etc/bashrc
fi

因此,~/.bashrc會調用 /etc/bashrc文件。最後,在退出shell時,還會執行 ~/.bash_logout文件。

執 行順序爲:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc ->/etc/bashrc -> ~/.bash_logout

關於各個文件的做用域,在網上找到了如下說明:
(1)/etc/profile: 此文件爲系統的每一個用戶設置環境信息,當用戶第一次登陸時,該文件被執行. 並從/etc/profile.d目錄的配置文件中搜集shell的設置。

(2)/etc/bashrc: 爲每個運行bash shell的用戶執行此文件.當bash shell被打開時,該文件被讀取。

(3)~/.bash_profile: 每一個用戶均可使用該文件輸入專用於本身使用的shell信息,當用戶登陸時,該文件僅僅執行一次!默認狀況下,他設置一些環境變量,執行用戶的.bashrc文件。

(4)~/.bashrc: 該文件包含專用於你的bash shell的bash信息,當登陸時以及每次打開新的shell時,該該文件被讀取。

(5)~/.bash_logout: 當每次退出系統(退出bash shell)時,執行該文件. 另外,/etc/profile中設定的變量(全局)的能夠做用於任何用戶,而~/.bashrc等中設定的變量(局部)只能繼承/etc /profile中的變量,他們是"父子"關係。

(6)~/.bash_profile 是交互式、login 方式進入 bash 運行的~/.bashrc 是交互式 non-login 方式進入 bash 運行的一般兩者設置大體相同,因此一般前者會調用後者。

我 作了個實驗,在/etc/profile,/etc/bashrc,~/.bashrc和~/.bash_profile文件的最後追加同一個變量分別賦 予不一樣的值,實驗結果代表變量最後的值爲~/.bash_profile裏的值。(4個文件都沒有修改其餘設置,都是安裝系統後的默認值。)
再有就是4個文件都追加一個值到同一個文件,開機後查看該文件內容的順序爲:
/etc/profile
~/.bash_profile
~/.bashrc
/etc/bashrc

----------------------
其餘文章:
redhat 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的設置.

相關文章
相關標籤/搜索