如何永久更改 sudo 的 $PATH 变量(Ubuntu 9.x)
我想在运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 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
toenv_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 replacePATH
when sudo is run:Defaults secure_path="/bin:/usr/bin"
That way you can control what specific directories to include in the path.
我认为如果你将它保存在 /root/.bashrc 中应该可以解决:
我忘记它是 PATH 还是 PYTHONPATH,如果它真的很重要,这是基于我用户的 .bashrc:
I think this should work out if you save it in /root/.bashrc:
I forget if it's PATH or PYTHONPATH and if it actually matters, this is based on my user's .bashrc:
您可以在 /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).