如何在虚拟机中的 Windows 10 和 Linux 发行版上设置和使用 WSL?

如何在虚拟机中的 Windows 10 和 Linux 发行版上设置和使用 WSL?

我已经在 Windows 10 上安装了 WSL 2。我有一个 Debian 系统。一切运行正常。

我想从 PowerShell 终端启动 WSL,因此我输入命令wsl。但我收到错误:

PS C:\Users\MyUser> wsl
wsl : The term 'wsl' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ wsl
+ ~~~
    + CategoryInfo          : ObjectNotFound: (wsl:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

这是为什么?

更新 奇怪的是,当我尝试获取命令时,我得到的是否定的答案:

PS C:\WINDOWS\system32> Get-Command wsl -All
Get-Command : The term 'wsl' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-Command wsl -All
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (wsl:String) [Get-Command], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand

尽管如此,可执行文件wsl.exe仍然在C:\Windows\Syste32,它应该在...

如果我设置了别名:

Set-Alias -Name wsl -Value C:\Windows\system32\wsl.exe

当我运行时,输出没有任何变化wsl(我收到同样的错误)。

Windows 特性:

  • 版本 Windows 10 Pro
  • 版本 2004
  • 操作系统内部版本 19041.572
PS C:\WINDOWS\system32> Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux


FeatureName      : Microsoft-Windows-Subsystem-Linux
DisplayName      : Windows Subsystem for Linux
Description      : Provides services and environments for running native user-mode Linux shells and tools on Windows.
RestartRequired  : Possible
State            : Enabled
CustomProperties :
                   ServerComponent\Description : Provides services and environments for running native user-mode Linux shells and
                   tools on Windows.
                   ServerComponent\DisplayName : Windows Subsystem for Linux
                   ServerComponent\Id : 1033
                   ServerComponent\Type : Feature
                   ServerComponent\UniqueName : Microsoft-Windows-Subsystem-Linux
                   ServerComponent\Deploys\Update\Name : Microsoft-Windows-Subsystem-Linux

答案1

由于命令

wslconfig.exe /l

给予回应

Windows Subsystem for Linux Distributions:
docker-desktop-data
docker-desktop
Debian

我注意到没有设置默认值。因此,我简单地设置了默认值:

wslconfig.exe /setdefault "Debian"

现在wslconfig.exe /l我得到:

Windows Subsystem for Linux Distributions:
Debian (Default)
docker-desktop-data
docker-desktop

并且我能够使用启动 WSL 2 默认发行版wsl

答案2

这实际上不是一个 Powershell 代码问题,而是“如何在虚拟机中的 Windows 10 和 Linux 发行版上设置和使用 WSL?”

WSL 是一种服务...

https://docs.microsoft.com/en-us/windows/wsl/install-win10

... 允许 Linux 发行版在 Windows 10 上运行。它不是从 PowerShell 运行的应用程序/命令。因此,该错误非常具体。您的发行版必须正在运行!

您是否已查看并利用视频中所示的必要步骤......

...配置 WSL ...

(Get-CimInstance -ClassName CIM_OperatingSystem).Version
# Results
<#
10.0.19041
#>


$PSVersionTable
# Results
<#
Name                           Value
----                           -----
PSVersion                      5.1.19041.546
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.19041.546}
BuildVersion                   10.0.19041.546
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
#>

# Results
<#
Get-Command -Name wsl.exe

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     wsl.exe                                            10.0.19... C:\WINDOWS\system32\wsl.exe
#>

Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
# Results
<#
FeatureName      : Microsoft-Windows-Subsystem-Linux
DisplayName      : Windows Subsystem for Linux
Description      : Provides services and environments for running native user-mode Linux shells and tools on Windows.
RestartRequired  : Possible
State            : Enabled
CustomProperties :
                   ServerComponent\Description : Provides services and environments for running native user-mode Linux shells and tools on
                   Windows.
                   ServerComponent\DisplayName : Windows Subsystem for Linux
                   ServerComponent\Id : 1033
                   ServerComponent\Type : Feature
                   ServerComponent\UniqueName : Microsoft-Windows-Subsystem-Linux
                   ServerComponent\Deploys\Update\Name : Microsoft-Windows-Subsystem-Linux
#>

...并且您的发行版配置为使用该 Linux 发行版。

wsl --list --verbose
Windows Subsystem for Linux has no installed distributions.
Distributions can be installed by visiting the Microsoft Store:
https://aka.ms/wslstore

相关内容