I am using Ubuntu as my preferred Linux distro for WSL2. I want to create an environment variable that persist across all users.
I already tried lots of answers and none of them work. They work on a virtual machine but they do not work under WSL. For example I already tried every answer from this question https://askubuntu.com/questions/161924/how-do-i-set-persistent-environment-variables-for-root. My goal is to run from command prompt wsl echo $FOO
and it work just like wsl echo $USER
works..
Also wsl default user needs to be root.
Anyways here are the steps I am doing based on all the answers and research.
- Enable root user and every time I run ubuntu start it as root user
# First enable root user on Ubuntu by running this on ubuntu wsl:
sudo passwd root
# enter password etc...
# Then set that user as the default user. On command prompt run:
Ubuntu config --default-user root
- Place environment variable FOO under
/etc/environment
as:
FOO="TRUE"
Restart computer for changes to take effect
Open ubuntu and run
echo $FOO
. This displays nothing. If I switch the user to tono for example withsu tono
and then run the same command it works.I did some research and I change the line from
Defaults env_reset
toDefaults !env_reset
on/etc/sudoers
so that variables are not reset. Making this change and rebooting computer still does not work.I now modify file /root/.bashrc and add the environment variable in there. That works but only if I am inside the terminal. If from command prompt I run
wsl echo $FOO
it does not work.
In short my goal is to run the command wsl echo $FOO
and for it to return true just like wsl echo $USER
will work. Am I going to have to store the value of that variable in a file just because of how complicated it is to create a environment variable that persist for all users?
Edit
I am just going to create a folder /env with all environment variables in there. That way I will be able to run
wsl echo "hello $(cat /env/FOO) world"
Longer but it will work for sure
wsl
don't pick up on/etc/environment
nor/etc/profile
nor~/.profile
nor~/.bashrc
.