环境:
root@kylin-vm:/usr/local/unixODBC# cat /etc/lsb-release
DISTRIB_ID=Kylin
DISTRIB_RELEASE=4.0.2
DISTRIB_CODENAME=juniper
DISTRIB_DESCRIPTION=“Kylin 4.0.2”
DISTRIB_KYLIN_RELEASE=4.0-2SP2
DISTRIB_VERSION_TYPE=community
DISTRIB_VERSION_MODE=normal
问题描述:
正常的设置全局的环境变量的是通过在 /etc/profile 文件最后面,添加 export myEnvVar=12345 的方式增加变量。 但是我重启后没有生效,需要手动执行source /etc/profile 或者 切换到root,然后在切换回来才生效。
步骤:
① /etc/profile 文件后面,增加 export myEnvVar=12345
② 测试 source /etc/profile ,环境变量生效 echo $myEnvVar 能输出123456
问题:
重启计算机,非root用户登录,环境变量无效, echo $myEnvVar 没得反应。 只有重新加载才得行 source /etc/profile
想要的答案:
还有什么方式能够使得环境变量生效?或者在银河麒麟下正确的设置环境变量的方法是什么?
说明:
1、下面方法只能解决,当前登录的用户能够能够加载成功环境变量。
2、一旦切换su root环境变量失效, 在从su root切换回去,原本生效的环境变量,无效了。 (为什么登录的时候,在登录只加载了一次 /etc/profile , 切换用户的时候,没有执行这个 )
# 在目录下新增一个shell文件 ,把环境变量写进去
/etc/profile.d
# 新增sh
touch my.sh
#
vim my.sh
# 内容如下
export unixODBC_HOME="/usr/local/unixODBC"
export PATH="$unixODBC_HOME/bin:$PATH"
为什么可以这样写,是因为, /etc/profile 中是这样描述的。
if [ "$PS1" ]; then
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; 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
# 读取路径下shell,并执行。
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i