When I'm logged in as username
, why does
sudo ls ~
show me the contents of /home/username
rather than /root
? I thought that sudo
executes a command as root, in which case ~
would represent /root
.
more explanation:
Additionally, if I run the su
command, and then run ls ~
, I am shown contents of /root
. So I thought doing that was equivalent to sudo ls ~
.
As another example, some commands with a --user
flag will interpret the user to be root when run as sudo
. For example, pip install --user packagename
will install to a directory within /home/username
, while sudo pip install --user packagename
will install to a directory within /root
. Note that it is not advised to run the last command, I'm just using it to show the functionality I'm describing.