如何永久更改 sudo 的 $PATH 变量(Ubuntu 9.x)

发布于 2024-08-06 04:12:24 字数 230 浏览 4 评论 0原文

我想在运行 sudo 时向 $PATH 添加一些目录,这是一个(半)永久要求,而不是需要添加到脚本本身的东西。我注意到 Django 已经设法做到了这一点,(运行 sudo 时我的 $PATH 是“/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/ usr/X11R6/bin:/django/django-trunk/django/bin") - 那么它是如何做到的呢?

I want add some directory to the $PATH when running sudo, this is a (semi) permanent requirement, not something that needs to be added to the scripts themselves. I notice that Django has managed to do it, (my $PATH when running sudo is "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/django/django-trunk/django/bin") - so how did it do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

赴月观长安 2024-08-13 04:12:24

这是 sudoers 文件中重置的行:

Defaults env_reset

您可以通过将 PATH 添加到 env_keeps 或添加以下行来解决此问题:

默认 env_keep = "PATH"

编辑:meder,您不会禁用 env_reset,您只需绕过路径重置

或者您可以删除有问题的 env_reset 行。

更好的是,您可以声明一个 secure_path ,它将在 sudo 运行时替换 PATH

Defaults secure_path="/bin:/usr/bin" >

这样您就可以控制路径中包含哪些特定目录。

This is the line in the sudoers file that resets:

Defaults env_reset

You can work around this by adding PATH to env_keeps or by adding this line:

Defaults env_keep = "PATH"

EDIT: meder, you do not disable env_reset, you simply bypass the path reset

Or you can remove the offending env_reset line.

Even better though, you can declare a secure_path that will replace PATH when sudo is run:

Defaults secure_path="/bin:/usr/bin"

That way you can control what specific directories to include in the path.

晌融 2024-08-13 04:12:24

我认为如果你将它保存在 /root/.bashrc 中应该可以解决:

export PATH=/www/foo:$PATH

我忘记它是 PATH 还是 PYTHONPATH,如果它真的很重要,这是基于我用户的 .bashrc:

export PYTHONPATH=/www/django:$PYTHONPATH

I think this should work out if you save it in /root/.bashrc:

export PATH=/www/foo:$PATH

I forget if it's PATH or PYTHONPATH and if it actually matters, this is based on my user's .bashrc:

export PYTHONPATH=/www/django:$PYTHONPATH
一念一轮回 2024-08-13 04:12:24

您可以在 /etc/environment 中设置变量,然后使用“sudo -i”运行脚本(适用于 ubuntu 10.10)。

You can set the variable in /etc/environment, and then use "sudo -i" to run the script (works in ubuntu 10.10).

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