如何为root用户设置环境变量
我是Mac用户。
我想为 root 设置 PYTHONPATH env 。所以
$ sudo su -
# vi ~/.profile
并添加到文件'export PYTHONPATH=/mypythonlib'
然后
# env
我可以看到这一行
PYTHONPATH=/Users/simpnet2/projects/meiji/src/hershey
但是..
当我使用sudo命令时,找不到
$ sudo env
..没有PYTHONPATH
我的程序必须使用sudo命令运行并且需要PYTHONPATH< /代码>。
I'm Mac user.
I want to set PYTHONPATH env for root. so
$ sudo su -
# vi ~/.profile
and add to file '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 env
.. there's no PYTHONPATH
My program has to run with sudo command and needs PYTHONPATH
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果你使用 sh 尝试
/etc/profile
,bash 尝试/etc/bashrc
,如果你使用 zsh 尝试/etc/zshenv
。If you use sh try
/etc/profile
, bash try/etc/bashrc
and if you use zsh try/etc/zshenv
.您可以通过编辑 sudoers 文件使 PYTHONPATH 对 sudo 可见。请注意,您只能通过 visudo 如此处所述。
You can make PYTHONPATH visible to sudo be editing your sudoers file. Notice you should ONLY do this through visudo as explained here.
您应该尝试
sudo -i
,它将模拟以root
身份登录并获取~root/.profile
。You should try
sudo -i
which will simulate logging in asroot
and source the~root/.profile
.从 10.8.5 开始,将我的环境语句放入 root 用户 (/var/root) 的 home 的 .profile 路径中是有效的。退出 bash 并使用“su -”返回 root 用户提示符后,我可以使用“env”命令看到我的新路径等和我的 MacPorts 安装工作正常。
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 and my MacPorts installationw orking correctly.
那么,在其他Linux系统中,'sudo'不使用本地环境变量也是正确的。但是您可以与“sudo”命令一起声明临时环境变量。
例如,在您的情况下,您可以在命令“sudo env”中添加“PYTHONPATH=/mypythonlib”,最终命令是:
您还可以阅读这篇文章:使用 sudo。您可以看到“sudo”如何保留或忽略用户定义的环境变量。
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.
For example, in your case, you can add 'PYTHONPATH=/mypythonlib' in your command 'sudo env', and the final command is:
You can also read this article: Using sudo. You can see how 'sudo' keep or ignore user-defined environment variables.
在以普通用户身份登录并调用“su - root”的情况下,我发现Mac OS 10.8.5的bash忽略了.profile和.bash_profile;我无法通过编辑这些文件来更改 root 的 $PATH。有效的是编辑/etc/paths。退出 root shell 并再次输入“su - root”后,新路径就出现了。
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.