WSL(Windows Subsystem for Linux)メモ書き

技術情報
公開: 2024年08月20日

参考

公式:Windows Subsystem for Linux に関するドキュメント

インストール

公式:WSL を使用して Windows に Linux をインストールする方法

PowerShell から以下のコマンドを実行。

wsl --install

ユーザー名とパスワードを入力すれば完了。 以下の例ではユーザー名に korochin を入力している。

PS C:\Windows\system32> wsl --install
インストール中: Ubuntu
Ubuntu がインストールされました。
Ubuntu を起動しています...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: korochin
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.153.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


This message is shown once a day. To disable it please create the
/home/korochin/.hushlogin file.
korochin@koro-P6-I7G60:~$

WSLのファイルシステム

Windows エクスプローラーからは以下のパスでシステム全体のファイルが閲覧できる。

\\wsl.localhost\Ubuntu

WSL内からは以下のコマンドで、Windows エクスプローラーで現在のディレクトリを表示できる。

explorer.exe .

WSLの起動と停止

WSL の基本的なコマンド

  • インストールされているディストリビューションの確認
    ※Windows を再起動した場合、WSL の Linux は停止している
wsl --list --verbose
PS C:\Users\koro> wsl --list --verbose
  NAME              STATE           VERSION
* docker-desktop    Stopped         2
  Ubuntu            Stopped         2
  • Linux ディストリビューションを実行する
wsl --distribution <Distribution Name> --user <User Name>

Ubuntu を ユーザー korochin で起動

PS C:\Users\koro> wsl --distribution Ubuntu --user korochin
Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.153.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

This message is shown once a day. To disable it please create the
/home/korochin/.hushlogin file.
korochin@koro-P6-I7G60:/mnt/c/Users/koro$
  • Linux ディストリビューションを停止する
wsl --terminate <Distribution Name>
PS C:\WINDOWS\system32> wsl --terminate Ubuntu
この操作を正しく終了しました。
PS C:\WINDOWS\system32> wsl --list --verbose
  NAME              STATE           VERSION
* Ubuntu            Stopped         2
  docker-desktop    Stopped         2