一.關(guān)于linux配置文件
1.linux下主要有四個(gè)配置文件:/etc/profile 、/etc/bashrc 、/root/.bashrc 、/root/.bash_profile。
- ? /etc/profile 設(shè)置的是系統(tǒng)全局環(huán)境和登錄系統(tǒng)的一些配置,該配置對(duì)所有用戶生效;
- ? /etc/bashrc 是shell 全局自定義配置文件,主要用于自定義 shell,該配置對(duì)所有用戶的shell都生效;
- ? /root/.bashrc 用于單獨(dú)自定義root用戶的 bash,只對(duì)root用戶的bash生效,如果要使elk用戶生效,則需要配置/home/elk/.bashrc文件;
- ? /root/.bash_profile 用于單獨(dú)自定義root用戶的系統(tǒng)環(huán)境,只對(duì)root用戶生效,如果要使elk用戶生效,則需要配置/home/elk/.bash_profile。
2./etc/profile 、/etc/bashrc這兩個(gè)配置文件是全局配置文件,對(duì)所有用戶生效;/username/.bashrc 、/username/.bash_profile是局部
配置文件,只對(duì)單個(gè)用戶生效。
3.舉個(gè)最簡(jiǎn)單的例子:一個(gè)項(xiàng)目中需要你配置java環(huán)境,如果java環(huán)境只是elk用戶用到,其他用戶壓根用不到,此時(shí)就把java環(huán)境配置
在/home/elk/.bash_profile里;但是如果你這個(gè)項(xiàng)目是java web項(xiàng)目,所有的服務(wù)都是基于java環(huán)境的,此時(shí)你就需要把java配置
到/etc/profile里面,省得去每個(gè)用戶下面配置;如果elk用戶想對(duì)自己的一些常規(guī)操作設(shè)置別名,但是又不想影響別人,就可以把別名設(shè)
置在/home/elk/.bashrc里面;如果系統(tǒng)管理員想設(shè)置bash的代碼補(bǔ)全,bash的顏色,并且對(duì)所有的用戶生效,則需要配置
在/etc/bashrc里面
二.驗(yàn)證四個(gè)配置文件的加載順序
1.現(xiàn)在就衍生出一個(gè)問題,當(dāng)我們登錄系統(tǒng)或者系統(tǒng)開機(jī)時(shí),這四個(gè)配置文件的加載順序是怎樣的?
2.解題思路:在這四個(gè)配置文件的末尾追加一行"echo "this is ****"",然后登錄系統(tǒng),看echo輸出順序就知道四個(gè)配置文件的加載順序了。
3.現(xiàn)在開始證明,以root用戶為例:
#在每個(gè)配置文件末尾追加echo "this is *****"
[root@node5 ~]# echo "echo "this is /etc/profile"" >> /etc/profile
[root@node5 ~]# echo "echo "this is /etc/bashrc"" >> /etc/bashrc
[root@node5 ~]# echo "echo "this is /root/.bashrc"" >> /root/.bashrc
[root@node5 ~]# echo "echo "this is /root/.bash_profile"" >> /root/.bash_profile
#現(xiàn)在斷開連接,使用root用戶登錄系統(tǒng)
Connection closed.
Disconnected from remote host(node5) at 10:23:38.
Type `help' to learn how to use Xshell prompt.
[c:\~]$
Connecting to 192.168.110.184:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Dec 14 09:14:27 2020 from 192.168.110.1
this is /etc/profile
this is /etc/bashrc
this is /root/.bashrc
this is /root/.bash_profile
三.結(jié)論
從2.3步的輸出不難看出,當(dāng)?shù)卿浵到y(tǒng)或者新開啟一個(gè) ssh連接啟動(dòng) bash 進(jìn)程時(shí),這四個(gè)配置文件的加載順序如下:
- ? /etc/profile > /etc/bashrc > /root/.bashrc > /root/.bash_profile
|