如何为root用户设置环境变量

发布于 2024-10-21 12:39:23 字数 457 浏览 2 评论 0 原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

请止步禁区 2024-10-28 12:39:23

如果你使用 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.

滿滿的愛 2024-10-28 12:39:23

您可以通过编辑 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.

不必你懂 2024-10-28 12:39:23

您应该尝试 sudo -i,它将模拟以 root 身份登录并获取 ~root/.profile

You should try sudo -i which will simulate logging in as root and source the ~root/.profile.

断念 2024-10-28 12:39:23

从 10.8.5 开始,将我的环境语句放入 root 用户 (/var/root) 的 home 的 .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

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.

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
悲凉≈ 2024-10-28 12:39:23

那么,在其他Linux系统中,'sudo'不使用本地环境变量也是正确的。但是您可以与“sudo”命令一起声明临时环境变量。

例如,在您的情况下,您可以在命令“sudo env”中添加“PYTHONPATH=/mypythonlib”,最终命令是:

sudo PYTHONPATH=/mypythonlib env

您还可以阅读这篇文章:使用 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:

sudo PYTHONPATH=/mypythonlib env

You can also read this article: Using sudo. You can see how 'sudo' keep or ignore user-defined environment variables.

生生漫 2024-10-28 12:39:23

在以普通用户身份登录并调用“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文