WSL icon indicating copy to clipboard operation
WSL copied to clipboard

bash not picking up /etc/environment

Open stringhaml opened this issue 9 years ago • 37 comments

When setting up http_proxy="http://my.proxyserver.example.com" and other such environment variables in /etc/environment they are not exported when opening a new bash session.

for example: user@mycomputer:~$ cat /etc/environment PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" ftp_proxy="http://my.proxyserver.example.com:1234" http_proxy="http://my.proxyserver.example.com:1234" https_proxy="http://my.proxyserver.example.com:1234" no_proxy=example.com,*.example.com,10.0.0.0/8,192.168.0.0/16,127.0.0.0/8,localhost" socks_proxy="http://my.proxyserver.example.com:1235"

exit bash, re-open, and do a printenv they are not listed.

stringhaml avatar Nov 21 '16 17:11 stringhaml

Thanks @stringhaml for posting! I believe this is conceptually the same issue as #816 ; it probably shares the same workaround.

WSL team, any further thoughts on that issue? I feel like I recall a bunch of discussion about it somewhere on one of these tickets prior to the Anniversary release, but I haven't heard much since.

aseering avatar Nov 21 '16 21:11 aseering

Thanks for your response @aseering! I tried adding the --login option as described in #816 but it didn't resolve the issue. I should also mention I'm running on the following version of windows:

OS Name: Microsoft Windows 10 Enterprise OS Version: 10.0.14393 N/A Build 14393

stringhaml avatar Nov 23 '16 17:11 stringhaml

@stringhaml Upgrading to a newer release of Windows via the insider program and using the workaround from #816 should fix this issue. Let us know if that works for you.

jackchammons avatar Nov 23 '16 21:11 jackchammons

How can you upgrade to a newer release?

johnnyodonnell avatar Apr 05 '17 23:04 johnnyodonnell

@johnnyodonnell - supposedly you can download Creator's Update today via the media creation tool, but it "officially" releases on 4/11.

benhillis avatar Apr 05 '17 23:04 benhillis

This is still a problem with 1703 unfortunately.

Running bash --login doesn't help.

Right now there seems to be no way to set an environment variable that is used in all contexts?

I tested this with WSL and Ubuntu Server 16.04. I was expecting WSL to behave just like Ubuntu in this case.

I added RL_ENVIRONMENT=1 to my /etc/environment file and RL_BASHRC=1 to ~/.bashrc testing.sh:

#!/bin/bash
echo RL_ENVIRONMENT: $RL_ENVIRONMENT
echo RL_BASHRC: $RL_BASHRC

WSL 16170 (1703/Creators Update):

$ echo $RL_ENVIRONMENT

$ echo $RL_BASHRC
1
$ sudo echo $RL_ENVIRONMENT

$ sudo echo $RL_BASHRC
1
$ ./testing.sh
RL_ENVIRONMENT:
RL_BASHRC:
$ sudo ./testing.sh
RL_ENVIRONMENT: 1
RL_BASHRC:

Ubuntu 16.04:

$ echo $RL_ENVIRONMENT
1
$ echo $RL_BASHRC
1
$ sudo echo $RL_ENVIRONMENT
1
$ sudo echo $RL_BASHRC
1
$ ./testing.sh
RL_ENVIRONMENT: 1
RL_BASHRC:
$ sudo ./testing.sh
RL_ENVIRONMENT: 1
RL_BASHRC:

Using sudo su - does pick up /etc/environment and functions the same in both WSL and Ubuntu Using sudo su -; su defaultuser does cause WSL to work as expected in this scenario.

I also use HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss AppendNtPath 0, since I was hoping that would help. I could see this happening to not have /etc/environment stomp on

Somewhat related: https://github.com/Microsoft/BashOnWindows/issues/816

rlabrecque avatar Apr 17 '17 22:04 rlabrecque

It's been over a year and there still isn't a way to properly load the environment in Ubuntu? This is basic stuff. Are there any updates on how to source /etc/environment as it would in any standard Ubuntu install?

thesmart avatar Dec 07 '17 19:12 thesmart

Still not working on 1709 (16299.248).

vient avatar Mar 01 '18 13:03 vient

This got marked as a dupe of #816 a long time ago, but that issue was hence marked fixinsiderbuilds. I think it was marked as such because quoth:

As of build 16188 when running bash.exe without arguments (or with the ~ argument) bash will be launched as a login shell.

/etc/environment would be a different kettle of fish. This issue was conveniently never closed so I'll just de-dupe. Ref #352 which is along the same PAM lines.

therealkenc avatar Mar 01 '18 16:03 therealkenc

Is there any progress on getting WSL to pick up /etc/environment under Ubuntu? If not what is the best work around?

wpwoodjr avatar Jan 02 '19 19:01 wpwoodjr

Still present in 1809. One workaround right now is to use /etc/profile or /etc/profile.d/. Those seem to actually be used, unlike /etc/environment.

falfiya avatar Feb 01 '19 08:02 falfiya

I just tried it on build 18975 and WSL 2 (Ubuntu 18.04 LTS image) - /etc/environment is used by a call to bash -c "set | grep PATH".

oldium avatar Sep 11 '19 21:09 oldium

After sudo login I am able to use variables set in /etc/environment. Interestingly WSL with Ubuntu doesn't seem to read variables set in ~/.pam_environment

clearnote01 avatar Dec 23 '19 20:12 clearnote01

Tested in build 19041.21, not working. Tried to run bash -c "set" and checked the PATH and one more variable, which is in /etc/environment, but not seen in the output. This is broken.

oldium avatar Jan 19 '20 18:01 oldium

Perfect work around for me: using WSLENV

huangjj27 avatar Apr 25 '20 16:04 huangjj27

WSL 10.0, Ubuntu 16.04 - /etc/environment completely ignored. Add your vars to ~/.bashrc and then you have them in bash but not in pwsh.

cawoodm avatar Jun 19 '20 08:06 cawoodm

Try to rewrite /etc/enviroment using sudo. Weird, but for me is true that cat /etc/enviroment and sudo cat /etc/enviroment do different things

Aphanasiy avatar Jun 29 '20 15:06 Aphanasiy

Windows 10 Linux Subsystem ███████████████████ BUILD: 19041 BRANCH: vb_release RELEASE: Ubuntu 20.04.1 LTS KERNEL: Linux 4.19.104-microsoft-standard

/etc/environment still not picked up by fresh run in Win10 2004 by wsl2 + Ubuntu.

Andrei-Paul avatar Sep 10 '20 14:09 Andrei-Paul

Confirm this is an issue in Win10 2004 + Ubuntu 18.04. It's quite problematic for environment setup for non-interactive shells - for example PyCharm's WSL integration users the launcher exe which won't pick-up /etc/profile.d scripts - and /etc/bash.bashrc (above the interactive check) doesn't work. It seems there is no way of having an env var set without interactively logging in.

falloutphil avatar Oct 03 '20 16:10 falloutphil

Confirmed also on Win10 2004 + Ubuntu 20.04, WSL2

Is there a way to pass the --login option to a WSL instance started from Windows Terminal?

edit: nevermind, while read line; do export $line; done < /etc/environment inside ~/.zshrc or ~/.bashrc is sufficient.

smaktacular avatar Oct 27 '20 08:10 smaktacular

Confirmed on Win10 20H2, Ubuntu 20.04, WSL2. Still not working.

janekb04 avatar May 11 '21 18:05 janekb04

Still an issue using Windows 11 Home 21H2 build 22000.132 and Ubuntu 20.04 running on WSL

dineiar avatar Aug 13 '21 22:08 dineiar

I am not sure. I think this issue related to the systemd. To get a full functional systemd on WSL2, I always use genie. (https://github.com/arkane-systems/genie)

If I boot the WSL via genie, the pam_env can be loaded.

(PS: because the genie create a login shell so the issue may not related to the systemd. A login shell is the important point)

8ar10der avatar Sep 18 '21 15:09 8ar10der

@jackchammons @therealkenc It's been 6 years since this was opened, 4+ years since de-duped from #816 . 3 years since linked to #4232, and since then no maintainers from MS have commented or provided update on these two critical bugs. Are these things on any internal roadmap or associated with any project plan to be done in the next year? Or better yet, could you comment as to whether or not anyone is actively working on them?

solvingj avatar Jun 03 '22 13:06 solvingj

Let's hope Poettering cleans up this mess now that he's with Microsoft 😜

@8ar10der, I don't think it has anything to do with login shells. bash --login or zsh --login does not make a difference at least for me.

trallnag avatar Jul 27 '22 21:07 trallnag

Related: https://github.com/linux-pam/linux-pam/issues/481


Interesting finding: /etc/environment is parsed once sudo login is performed. Why does the default login not work?


Does anybody know what tool WSL uses to perform the login? Maybe what is missing is a dedicated config in pam.d for that.


I found a way to get into the WSL distribution from PowerShell that uses /bin/login and by that parses /etc/environment.

wsl.exe -u root -- /bin/login -f <username>

Found this by googling here: https://uzxmx.github.io/wsl-tricks.html

trallnag avatar Jul 31 '22 21:07 trallnag

Any update on this? I still have this problem using Windows 11 build 22621.1702.

I found a way to get into the WSL distribution from PowerShell that uses /bin/login and by that parses /etc/environment.

wsl.exe -u root -- /bin/login -f <username>

Found this by googling here: https://uzxmx.github.io/wsl-tricks.html

This works for me, but then every time I open VS Code within WSL, it opens from Windows' file system instead of WSL's

I ended up using WSLENV to set my environment variables. I added something like this to my Powershell $PROFILE:

$env:MYENV = "some-value"
$env:WSLENV = "${env:WSLENV}::MYENV"

And then setting my windows terminal profile:

{
  "name": "Ubuntu",  
- "source": "CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc",
+ "commandline": "pwsh.exe --command \"wsl --cd ~\"",
},

Or executing directly from powershell:

ubuntu

This works, but I don't feel like this should be the way to do it.

lombervid avatar May 26 '23 15:05 lombervid

any update?

npuichigo avatar Jul 10 '23 12:07 npuichigo

@jackchammons @therealkenc It's been 6 years since this was opened, 4+ years since de-duped from #816 . 3 years since linked to #4232, and since then no maintainers from MS have commented or provided update on these two critical bugs. Are these things on any internal roadmap or associated with any project plan to be done in the next year? Or better yet, could you comment as to whether or not anyone is actively working on them?

7 years now...

iamexe avatar Oct 02 '23 18:10 iamexe

I have just found my way here after spending an incredible amount of time trying to do what seems to be the most simple thing: set an env var globally inside of a distro or across all distros. ChatGPT thought it was possible to assign them in an [environment] section in /etc/wsl.conf or the system-level .wslconfig, which would indeed do the job nicely, but appears the bot was hallucinating that one.

Although this present issue is about bash specifically, for many use cases it would be helpful to have this not be shell-dependent. In my own case as a PowerShell-first user, the bash profile workarounds are not that helpful, and what should we do, define these things differently for every different shell type? In a corporate environment like mine we want to have a pre-configured distro with things like SSL config vars set up correctly, so that things work right out of the box without every dev having to configure their profiles.

The WSLENV workaround is about the only one that makes sense in that context, but is ugly as it means pushing profile changes to Windows desktops rather than just tailoring our WSL distro. The config sections suggested by ChatGPT would be dead simple to implement surely, and gets around any messing about with PAM etc. Can we have something like that, please please please???

mikenunan avatar Nov 08 '23 16:11 mikenunan