環境變量文件的分類
系統級環境變量:每一個登錄到系統的用戶都能夠讀取到系統級的環境變量。
用戶級環境變量:每一個登錄到系統的用戶只能夠讀取屬於自己的用戶級的環境變量。
一、系統級環境變量文件
- Centos 中:/etc/environment、 /etc/profile、 /etc/profile.d/*.sh、 /etc/bashrc
- Ubuntu 中:/etc/environment、 /etc/profile、 /etc/profile.d/*.sh、 /etc/bash.bashrc
- Debian 中:/etc/environment、 /etc/profile、 /etc/profile.d/*.sh、 /etc/bash.bashrc
二、用戶級環境變量文件
- Centos 中:~/.bashrc、 ~/.bash_profile
- Ubuntu 中:~/.bashrc、 ~/.profile
- Debian 中:~/.bashrc、 ~/.profile
環境變量文件的加載順序
一、正常登錄系統的時候
正常 就是直接輸入username+password ,login 的時候。不正常 就是當我們 在root用戶中 使用 su 命令 切換用戶 的時候。
- Centos 中:/etc/environment、 /etc/profile(包含/etc/profile.d/*.sh)、 ~/.bash_profile、 ~/.bashrc、 /etc/bashrc。
- Ubuntu 中:/etc/environment、 /etc/profile(包含/etc/profile.d/*.sh與/etc/bash.bashrc)、 ~/.profile、 ~/.bashrc。
- Debian 中:/etc/environment、 /etc/profile(包含/etc/profile.d/*.sh與/etc/bash.bashrc)、 ~/.profile、 ~/.bashrc。
二、非正常登錄系統的時候
非正常登錄的時候,是指在 root用戶 中 使用 su 命令 切換其他用戶,因為 系統在我們登錄root用戶 的時候,已經加載過一遍 環境變量的配置文件了,所以當你 切換用戶登錄系統的時候就不用重頭重新加載環境變量文件了!
- Centos 中:/etc/bashrc、 /etc/profile.d/*.sh、 /etc/profile.d/lang.sh。
- Ubuntu 中:/etc/bash.bashrc、 /etc/profile.d/*.sh、 /etc/profile.d/cedilla-portuguese.sh。
- Debian 中:/etc/bash.bashrc、 /etc/profile.d/*.sh。
環境變量設置文件
1、讀取環境變量的方法:
[root@localhost ~]# env # 顯示當前用戶定義的所有環境變量。 [root@localhost ~]# export # 顯示當前系統定義的所有環境變量。 [root@localhost ~]# echo $PATH # 輸出當前的PATH環境變量的值。
2、Linux 環境變量設置文件
- /etc/profile 全局用戶,應用於所有的Shell。
3、Linux 環境變量加載順序
- /etc/environment
4、/etc/profile>/etc/profile.d/*.sh>/etc/bashrc
Linux 環境變量配置方法
使用man命令:man:command not found
參考:https://www.cnblogs.com/ben-ben/archive/2012/11/13/2767588.html
# 用echo $PATH查看該環境變量。這種問題一般是環境變量PATH不對的問題。 [root@localhost ~]# echo $PATH # 用whereis命令查看,該指令的位置。 [root@localhost ~]# whereis man # 直接使用該地址,或者把該路徑加入PATH,添加PATH環境變量方法有兩種。 # 方法1:PATH 在終端關閉后就會消失。 [root@localhost ~]# export PATH=/usr/local/webserver/mysql/bin:$PATH # 方法2:通過編輯/etc/profile來改PATH,也可以修改家目錄下的.bashrc(即:~/.bashrc)。 [root@localhost ~]# vim /etc/profile [root@localhost ~]# export PATH="/usr/local/webserver/mysql/bin:$PATH" [root@localhost ~]# source /etc/profile
https://blog.csdn.net/ljlfather/article/details/105024401
https://www.cnblogs.com/youyoui/p/10680329.html
https://www.cnblogs.com/EasonJim/p/7137849.html
https://blog.csdn.net/pengjunlee/article/details/81585726
https://blog.csdn.net/qq_31877249/article/details/80607186