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中). 注意, 但是,实际的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屋!