4

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.

  1. 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
  1. Place environment variable FOO under /etc/environment as:
FOO="TRUE"
  1. Restart computer for changes to take effect

  2. Open ubuntu and run echo $FOO. This displays nothing. If I switch the user to tono for example with su tono and then run the same command it works.

  3. I did some research and I change the line from Defaults env_reset to Defaults !env_reset on /etc/sudoers so that variables are not reset. Making this change and rebooting computer still does not work.

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

5
  • Am I understanding correctly that you have required that the default login is "root"? Know thee NOTHING OF UNIX? I would start by backing up about six steps and approach this some other way. I could solve this for you but I won't. DO NOT BE ROOT AS YOUR USER. PERIOD. Root is there to facilitate modifying permissions and settings so you don't NEED TO BE ROOT. Commented Nov 10, 2020 at 19:22
  • 2
    What is wrong with being root user if you do not have any confidential data or critical services? The probability of something failing because of permissions etc is much higher than someone trying to hack a system that has nothing confidential.
    – Tono Nam
    Commented Nov 10, 2020 at 19:32
  • What is wrong is that *nix is not designed to have root be a typical user. I used to have the same argument as you before I actually learned how everything worked. It isn't about getting your credit card data.. it is about both how applications are designed and the fact that one little mistake will take the whole system down. Running as root is a sign that you don't know what you are doing. good luck with that. Commented Nov 10, 2020 at 20:23
  • 1
    Root or not, I can confirm that running commands from wsl don't pick up on /etc/environment nor /etc/profile nor ~/.profile nor ~/.bashrc.
    – M-Pixel
    Commented Feb 21, 2021 at 3:09
  • no matter how bad the practise is to use root as a default user, this is an issue for sure.
    – ws_
    Commented Apr 17, 2021 at 14:34

2 Answers 2

1

Okay there are two questions. For environment variables, you can use WSLENV.

For default user, I'm new to wsl too and don't know how to help you. But my suggestion is don't rely on root.

Here are the example to use WSLENV:

#!pwsh
> $env:FOO="asdf"
> $env:WSLENV="FOO"
> wsl echo `$FOO
asdf

In this example i'm not persisting environment. If you want to make it persistant, use setx.exe or google for a GUI based solution.

reference: WSLENV:https://devblogs.microsoft.com/commandline/share-environment-vars-between-wsl-and-windows/

0

I placed all my environment variables under /env/my-env-variables.sh as

var1="true"
export var1

var2="false"
export var2

alias my_alias="echo hello-world"

# ...etc

now on ~/.bashrc I append the line at the end: source /env/my-env-variables.sh so that they work when I am using the shell.

Now every time I execute a script from windows I just have to remember to include those variables as:

wsl source /env/my-env-variables.sh ; echo $var1 
1
  • Some programs (e.g. CLion IDE) are hard-coded to run wsl somecommand, or even path/to/ubuntu####.exe run somecommand. This answer doesn't help those cases.
    – M-Pixel
    Commented Feb 21, 2021 at 3:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .