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 nano /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 for i 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 unset i 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`" -eq 0 ]; then PS1='# ' else PS1='$ ' fi fi fi
轉自:http://snowfigure.diandian.com/post/2012-09-23/40038540721