仮想マシン内の 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
  • OSビルド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/ja-jp/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

関連情報