sudo -H 有什么作用? [英] What does sudo -H do?

查看:33
本文介绍了sudo -H 有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 pip 安装 virtualenv 后

After trying to install virtualenv with pip

$ pip install virtualenv

我收到一个权限被拒绝错误

I got a permission denied error

IOError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/virtualenv.py'

所以我用sudo来安装virtualenv

So I used sudo to install virtualenv

$ sudo pip install virtualenv

但随后出现了警告:

目录'/Users/petertao/Library/Caches/pip/http'或其父目录目录不属于当前用户,缓存已被禁用.请检查该目录的权限和所有者.如果使用 sudo 执行 pip,您可能需要 sudo 的 -H 标志.

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.

目录'/Users/petertao/Library/Caches/pip'或其父目录目录不归当前用户所有,并且缓存轮已被禁用.检查该目录的权限和所有者.如果使用 sudo 执行 pip,您可能需要 sudo 的 -H 标志.

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.

sudo 的 -H 标志有什么作用?

推荐答案

一般情况

man sudo(具体文字可能有所不同,但它会是相似的):

Generally

man sudo (the exact text may vary, but it will be similar):

-H

-H (HOME) 选项请求安全策略将 HOME 环境变量设置为密码数据库指定的目标用户(默认为 root)的主目录.根据政策,这可能是默认行为.

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.

那么为什么这甚至是一种选择?通常使用sudo"不会改变 $HOME 环境变量.

So why is this even an option? Normally using "sudo" does not change the $HOME environment variable.

例如:

 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

您可以看到,普通的 sudo 将我的用户从testuser"更改为root",但不会将 $HOME 设置为什么,而 sudo -H 还将变量从我的"主目录更改为 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.

pip 警告您它是以 root 用户身份执行的,并希望修改 $HOME 中的内容,该 $HOME 设置为/Users/petertao",该文件不属于 root(很可能是petertao"用户).警告表明 pip 使用 $HOME 缓存文件,但由于文件夹所有权差异而禁用了自己的缓存.

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.

当然,当以 root 身份执行时,pip 可以修改 '/Users/petertao/Library/Caches/pip' 因为 root(几乎)是全能的.这在以后会变得很麻烦,因为在没有 root 的情况下运行的程序无法再覆盖或修改这些文件.相反,pip 拒绝写入另一个用户拥有的目录.

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.

这篇关于sudo -H 有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆