Python sudo -H 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43623025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does sudo -H do?
提问by Peter Tao
After trying to install virtualenv with pip
尝试使用 pip 安装 virtualenv 后
$ pip install virtualenv
I got a permission denied error
我收到权限被拒绝错误
IOError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/virtualenv.py'
So I used sudo to install virtualenv
所以我用 sudo 来安装 virtualenv
$ sudo pip install virtualenv
But then a warning showed up:
但随后出现了警告:
The directory '/Users/petertao/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/petertao/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
目录“/Users/peterao/Library/Caches/pip/http”或其父目录不属于当前用户所有,缓存已被禁用。请检查该目录的权限和所有者。如果使用 sudo 执行 pip,您可能需要 sudo 的 -H 标志。
目录“/Users/petertao/Library/Caches/pip”或其父目录不属于当前用户所有,并且缓存轮已被禁用。检查该目录的权限和所有者。如果使用 sudo 执行 pip,您可能需要 sudo 的 -H 标志。
What does sudo's -H flag do?
sudo 的 -H 标志有什么作用?
回答by user3141593
Generally
一般来说
man sudo
(the exact text may vary, but it will be similar):
man sudo
(确切的文字可能会有所不同,但应该是相似的):
-H
The -H (HOME) option requests that the security policy set the HOME environment variable to the home directory of the target user (root by default) as specified by the password database. Depending on the policy, this may be the default behavior.
-H
-H (HOME) 选项请求安全策略将 HOME 环境变量设置为密码数据库指定的目标用户(默认为 root)的主目录。根据策略,这可能是默认行为。
So why is this even an option? Normally using "sudo" does not change the $HOME environment variable.
那么为什么这甚至是一种选择? 通常使用“sudo”不会改变 $HOME 环境变量。
for example:
例如:
echo $HOME $USER
/home/testuser testuser
sudo bash -c 'echo $HOME $USER'
/home/testuser root
sudo -H bash -c 'echo $HOME $USER'
/home/root root
You can see that a normal sudo changes which user I am from "testuser" to "root", but not what $HOME is set to, while a sudo -H also changes the variable from "my" home directory to root's home directory.
您可以看到,普通 sudo 将我的用户从“testuser”更改为“root”,但不会将 $HOME 设置为什么,而 sudo -H 还将变量从“my”主目录更改为 root 的主目录。
In your Case
在你的情况下
pip is warning you that it was executed as the user root and wanted to modify things in $HOME, which was set to '/Users/petertao', which is not owned by root (most likely the "petertao" user). the warning indicates that pip uses $HOME to cache files, but has disabled its own caching because of the folder ownership discrepancy.
pip 警告您它是作为 root 用户执行的,并希望修改 $HOME 中的内容,该 $HOME 设置为“/Users/petertao”,该文件不属于 root(很可能是“petertao”用户)。该警告表明 pip 使用 $HOME 缓存文件,但由于文件夹所有权差异而禁用了自己的缓存。
Of course while executing as root pip can modify '/Users/petertao/Library/Caches/pip' because root is (almost) almighty. This can become troublesome later because a program running without root could no longer overwrite or modify these files. Instead pip refuses to write to a directory owned by another user.
当然,在以 root 身份执行时,pip 可以修改 '/Users/petertao/Library/Caches/pip' 因为 root(几乎)是全能的。这在以后会变得很麻烦,因为在没有 root 的情况下运行的程序无法再覆盖或修改这些文件。相反,pip 拒绝写入另一个用户拥有的目录。
回答by raspberrypi-guy
-h flag usually means human readable format Its a convenient built in conversion in a Linux OS. for example if you went to your command terminal in Linux in my case a raspberry pi and typed df. This will show you your used and available memory. It will show something like you have 448336 bytes. Your probably like huh that's confusing. Now type in df -h (using the -h flag) you should get a result like this 448M instead of 4488336 bytes.
-h 标志通常表示人类可读的格式,它是 Linux 操作系统中一种方便的内置转换。例如,如果你在 Linux 中进入你的命令终端,在我的情况下是树莓派并输入 df。这将显示您的已用和可用内存。它将显示类似于您有 448336 个字节的内容。你可能喜欢啊,这很令人困惑。现在输入 df -h (使用 -h 标志)你应该得到这样的结果 448M 而不是 4488336 字节。