Ubuntu設置環境變量並立即生效
阿新 • • 發佈:2017-06-28
覆蓋 r12 編輯器 .com system sudo x11 var cells
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):
1 |
sudo nano /etc/profile
|
在裏面加入:
1 2 3 4 5 6 |
JAVA_HOME= /usr/jdk1 .6.0_25
export JAVA_HOME
PATH=$PATH:$JAVA_HOME /bin
export PATH
CLASSPATH=.:$JAVA_HOME /lib
export CLASSPATH
|
你可以自己加上指定的多個路徑,中間用冒號隔開。環境變量更改後,在用戶下次登陸時生效,如果想立刻生效,則可執行下面的語句:
1 |
$ source /etc/profile
|
需要註意的是,最好不要把當前路徑”./”放到PATH裏,這樣可能會受到意想不到的攻擊。
其他文件的修改方式與此類似,需要註意的是/etc/environment不需要使用export設置環境變量,其他profile文件需要。
更詳細的說明可以參考這裏。
當然如果想使用文本編輯工具修改環境變量,可以使用root權限登錄後,直接用文本編輯器打開修改保存
也可以普通用戶權限下把文件復制到桌面等修改後用root權限覆蓋回去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# /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
Ubuntu設置環境變量並立即生效