Ubuntu环境变量在root下无效

来源:互联网 发布:看舌头知健康 编辑:程序博客网 时间:2024/05/22 13:58

Ubuntu 上配置环境变量后切换到root下环境变量无效 的解决方法。

原因:非root  和 root 下,环境变量的配置文件不同。

非root下,配置文件为:~/.bashrc  ,配置后执行更新: source ~/.bashrc

root下,配置文件为:/etc/profile  ,配置后执行更新:  source /etc/profile

解决:打开 ~/.bashrc ,把里面相应的配置变量(环境路径),复制到  /etc/profile ,然后更新即可。

但还有一个问题,关闭终端后,下次打开又无效了。解决方法:

解决方法:打开 gedit /root/.bashrc   ,在文件的末尾添加:

      source /etc/profile

                 然后执行更新:source /root/.bashrc

就搞定啦。


以下为博主遇到的实际问题:

在Ubuntu14.04上(非root)安装了Anaconda,在非root下,可以正常使用配置的环境,可以使用Anaconda自带的Python。

但是进入root下,却用不了Anaconda自带的Python。始终用的是系统默认的Python2.7 。

然后进入root下重新安装Anaconda,还是一样无效。输入 python ,打开的还是系统默认的python2.7 。


查找后发现,是root下的环境变量配置文件不同。

非root用户下,环境变量配置文件为 ~/.bashrc , 打开 gedit ~/.bashrc ,在最下面发现添加的环境变量:

# added by Anaconda3 4.2.0 installer
export PATH="/home/ying/anaconda3/bin:$PATH"

修改后,输入 source ~/.bashrc  更新

root下,环境变量配置文件为 /etc/profile ,打开 gedit /etc/profile ,发现里面没有定义Anaconda的路径 ,所以在root下用不了Anaconda

解决方法:在 /etc/profile 的最后添加 export PATH="/home/ying/anaconda3/bin:$PATH"   (也就是非root下的环境变量,直接复制过来)

  然后更新: source /etc/profile

                 搞定

但又发现了一个问题,关闭终端后,再次打开,又无效了。

解决方法:打开 gedit /root/.bashrc   ,在文件的末尾添加:

      source /etc/profile

                 然后执行更新:source /root/.bashrc

就搞定啦。


参考以下博文:http://blog.csdn.net/vblittleboy/article/details/36892621




原创粉丝点击