首頁  >  文章  >  運維  >  linux的環境變數在哪裡

linux的環境變數在哪裡

WBOY
WBOY原創
2022-04-12 11:19:2314236瀏覽

linux環境變數有兩個位置:1、“/etc”目錄下的bashrc、profile和environment文件,文件內是已經配置好的環境變數;2、使用者目錄下的“.bashrc”和「.bash_profile」文件,這兩個文件都是隱藏文件。

linux的環境變數在哪裡

本教學操作環境:linux7.3系統、Dell G3電腦。

linux的環境變數在哪裡

1、系統位置,/etc目錄下的bashrc、profile、environment三個檔案

2、使用者目錄(root或普通使用者)下的.bashrc、.bash_profile兩個文件,注意這兩個文件前面都有點,是隱藏文件。

先說這3個系統文件,列出文件內容(參考CentOS7)。 /**/為新增註解

# /etc/bashrc
# System wide functions and aliases      /* 系统广泛功能(函数)和别名 */
# Environment stuff goes in /etc/profile /* 环境东西(环境变量)在/etc/profile文件中 */
/* 修改这个文件不是一个好主意,除非你知道自己在干什么。*/
/* 在/etc/profile.d/目录下创建一个自定义脚本会是修改环境变量的更好方法 */
/* 但是会阻止在将来更新时合并的需要 */
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging(合并) in future updates.
……………………………………………………………………
……………………………………………………………………
……………………………………………………………………
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
……………………………………………………………………
……………………………………………………………………
……………………………………………………………………

第三個文件environment檔案開啟是一個空文件,不表。

再說兩個使用者文件,貼上文件內容(參考CentOS7)/**/為新增註解

普通使用者:

linux的環境變數在哪裡

#root使用者

# .bashrc
# User specific aliases and functions      /* 用户指定的别名和函数(功能)*/
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# .bash_profile
# Get the aliases and functions            /* 获取别名和函数(功能)*/
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs    /* 用户指定环境变量和启动程序(linux开机启动程序放在这里?还没有试过) */
PATH=$PATH:$HOME/bin
export PATH

這幾個檔案的註釋已經說得很清楚了,bashrc檔案是使用者指定別名和函數(功能), bash_profile檔案是用來取得bashrc檔案中的別名和函數(功能),使用者也可以在此檔案中指定環境變數和開機啟動程式。在系統的bashrc、和bash_profile檔案明確說明 在/etc/profile.d/目錄下建立自訂腳本會是修改環境變數的更好方法。如圖:

linux的環境變數在哪裡

到這裡文章就結束了,但是在實際配置環境變數中發現,如果變數僅配置在系統設定檔中,以普通使用者登入系統終端中是有自己加入的環境變量,一旦su到root,終端機就沒有了先前配環境變數。解決方法就是在每個使用者的設定檔中都配置一次,這樣太麻煩。在/etc/profile.d/目錄下創建自訂腳本的方法無論你在一個終端中如何切換用戶,環境變數仍然存在。

相關推薦:《Linux影片教學

以上是linux的環境變數在哪裡的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn