为什么 sudo 会改变 PATH? [英] Why does sudo change the PATH?
问题描述
这是没有 sudo 的 PATH
变量:
This is the PATH
variable without sudo:
$ echo 'echo $PATH' | sh
/opt/local/ruby/bin:/usr/bin:/bin
这是带有 sudo 的 PATH
变量:
This is the PATH
variable with sudo:
$ echo 'echo $PATH' | sudo sh
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin
据我所知,sudo
应该保持 PATH
不变.这是怎么回事?我该如何改变?(这是在 Ubuntu 8.04 上).
As far as I can tell, sudo
is supposed to leave PATH
untouched. What's going on? How do I change this? (This is on Ubuntu 8.04).
更新:据我所知,没有任何脚本以 root 更改 PATH
开始.
UPDATE: as far as I can see, none of the scripts started as root change PATH
in any way.
来自man sudo
:
为了防止命令欺骗,sudo检查."和"(都表示当前目录)最后搜索时对于用户路径中的命令(如果一个或两个都在 PATH 中).注意,然而,实际的 PATH环境变量未修改并原封不动地传递给程序sudo 执行.
To prevent command spoofing, sudo checks ``.'' and ``'' (both denoting current directory) last when searching for a command in the user's PATH (if one or both are in the PATH). Note, however, that the actual PATH environment variable is not modified and is passed unchanged to the program that sudo executes.
推荐答案
这是一个烦人的功能 sudo 在许多发行版上的一个特性.
要在 ubuntu 上解决这个问题",我会这样做以下在我的 ~/.bashrc
To work around this "problem" on ubuntu I do the following in my ~/.bashrc
alias sudo='sudo env PATH=$PATH'
请注意,上述内容适用于不会自行重置 $PATH 的命令.但是,`su' 会重置它的 $PATH,因此您必须使用 -p 来告诉它不要这样做.即:
Note the above will work for commands that don't reset the $PATH themselves. However `su' resets it's $PATH so you must use -p to tell it not to. I.E.:
sudo su -p
这篇关于为什么 sudo 会改变 PATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!