如何在虛擬機器中的 Windows 10 和 Linux 發行版上設定和使用 WSL?

如何在虛擬機器中的 Windows 10 和 Linux 發行版上設定和使用 WSL?

我在 Windows 10 上安裝了 WSL 2。一切正常。

我想從 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 專業版
  • 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

這實際上不是一個 Powershell 程式碼問題,而是一個“如何在 Windows 10 和虛擬機中的 Linux 發行版上設定和使用 WSL?”

WSL 是一項服務...

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

...允許 Linux 發行版在 Windows 10 上運行。所以,這個錯誤是非常具體的。您的發行版必須正在運行!

您是否已查看並利用影片中所示的所需步驟...

....配置 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

相關內容