How to remove the Win10's PATH from WSL
For Windows build LOWER than 17713: WSL uses WSL_DISTRIBUTION_FLAGS Enumeration to configure its behavior and interoperability between Windows and Linux side. 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) Windows NT path will not appended in
$PATH
environment variable. So, how to find that flags registry value? OpenHKCU\Software\Microsoft\Windows\CurrentVersion\Lxss
registry path in Registry Editor aka.regedit.exe
. Open each subkey with UID values and matchDistributionName
with your installed distribution name. Then edit/add theFlags
DWORD registry value to0x5
.For Windows build HIGHER than 17713: In new build WSL uses
wsl.conf
file to configure its behavior and interoperability between Windows and Linux side. Thatwsl.conf
file follows INI file format. Runwsl.exe
orbash.exe
. Create a file/etc/wsl.conf
. Then add the following interop section with any text editor in Linux.[interop] enabled=false # enable launch of Windows binaries; default is true appendWindowsPath=false # append Windows path to $PATH variable; default is true
Save that file and exit from
wsl.exe
. Now whenever WSL is executed Windows paths will not appended to Linux$PATH
environment variable.
As mentioned by AndrewBourgeois, you may need to shutdown the current instance of wsl with wsl --shutdown
or wsl -t <Distribution>
for changes to take effect.
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
thenCtrl+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
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.