macos 如何为root用户设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5240628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set environment variable for root user
提问by Ickhyun Kwon
I'm Mac user.
我是 Mac 用户。
I want to set PYTHONPATH env for root. so
我想为 root 设置 PYTHONPATH env。所以
$ sudo su -
# vi ~/.profile
and add to file 'export PYTHONPATH=/mypythonlib'
并添加到文件“export PYTHONPATH=/mypythonlib”
then
然后
# env
I can see this line
我可以看到这条线
PYTHONPATH=/Users/simpnet2/projects/meiji/src/hershey
but..
但..
when I use sudo command, cannot find that
当我使用 sudo 命令时,找不到
$ sudo env
.. there's no PYTHONPATH
.. 没有 PYTHONPATH
My program has to run with sudo command and needs PYTHONPATH
.
我的程序必须使用 sudo 命令和需要运行PYTHONPATH
。
采纳答案by Tabitha
If you use sh try /etc/profile
, bash try /etc/bashrc
and if you use zsh try /etc/zshenv
.
如果你使用 sh try /etc/profile
, bash try /etc/bashrc
,如果你使用 zsh try /etc/zshenv
。
回答by user1756254
You can make PYTHONPATH visible to sudo be editing your sudoers file. Notice you should ONLY do this through visudo as explained here.
您可以在编辑 sudoers 文件时使 sudo 可见 PYTHONPATH。请注意,您应该只通过 visudo 执行此操作,如此处所述。
回答by samplebias
You should try sudo -i
which will simulate logging in as root
and source the ~root/.profile
.
您应该尝试使用sudo -i
which 来模拟登录身份root
并获取~root/.profile
.
回答by Gerardo Costilla
As of 10.8.5, putting my environment statements in the .profile path in the home of the root user (/var/root) worked. after quitting bash and coming back to the root user prompt with 'su -', I could see my new path, etc. with the 'env' command andmy MacPorts installationw orking correctly.
从 10.8.5 开始,将我的环境语句放在 root 用户 (/var/root) 的主目录中的 .profile 路径中。在退出 bash 并使用“su -”返回到 root 用户提示符后,我可以使用“env”命令和我的 MacPorts 安装正确看到我的新路径等。
MacBook-Pro:~ root# cat /var/root/.profile export MANPATH=/opt/local/share/man:$MANPATH export PATH=/opt/local/bin:/opt/local/sbin:$PATH MacBook-Pro:~ root# which port /opt/local/bin/port
回答by ppppplv
Well, in other Linux system, it is also right that 'sudo' does not use local environment variable. But you can declare the temporary environment variable along with 'sudo' command.
那么,在其他Linux系统中,'sudo'不使用本地环境变量也是正确的。但是您可以与 'sudo' 命令一起声明临时环境变量。
For example, in your case, you can add 'PYTHONPATH=/mypythonlib' in your command 'sudo env', and the final command is:
例如,在您的情况下,您可以在命令“sudo env”中添加“PYTHONPATH=/mypythonlib”,最终命令为:
sudo PYTHONPATH=/mypythonlib env
You can also read this article: Using sudo. You can see how 'sudo' keep or ignore user-defined environment variables.
您还可以阅读这篇文章:使用 sudo。您可以看到“sudo”如何保留或忽略用户定义的环境变量。
回答by Thagomizer
In the case of logging in as a normal user and invoking "su - root" I found that Mac OS 10.8.5's bash was ignoring .profile and .bash_profile; I was unable to change root's $PATH by editing those files. What did work was editing /etc/paths. After exiting the root shell and entering again with "su - root" the new path was present.
在以普通用户身份登录并调用“su - root”的情况下,我发现 Mac OS 10.8.5 的 bash 忽略了 .profile 和 .bash_profile;我无法通过编辑这些文件来更改 root 的 $PATH。所做的工作是编辑 /etc/paths.conf 。退出 root shell 并使用“su - root”再次输入后,新路径出现了。