一.關於linux配置文件
1.linux下主要有四個配置文件:/etc/profile 、/etc/bashrc 、/root/.bashrc 、/root/.bash_profile。
- /etc/profile 設置的是系統全局環境和登錄系統的一些配置,該配置對所有用戶生效;
- /etc/bashrc 是shell 全局自定義配置文件,主要用於自定義 shell,該配置對所有用戶的shell都生效;
- /root/.bashrc 用於單獨自定義root用戶的 bash,只對root用戶的bash生效,如果要使elk用戶生效,則需要配置/home/elk/.bashrc文件;
- /root/.bash_profile 用於單獨自定義root用戶的系統環境,只對root用戶生效,如果要使elk用戶生效,則需要配置/home/elk/.bash_profile。
2./etc/profile 、/etc/bashrc這兩個配置文件是全局配置文件,對所有用戶生效;/username/.bashrc 、/username/.bash_profile是局部
配置文件,只對單個用戶生效。
3.舉個最簡單的例子:一個項目中需要你配置java環境,如果java環境只是elk用戶用到,其他用戶壓根用不到,此時就把java環境配置
在/home/elk/.bash_profile里;但是如果你這個項目是java web項目,所有的服務都是基於java環境的,此時你就需要把java配置
到/etc/profile里面,省得去每個用戶下面配置;如果elk用戶想對自己的一些常規操作設置別名,但是又不想影響別人,就可以把別名設
置在/home/elk/.bashrc里面;如果系統管理員想設置bash的代碼補全,bash的顏色,並且對所有的用戶生效,則需要配置
在/etc/bashrc里面
二.驗證四個配置文件的加載順序
1.現在就衍生出一個問題,當我們登錄系統或者系統開機時,這四個配置文件的加載順序是怎樣的?
2.解題思路:在這四個配置文件的末尾追加一行"echo "this is ****"",然后登錄系統,看echo輸出順序就知道四個配置文件的加載順序了。
3.現在開始證明,以root用戶為例:
#在每個配置文件末尾追加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
#現在斷開連接,使用root用戶登錄系統
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
三.結論
從2.3步的輸出不難看出,當登錄系統或者新開啟一個 ssh連接啟動 bash 進程時,這四個配置文件的加載順序如下:
- /etc/profile > /etc/bashrc > /root/.bashrc > /root/.bash_profile