如何从WSL中删除Win10的路径

问题描述:

我在Win10中使用Windows子系统Linux(Ubuntu 18.04),并在其中安装了Maven. 此外,我之前在Win10中安装了一个Maven. 现在,当我在WSL中使用mvn compile时,它告诉我maven编译失败. 我使用which mvn并发现它引用了Win10中安装的Maven.

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.

此外,我运行env并发现Win10的路径已添加到WSL的路径中. 使用WSL时,我不想在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?

  • 对于低于17713的Windows版本: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)
      

      在第一次启动时,WSL使用默认标志= 0x7(即0 + 1 + 2 + 4).如果该标志= 0x5(即0 + 1 + 4)Windows NT路径将不会附加在$PATH环境变量中.那么,如何找到该标志的注册表值? aka在注册表编辑器中打开HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss注册表路径. regedit.exe.打开具有UID值的每个子项,然后将DistributionName与已安装的分发名称匹配.然后将Flags DWORD注册表值编辑/添加到0x5.

      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? Open HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss registry path 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.

      对于Windows构建比17713高:在新版本中,WSL使用 wsl.conf文件,以配置其在Windows和Linux端之间的行为和互操作性.该wsl.conf文件遵循 INI文件格式.运行wsl.exebash.exe.创建文件/etc/wsl.conf.然后,将以下 interop部分添加任何文本Linux中的编辑器.

      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. That wsl.conf file follows INI file format. Run wsl.exe or bash.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
      

      保存该文件,然后从wsl.exe退出.现在,无论何时执行WSL,Windows路径都不会附加到Linux $PATH环境变量中.

      Save that file and exit from wsl.exe. Now whenever WSL is executed Windows paths will not appended to Linux $PATH environment variable.