How to remove the Win10's PATH from WSL
Asked Answered
C

4

86

I use Windows Subsystem Linux(Ubuntu 18.04) in my Win10, and I install a Maven in it. Besides, I install a maven in Win10 before. Now when I used mvn compile in WSL, it told me that maven compile fail. I use which mvn and find that it references to the Maven installed in Win10.

Besides, I run env and find that Win10's Path is added to the WSL's Path. I don't want to use any thing in Win10's Path when I use WSL, how should I do?

Commune answered 14/7, 2018 at 7:6 Comment(0)
D
144

For Windows builds HIGHER than 17713:

WSL uses the file /etc/wsl.conf inside each Linux VM's filesystem to configure its behavior. Add the following configuration settings (explained here) to /etc/wsl.conf, creating that file if necessary:

[interop]
appendWindowsPath = false

Note that appendWindowsPath must be under [interop] for this to work. You may need to shutdown the current instance of WSL with wsl --shutdown or wsl -t <Distribution> for changes to take effect. You could also add the option enabled = false, also under the [interop] section, to disable the ability to launch Windows binaries from inside WSL.

For Windows builds LOWER than 17713:

WSL uses WSL_DISTRIBUTION_FLAGS Enumeration to configure its behavior. Here is the code snippet from wslapi.h header file.

/* Flags specifying WSL behavior */
typedef enum
{
  WSL_DISTRIBUTION_FLAGS_NONE                  = 0x0,
  WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP        = 0x1,
  WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH        = 0x2,
  WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING = 0x4
} WSL_DISTRIBUTION_FLAGS;
        
#define WSL_DISTRIBUTION_FLAGS_VALID (WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP | WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH | WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING)
#define WSL_DISTRIBUTION_FLAGS_DEFAULT (WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP | WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH | WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING)

At first launch, WSL uses the default flag = 0x7 (i.e. 0+1+2+4). If that flag = 0x5 (i.e. 0+1+4), the Windows NT path will not appended in the $PATH environment variable. To find that flag's registry value, open HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss in Registry Editor aka. regedit.exe. Open each subkey with UID values and match DistributionName with your installed distribution name. Then edit/add the Flags DWORD registry value to 0x5.

Divertissement answered 15/7, 2018 at 6:30 Comment(9)
After modifying "wsl.conf" file in WSL you need to "wsl --shutdown" for the changes to take effect.Lonilonier
The latter did not exactly work for me. Even though it's an ini file, I removed all the comments (after #, including #) and put spaces around '='. To restart WSL, you can also ctrl-x->powershell(admin) and run "Restart-Service LxssManager".Dismay
This fixed my conda installation that had problems after updating my linux distribution to WSL2. Because it suddenly imported windows paths that caused unexpected errors when trying to activate conda environments.Hector
Worth noting, if you installed WSL on Windows 10 prior to having 17713 installed, the first instructions are still the answer.Neve
This actually worked! For me, the resolution at github.com/microsoft/WSL/issues/1493#issuecomment-266480323 did not do anything. Adding the interop to wsl.conf worked. (I'm using WSL2 by the way)Expert
WSL1 on Win10 LTSC 1809 Build 17763.1875, the registry edits were required in my case.Alterant
I have (re)installed WSL/WSL2 a few times and am trying to get Ubuntu 20.04 to not use my Windows path (that has spaces in it). I am not seeing wsl.conf; where is it located?Fefeal
This no longer works as of January 2023. Had to edit the registry according to the other answer.Dockage
In my case (Windows 11 22H2), the default value for the Flags in the registry was 15 (0xf) (not sure what the extra 0x8 bit means) so I subtracted 2 from it and set it to 13 (0xd) and it worked.Peaslee
A
59

1st step - Disable Windows path on WSL

Option A: Add to wsl.conf (after Build 17093)

sudo nano /etc/wsl.conf

Then add

[interop]
appendWindowsPath = false

then Ctrl+S then Ctrl+X then exit.

Option B: remove paths on runtime

Add the following code to .bashrc

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

Alternative (run once!):

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

Alternative 2

Just add export PATH="$PATH:/usr/bin" to the end of ~/.bashrc, so that usr/bin takes precedence over windows' apps. Probably not a good option.

Option C:

Edit Windows Registry. It is currently not recommended.

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

Change Flags from 7 to 5 in order to exclude the WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH enum.


2nd step - Restart WSL

Option A:

Simple reenter WSL and test:

echo $PATH

Option B:

Run at PowerShell as Admin:

Restart-Service LxssManager

Option C:

Terminating WSL from PowerShell as Admin using

wslconfig /t Ubuntu

Adapt was your need, Ubuntu-18.04 in my case


References:

https://github.com/microsoft/WSL/issues/1493

https://devblogs.microsoft.com/commandline/automatically-configuring-wsl

https://gist.github.com/ilbunilcho/4280bd55a10cefef75e74986b6bff936

Aeroscope answered 31/7, 2020 at 16:46 Comment(2)
The most comprehensive answer!Frazier
While wsl.conf is available after Build 17093, the option appendWindowsPath is only available in Insider Build 17713 and laterConventionalism
B
2

Modifying the Flags attribute in the Windows Registry still works with WSL 2. It worked fine for me and didn't cause any issues whatsoever. The only difference is that the initial Flags value in my case was 0x0F (= 15). Changing that to 0x0D (= 13) prevents appending the Windows %PATH% to the Ubuntu $PATH.

You can still find all WSL 2 distributions in the Registry under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\. Each distribution has its own GUID subfolder, looking something like

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\
    |
    |-- {40db8e6f-0520-4ddd-9c8b-0f5414e685c9}
    |-- {6a61345d-2dc6-4b9e-abb1-28a26d64e19f}
    |-- {bce2f98a-b234-4749-adbe-7311df078d26}
    |-- ...
    |-- {ea808e4e-dc24-4645-aed0-ebd67c871e01}

Of course, your GUIDs will be different from mine.

By clicking on each of the GUIDs and looking at the DistributionName attribute you can find out which GUID refers to what WSL 2 distribution, see (1) in screenshot below. And then simply modify the Flags value and set it to 13, see (2) in screenshot. If the value in the Flags attribute is different from 0x0F it shouldn't matter, just reduce it by 2 and you should be fine.

enter image description here

Bordereau answered 2/12, 2020 at 21:51 Comment(0)
S
2

Warning: if your WSL2 distro was installed after Windows build 17093, do NOT change the "Flags" regedit value from 0xf to 0x5, as would be the case for WSL1. This will destroy your WSL2 setup the next time it gets shutdown/rebooted, losing all of your data. Changing it to 0xD instead will work to remove the NT path from your unix path.

Sadler answered 17/6, 2021 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.