Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does sudo -H do?

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' 

So I used sudo to install 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.

What does sudo's -H flag do?

like image 571
Peter Tao Avatar asked Apr 26 '17 00:04

Peter Tao


People also ask

What does sudo do exactly?

Sudo (superuser do) is a utility for UNIX- and Linux-based systems that provides an efficient way to give specific users permission to use specific system commands at the root (most powerful) level of the system. Sudo also logs all commands and arguments.

What happens when you run sudo?

It runs whatever command you want to run as an administrator. It's often used to give you the privilege to edit system files (like /etc/hosts ) or to add directories to system directories and so on.

What sudo means in Linux?

sudo , which is an acronym for superuser do or substitute user do, is a command that runs an elevated prompt without a need to change your identity. Depending on your settings in the /etc/sudoers file, you can issue single commands as root or as another user.

Should I use sudo or root?

1 Answer. root is a user that has unlimited privileges. sudo is the act of temporary elevating privileges of a normal user, but designated as an admin, to the level of the root user.


1 Answers

Generally

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

-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.

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

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.

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.

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.

like image 155
user3141593 Avatar answered Sep 22 '22 07:09

user3141593