Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bash $PATH includes native Windows path entries #1640

Closed
markgross opened this issue Jan 28, 2017 · 54 comments
Closed

bash $PATH includes native Windows path entries #1640

markgross opened this issue Jan 28, 2017 · 54 comments

Comments

@markgross
Copy link

  • A brief description
    I just noticed that there are entries in the bash PATH that include windows executibles. I'm not sure if this is a feature or a bug.

I see that my Anaconda3 (a windows python distrobution) has executibles on the bash path. I expected only linux native programs to be accessible from the ubuntu bash env.

mgross@marks-x1:$ which fetch_file.exe
/mnt/c/Program Files/Anaconda3/Scripts/fetch_file.exe
mgross@marks-x1:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Windows/System32:/mnt/c/Windows:/mnt/c/Windows/System32/wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/Program Files/Anaconda3:/mnt/c/Program Files/Anaconda3/Scripts:/mnt/c/Program Files/Anaconda3/Library/bin:/mnt/c/Users/mark9/AppData/Local/Microsoft/WindowsApp

I worry that the leakage of the Windows environment into the bash will result in unexpected behaviors from time to time.

  • Your Windows build number
    14986.rs_prerelease.161202-1928

  • Steps / All commands required to reproduce the error from a brand new installation
    not sure if Anaconda3 needs to be installed before enabling the WSL or after (or if that matters.)

@Gabrielcarvfer
Copy link

@markgross people asked in UserVoice to be able to run Windows commands from bash, so it should be a feature. As it is appended to the PATH, it shouldn't make any difference if you have python or any other linux programs installed on WSL. If you don't want that to happen, just export PATH containing only WSL folders on ~/.bashrc . :)

@MikeGitb
Copy link

@Gabrielcarvfer: It makes a difference if you have installed a tool in Windows but not in linux. Usually you'd get an error like "tool XY is currently not installed. Type sudo apt install ... to install it now". If the windows version gets picked up instead, everything works either as expected or you might get very strange errors.
However, I'd expect this to be a rather rare occasion.

@markgross
Copy link
Author

Default options should be secure options. If someone wants to add power shell env path's to their bash env they can so. But, adding things by default is IMO a problem that will bite people in the future.

Also, I don't want any python scripts or executable leaking between my windows and linux environments.

@Gabrielcarvfer
Copy link

@MikeGitb and @markgross , a switch for that in Settings's Developer section for that (injecting Windows path on WSL path) would solve your problem, but this is a work in progress, in a prerelease build, then you can use the workaround until they fix it.

@aseering
Copy link
Contributor

aseering commented Jan 30, 2017

@markgross -- to answer your implied initial question, this is a deliberate feature.

Regarding your concern about namespace collisions -- note that Windows executables must have a file extension such as .exe, .com, etc. As you've seen above, you can find fetch_file.exe, but you must then execute it as fetch_file.exe, not fetch_file. In contrast, by convention Linux executables in $PATH typically do not have file extensions. As a result, names will tend to not conflict too often. You are, of course, absolutely correct that they can conflict.

Regarding security -- my expectation right now as a user is that any malicious WSL process can acquire Linux root at any time. If you've already got root, messing with stuff in $PATH is small potatoes :-) WSL processes (even ones running as root) are all just unprivileged (as far as Windows is concerned) programs running under your Windows user account. So the environment is effectively single-user, and should IMO be secured accordingly. (The WSL team is probably looking into more-sophisticated security models, but that's what exists today, as far as I can tell.)

Regarding controlling this behavior, take a look at #1493 .

@itomkins
Copy link

On my system the bash path is truncated for some reason I can't figure:
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/ProgramData/Oracle/Java/javapath_target_4529046:/mnt/c/Windows/System32:/mnt/c/Windows:/mnt/c/Windows/System32/wbem <SNIP> :/mnt/c/Program Files/Oracle/VirtualBox:/mnt/c/Program Files (x86)/Nma

Where the original Windows path ends with ;C:\Program Files (x86)\Nmap

Also for some reason the .profile is not adding my ~/bin directory to the path, I have copied the relevant code to the end of .bashrc and it works fine so not sure what is going on there

@benhillis
Copy link
Member

benhillis commented Jan 31, 2017

I agree with @aseering. Since you have also provide the file extension to invoke the Windows binaries I think it is pretty unlikely that there will be much confusion. @markgross if you have a specific case where this is happening kindly reopen this ticket.

You can disable this feature by setting this registry key:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss]
"AppendNtPath"=dword:00000000

@itomkins - I assume you're running an older insider build? There was an off by one error that is fixed in recent builds.

@aseering
Copy link
Contributor

@itomkins -- regarding .profile, that might be an instance of #816 ?

@benhillis
Copy link
Member

@aseering - yes it is.

@itomkins
Copy link

itomkins commented Feb 2, 2017

Thanks @benhillis @aseering correct on both points, added --login to shortcut and will have to switch from slow to fast to pick up a newer build i guess.

@markgross
Copy link
Author

I just watched some videos from https://blogs.msdn.microsoft.com/commandline/learn-about-bash-on-windows-subsystem-for-linux/

Specifically https://sec.ch9.ms/ch9/ed3c/e3560e79-4719-49dc-a7e8-607ca516ed3c/WindowSubsystemforLinuxFileSystem_high.mp4

around time stamp 22min it talks about the effects of native windows programs operating on WSL files. It results in files that can't be seen from the bash environment.

Please explain why allowing windows path's is a good idea? It looks like a train wreck given this new information.

@markgross
Copy link
Author

Hmm, I guess there is one more video I needed to watch before posting my last comment.
https://blogs.msdn.microsoft.com/wsl/2016/10/19/windows-and-ubuntu-interoperability/

Still, if I do the following I get a bad experiance:

  1. from bash vim $HOME/temp.txt and add some text.
    2)close vim.
  2. open gvim.exe (windows gvim install) gvim.exe $HOME/temp.txt

I can't edit the content I added and I can't save anychanges I made over top temp.txt

@aseering
Copy link
Contributor

Hi @markgross -- have you actually tried running the command that you suggest? I think you will find that the set of things that you can't do is larger than you are saying :-)

@benhillis
Copy link
Member

@markgross - Due to the filesystem limitations you mention above accessing files in LxFs (anything not under /mnt/*) is not supported via interop. You won't be able to modify files in LxFs unless you do some trickery like passing the full NT path of .bashrc to gvim.exe.

I'd suggest trying out the feature and seeing how it works. I'd love to hear specific feedback about things that aren't working as you expect. As long as you're working out of one of your mounted NTFS drives you shouldn't run into issues.

As an aside: the limitation of not being able to modify files in LxFs is something we're spending a lot of time looking at for a future release. I would love to come up with a solution that gets rid of this limitation - but creating a mapping between Linux and NT security models is a complicated problem. Just ask anybody who's worked on samba :)

@markgross
Copy link
Author

Thanks. I will think some more about the VSF/NTFS issues for content not under /mnt/[a..z].

FWIW I have not done any powershell based development so a lot of the utility for this feature is lost on me. but, I do work in linux on native Linux boxes. Yes, those mappings are not simple to deal with. they are so complex I would punt on it.

Understanding the use case after watching your interview helps. I'm still not sure its worth the native linux file inconsistencies that happen because of having this feature. ok, I don't think its worth it because of the LxFs / NTFS extended attributes (and caching) issues. but, I agree its handy when operating on NTFS files under /mnt/[a..z] somewhere and wanting to use native linux user mode tools.

FWIW I'd hide the extended path from the normally launched bash shell but enable it from powershell command line invocations of base.exe if I couldn't address the LxFs / NTFS issues enough for a good UX. But, the user needs to be aware of the file system limitations.

Thanks for the responses! I've been very impressed with the direct interaction with you developers on WLS.

@irontoby
Copy link

irontoby commented Sep 19, 2017

Not sure where the idea that "since you have to also provide the file extension to invoke the Windows binaries I think it is pretty unlikely that there will be much confusion" is coming from. Perhaps this helps illustrate the problem:

Me on any other Ubuntu system:

toby@localhost:~$ npm -v
The program 'npm' is currently not installed. You can install it by typing:
sudo apt install npm
toby@localhost:~$ sudo apt install npm

It's a super-helpful feature and incredibly streamlined workflow. I don't even worry about whether some CLI snippet I see online needs a tool I don't have. I just paste it and if something is missing, Bash tells me exactly how to get it.

Me using Ubuntu bash on Windows:

toby@localhost:~$ npm -v
: not foundram Files/nodejs/npm: 3: /mnt/c/Program Files/nodejs/npm:
: not foundram Files/nodejs/npm: 5: /mnt/c/Program Files/nodejs/npm:
/mnt/c/Program Files/nodejs/npm: 6: /mnt/c/Program Files/nodejs/npm: Syntax error: word unexpected (expecting "in")
toby@localhost:~$

Incredibly unhelpful, and not a very good experience for those who are used to Linux and expect the WSL environment to be more "clean". I didn't type any Windows extension anywhere for this to happen. I think the intersection of people who have node.js installed on Windows and want a completely separate node.js in WSL would be quite high.

Very glad to see the regedit entry to solve this, but exposing it more visibly would be quite welcome. At any rate, thanks for listening, I'm very much enjoying WSL so far...

@oterhals
Copy link

oterhals commented Oct 8, 2017

Here's another workaround, although not the most elegant one.

If you add this to the end of your .bashrc file, all references to Windows PATHs will be removed. In my case 20 elements are removed and only 8 are left:

PATH=$(/usr/bin/printenv PATH | /usr/bin/perl -ne 'print join(":", grep { !/\/mnt\/[a-z]/ } split(/:/));')

Not sure if this will catch everything, but it does the trick for me.

@markgross
Copy link
Author

markgross commented Oct 9, 2017 via email

@zbutfly
Copy link

zbutfly commented Mar 15, 2018

I write a script to resolve it, append . scriptname.sh at your ~/.profile or any other init script else:

#!/bin/bash

IFS=':'
NEWPATH=''

for p in ${PATH[@]}
do
	if [ "${p:0:5}" != "/mnt/" ]; then
		NEWPATH+=":$p"
	fi
done
IFS=' '

export PATH=${NEWPATH:1}

@mpiroc
Copy link

mpiroc commented Jun 7, 2018

The rationale for closing this bug is that "you need to include the file extension in order to invoke windows executables". But as @irontoby demonstrated (I see the same behavior on my system), this just isn't true. If you invoke foo, Windows considers foo.exe to be a match.

Even with nodejs installed through apt-get, running node still picks up the Windows version, not the Linux version.

I don't believe that this bug should be closed.

@benhillis
Copy link
Member

@mpiroc - That's because the node guys removed the .exe extension from their binary.

@mpiroc
Copy link

mpiroc commented Jun 8, 2018

@benhillis On my machine, it's still present:

pirocchi@LAPTOP:/mnt/c/repos/foo$ whereis node
node: /mnt/c/Program Files/nodejs/node.exe

@benhillis
Copy link
Member

@mpiroc - Look in that directory, you'll also find a "node" file with no extension.

@mpiroc
Copy link

mpiroc commented Jun 8, 2018

pirocchi@LAPTOP:/mnt/c/repos$ ls "/mnt/c/Program Files/nodejs/"
node_etw_provider.man  node.exe  node_modules  node_perfctr_provider.man  nodevars.bat  npm  npm.cmd  npx  npx.cmd

@mhamrah
Copy link

mhamrah commented Jul 19, 2018

Correct, but I want Windows to automatically prepend 'wsl' based on any commands it finds in my path that are in linux via WSL. This will let things like extensions in vs code call out to 'npm' and have it run the linux version.

@therealkenc
Copy link
Collaborator

prepend 'wsl' based on any commands it finds in my path that are in linux via WSL

That's #1823

@mean-ui-thread
Copy link

mean-ui-thread commented Aug 27, 2018

I got the same kind of issues than most people here: It picks the ruby from Windows 10 instead of /usr/bin/ruby from Linux, it picks npm from Windows 10 instead of /usr/bin/npm from Linux, it picks perl from Windows 10 instead of /usr/bin/perl from linux, etc. And these are causing all kinds very weird and strange error messages and weird behaviours on my side.

To fix it, I just added these two lines in my ~/.profile file:

unset PATH
. /etc/environment

and all of the problems when away:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

No more Windows 10 Paths in my linux's PATH environment variable.

EDIT 2019-05-17

@m00head 's comment next to mine is the best workaround since it doesn't involve hacking anything in your bash profile: #1493 (comment)

regedit here:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\{GUID}\Flags

Change Flags from 7 to 5. That's it!

Now my path looks like this:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

@m00head
Copy link

m00head commented Aug 31, 2018

I have posted the latest workaround for this issue here: #1493 (comment)

@c0ze
Copy link

c0ze commented Dec 21, 2018

@markgross -- to answer your implied initial question, this is a deliberate feature.

this is a horrible call of judgement and I am apalled this has not been fixed yet.

this should not be on by default. It should be other way around.

@Vichoko
Copy link

Vichoko commented Apr 18, 2019

As the windows paths are added to the tail of PATH, it have preference.
I got colissions of names with python (as it's called without extension in both systems), also pip didn't work even that i'm sure it was installed in both windows and linux. @oterhals solution worked perfectly, as i prefer having isolated enviroments between both systems

@DavidKDeutsch
Copy link

A note to those that have stumbled upon this thread via searching: forget the registry tweaks mentioned here and elsewhere, as they keep changing. I used the method mentioned in #1493 and it worked like a charm:

echo "export PATH=`echo $PATH | tr ':' '\n' | grep -v /mnt/ | tr '\n' ':'`" >> ~/.bashrc

I'm sure @mchiasson's solution, @oterhals solution, etc. work as well; the benefit of any of these methods is that since the changes are within the WSL environment itself, the fix will survive whatever Microsoft decides to "tweak" later on.

And for the record, add me to the list of people who are baffled that this "feature" is enabled by default. It's not a lot of fun trying to diagnose errors such as : not foundram Files/nodejs/npm....

@newtonmunene99
Copy link

@DavidKDeutsch Thank you so much. You are a life saver

@mean-ui-thread
Copy link

@m00head 's workaround is the best one so far : #1493 (comment)

@gnarus-io
Copy link

Fix at #1493 fixes this flutter error for anyone landing here from google.

~/sdks/flutter$ flutter upgrade
/mnt/c/Users/GavinShaw/.flutter/bin/flutter: line 5: $'\r': command not found
/mnt/c/Users/GavinShaw/.flutter/bin/flutter: line 6: $'\r': command not found
/mnt/c/Users/GavinShaw/.flutter/bin/flutter: line 14: $'\r': command not found

@LachlanArthur
Copy link

LachlanArthur commented Apr 21, 2020

Instead of trying to fiddle with the path or the registry, just disable the feature.

in WSL:

sudo vi /etc/wsl.conf

add:

[interop]
appendWindowsPath = false

then in Windows find your distro name and terminate it so the config changes are picked up:

wsl.exe --list
wsl.exe --terminate <distro_name>

wsl.conf options

@wlerin
Copy link

wlerin commented Apr 21, 2020

That option was added long after this issue was closed, and is mentioned in #1493 , but still helpful to document it here as well I guess.

@mattwelke
Copy link

mattwelke commented Oct 21, 2020

The approach to edit sudo vi /etc/wsl.conf and add:

[interop]
appendWindowsPath = false

solved this problem for me. I no longer have Windows executables on my path. But it changed how I have to start VS Code with WSL now. Just thought I'd mention it in case someone else googles it and ends up here.

I used to be able to start VS Code for Remote WSL work by typing code <dir_name> in WSL, but that was the Windows binary it was launching before.

So now, since VS Code can't be started from within WSL, start open VS Code in Windows (with the remote extension installed) and use the command palette (because the option doesn't show up in the GUI) menu to select Remote-WSL: New Window if your VS Code didn't start in WSL mode when you opened it:

image

Then, Open Folder..., which will show you folders within your WSL distro.

@giraffesyo
Copy link

@mattwelke To solve that problem you can include the Windows installation of vs code in your path explicitly,

e.g.

echo PATH=\$PATH:/mnt/c/Users/`whoami`/AppData/Local/Programs/Microsoft\\ VS\\ Code/bin >> ~/.bash_profile

@therealkenc
Copy link
Collaborator

Quoted rather than escaped is probably more straightforward, and that leading backslash doesn't look right, and your WSL username is not necessarily your Windows username.

This said, cherry picking Windows paths in .bashrc is not a bad way to roll in some circumstances.

@mattwelke
Copy link

This said, cherry picking Windows paths in .bashrc is not a bad way to roll in some circumstances.

Would this be an effective way to limit access to Windows executables to just VS Code (or some other program) while keeping Windows binary interop turned on? For example, to prevent node within WSL from picking up Node.js installed in Windows?

@giraffesyo
Copy link

Quoted rather than escaped is probably more straightforward, and that leading backslash doesn't look right, and your WSL username is not necessarily your Windows username.

This said, cherry picking Windows paths in .bashrc is not a bad way to roll in some circumstances.

That's true that the username doesn't have to match, good call.

As for the leading backslash, thats because it would expand your current path if you didn't do that.

@gioreva
Copy link

gioreva commented Sep 3, 2021

@Gabrielcarvfer

If you don't want that to happen, just export PATH containing only WSL folders on ~/.bashrc . :)

My ~/.bashrc does not contain Windows PATH.
And I can't find any other files, I need to remove them.

WSL2 Ubuntu

@gioreva
Copy link

gioreva commented Sep 3, 2021

Also adding this keys windows path remain

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss] "AppendNtPath"=dword:00000000

Also Here
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss{d3890f4a-e8e5-40c0-8ca1-18985822d0c2}

@gioreva
Copy link

gioreva commented Sep 3, 2021

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss{GUID}\Flags
Change flags from 7 to 5.

I've lost everything.
Ubuntu has returned empty, as just installed.

And Windows path remain.

@gioreva
Copy link

gioreva commented Sep 3, 2021

Solved overwriting the path

~/.profile
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

@eatcosmos
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests