Ubuntu設定環境變數並立即生效(以Ubuntu12.04為例)
阿新 • • 發佈:2018-12-25
Ubuntu Linux系統包含兩類環境變數:系統環境變數和使用者環境變數。系統環境變數對所有系統使用者都有效,使用者環境變數僅僅對當前的使用者有效。
修改使用者環境變數
使用者環境變數通常被儲存在下面的檔案中:
-
~/.profile
-
~/.bash_profile 或者 ~./bash_login
-
~/.bashrc
上述檔案在Ubuntu 10.0以前版本不推薦使用。
系統環境變數
系統環境變數一般儲存在下面的檔案中:
-
/etc/environment
-
/etc/profile
-
/etc/bash.bashrc
/etc/profile和 /etc/bash.bashrc在Ubuntu 10.0版本中不推薦使用。
加入環境變數
如想將一個路徑加入到$PATH中,可以像下面這樣做(修改/etc/profile):
sudo vi /etc/profile
在裡面加入:
JAVA_HOME=/usr/jdk1.6.0_25
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
CLASSPATH=.:$JAVA_HOME/lib
export CLASSPATH
你可以自己加上指定的多個路徑,中間用冒號隔開。環境變數更改後,在使用者下次登陸時生效,如果想立刻生效,則可執行下面的語句:
source /etc/profile
需要注意的是,最好不要把當前路徑”./”放到PATH裡,這樣可能會受到意想不到的攻擊。
其他檔案的修改方式與此類似,需要注意的是/etc/environment不需要使用export設定環境變數,其他profile檔案需要。
更詳細的說明可以參考這裡。
當然如果想使用文字編輯工具修改環境變數,可以使用root許可權登入後,直接用文字編輯器開啟修改儲存
也可以普通使用者許可權下把檔案複製到桌面等修改後用root許可權覆蓋回去
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). if[ -d /etc/profile.d ]; then fori in/etc/profile.d/*.sh;do if[ -r $i ]; then . $i fi done JAVA_HOME=/usr/hadoop/jdk1.6.0_25 export JAVA_HOME PATH=$PATH:$JAVA_HOME/bin export PATH CLASSPATH=.:$JAVA_HOME/lib export CLASSPATH unseti fi if[ "$PS1"]; then if[ "$BASH"]; then # The file bash.bashrc already sets the default PS1. # PS1='\h:\w\$ ' if[ -f /etc/bash.bashrc ]; then ./etc/bash.bashrc fi else if[ "`id -u`" -eq0 ]; then PS1='# ' else PS1='$ ' fi fi fi