I have two Ubuntu VM's where one is running on VirtualBox (Ubuntu 18.04) and the other is on WSL 1 (Ubuntu 20.04).
#!/bin/bash
dir=~
echo $dir
When I run the above shell script without the sudo
, both VM's print my own /home/user
directory. But when run this with sudo
, VirtualBox instance prints /home/user
and the WSL instance prints /root
. I'm not sure why this happens and if there is any relevance to VM's.
I need this to print the /home/user
directory in the WSL instance even when running as sudo
. I achieved this by running this as sudo -E
but I don't know if that's the best method. Can someone explain why do I get different behaviours in different environments?
$HOME
/$HOME
, regardless of whether it's WSL or on my "pure Linux" 18.04 machine. On my 20.04 WSL instances and my 20.04 "real Linux" hosts, I see$HOME
//root
. Not sure what changed between the two releases to cause this (yet), though. – NotTheDr01ds Aug 21 '21 at 20:24SUDO_USER
as well but of course it won't be as "generic" as env keeping. – Tom Yan Aug 21 '21 at 20:25sudoers
options a couple of years ago. Thanks! – NotTheDr01ds Aug 21 '21 at 20:44sudoers
that causes this. Updating thesudoers
(viavisudo
) in the 18.04 versions to match theenv_keep
/env_reset
/always_set_home
/set_home
settings from 20.04 should smooth things out. – NotTheDr01ds Aug 21 '21 at 21:59