1. 程式人生 > >linux下/etc/profile /etc/bashrc /root/.bashrc /root/.bash_profile這四個配置檔案的載入順序

linux下/etc/profile /etc/bashrc /root/.bashrc /root/.bash_profile這四個配置檔案的載入順序

[TOC] # 一.關於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使用者為例: ```shell #在每個配置檔案末尾追加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