1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WSLを使ってみる

Last updated at Posted at 2025-11-08

What's?

WSLを使う機会がそこそこできそうなので、少し見ておこうかなということで。

WSL(Windows Subsystem for Linux)

WSLのドキュメントはこちら。WSLはWindows Subsystem for Linuxの略ですね。

概要はこちら。

ポイント。

WSLにはバージョンが2つあり、現在はWSL 2を使うことになります。

基本的なWSLコマンドはこちら。

もう少しポイントを見ておきましょう。

それでは使ってみましょう。

環境

PS > [System.Environment]::OSVersion

Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.22621.0 Microsoft Windows NT 10.0.22621.0


PS > $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      22621  6060


WSLをインストールする

WSLは、Windows 10 バージョン2004以降(ビルド19041以降)かWindows 11上で利用できます。

PS > wsl --install

インストールが行われます。

インストール中: 仮想マシン プラットフォーム
仮想マシン プラットフォーム はインストールされました。
インストール中: Linux  Windows サブシステム
Linux  Windows サブシステム はインストールされました。
インストール中: Ubuntu
[================          28.0%                           ]

デフォルトでは、ディストリビューションとしてUbuntu Linuxがインストールされます。

完了すると、再起動が必要です。

要求された操作は正常に終了しました。変更を有効にするには、システムを再起動する必要があります。

再起動すると、ディストリビューションのダウンロードが始まります。

image.png

ダウンロードとインストールが完了すると、ユーザとパスワードの設定を求められます。

image.png

  • このユーザ名とパスワードはLinuxディストリビューションごとに固有であり、Windowsのユーザ名とは関係がない
  • ディストリビューションのデフォルトユーザとなり、起動時に自動的にサインインする
  • 管理者と見なされ、sudoコマンドが実行できる
  • WSLで実行される各ディストリビューションごとに独自のユーザとパスワードが存在し、ディストリビューションの追加、再インストール、再設定のたびにLinuxユーザを構成することになる

WSL 開発環境を設定する / Linux のユーザー名とパスワードを設定する

というわけで、WSL上でUbuntu Linuxが起動しました。

image.png

あとはLinuxとして使っていきます。

インストールしたWSLのバージョンはこちら。

PS > wsl --version
WSL version: 2.6.1.0
Kernel version: 6.6.87.2-1
WSLg version: 1.0.66
MSRDC version: 1.2.6353
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.26100.1-240331-1435.ge-release
Windows version: 10.0.22621.1105

現時点で選択できるディストリビューションはこちら。

PS > wsl --list --online
The following is a list of valid distributions that can be installed.
Install using 'wsl.exe --install <Distro>'.

NAME                            FRIENDLY NAME
AlmaLinux-8                     AlmaLinux OS 8
AlmaLinux-9                     AlmaLinux OS 9
AlmaLinux-Kitten-10             AlmaLinux OS Kitten 10
AlmaLinux-10                    AlmaLinux OS 10
Debian                          Debian GNU/Linux
FedoraLinux-43                  Fedora Linux 43
FedoraLinux-42                  Fedora Linux 42
SUSE-Linux-Enterprise-15-SP7    SUSE Linux Enterprise 15 SP7
SUSE-Linux-Enterprise-16.0      SUSE Linux Enterprise 16.0
Ubuntu                          Ubuntu
Ubuntu-24.04                    Ubuntu 24.04 LTS
archlinux                       Arch Linux
kali-linux                      Kali Linux Rolling
openSUSE-Tumbleweed             openSUSE Tumbleweed
openSUSE-Leap-16.0              openSUSE Leap 16.0
Ubuntu-20.04                    Ubuntu 20.04 LTS
Ubuntu-22.04                    Ubuntu 22.04 LTS
OracleLinux_7_9                 Oracle Linux 7.9
OracleLinux_8_10                Oracle Linux 8.10
OracleLinux_9_5                 Oracle Linux 9.5
openSUSE-Leap-15.6              openSUSE Leap 15.6
SUSE-Linux-Enterprise-15-SP6    SUSE Linux Enterprise 15 SP6

WSLの設定

WSLには、2つの設定ファイルがあるようです。

  • %UserProfile%\.wslconfig … すべてのWSLに適用される設定
  • /etc/wsl.conf … WSLディストリビューション単位の設定

%UserProfile%\.wslconfigはデフォルトでは存在しないみたいですね。

PS > ls $Env:USERPROFILE\.wslconfig
ls : Cannot find path 'C:\Users\user\.wslconfig' because it does not exist.
At line:1 char:1
+ ls $Env:USERPROFILE\.wslconfig
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\user\.wslconfig:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

/etc/wsl.confはありました。

/etc/wsl.conf
[boot]
systemd=true

[user]
default=user

ここから各設定ファイルの内容を少し見ていこうと思いますが、設定変更には8秒ルールがあるようです。

WSL での詳細設定の構成 / 構成変更の 8 秒ルール

要するにディストリビューションを停止してもWSL自体が実行されている可能性があるので、変更がすぐに反映されないかもしれない、ということです。

確実に反映するには、以下のようにwsl --terminateで特定のディストリビューションを終了させるとよいでしょう。

PS > wsl --terminate [ディストリビューション名]

wsl.conf

ここではwsl.confについて見ていきます。

WSL での詳細設定の構成 / wsl.conf

wsl.confは、ディストリビューションごとの設定ファイルでした。/etc/wsl.confに配置されています。

wsl.confでは以下のような設定があります。

  • systemdサポートの有効化
  • 自動マウントの設定
    • デフォルトで固定ドライブ(たとえばCドライブ)が/mnt配下にマウントされる
  • ネットワーク設定
    • /etc/hostsresolv.confの生成など
  • Windowsとの相互運用
    • Windowsのプロセスの起動を可能とするか、WindowsのPATH環境変数をWSL側のPATH環境変数にも追加するかどうかなど

設定例は以下ですね。

/etc/wsl.conf
# Automatically mount Windows drive when the distribution is launched
[automount]

# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled=true

# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c.
root = /

# DrvFs-specific options can be specified.
options = "metadata,uid=1003,gid=1003,umask=077,fmask=11,case=off"

# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
mountFsTab=true

# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).
[network]
hostname=DemoHost
generateHosts=false
generateResolvConf=false

# Set whether WSL supports interop processes like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
[interop]
enabled=false
appendWindowsPath=false

# Set the user when launching a distribution with WSL.
[user]
default=DemoUser

# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
[boot]
command=service docker start

デフォルトではWindows側のものが見えるようになっているので、こうなっています。

$ ll /mnt/c
ls: cannot access '/mnt/c/DumpStack.log.tmp': Permission denied
ls: cannot access '/mnt/c/pagefile.sys': Permission denied
ls: cannot access '/mnt/c/swapfile.sys': Permission denied
ls: '/mnt/c/System Volume Information': Permission denied
total 448
drwxrwxrwx 1 user user    512 Nov  8 10:38 '$Recycle.Bin'/
drwxrwxrwx 1 user user    512 Feb  9  2023 '$WinREAgent'/
drwxrwxrwx 1 user user    512 Nov  8 10:40  ./
drwxr-xr-x 5 root root   4096 Nov  8 10:43  ../
-rwxrwxrwx 1 user user      1 May  7  2022  BOOTNXT*
-r-xr-xr-x 1 user user   8192 Feb 10  2023  BOOTSECT.BAK*
drwxrwxrwx 1 user user    512 Feb  9  2023  Boot/
lrwxrwxrwx 1 user user     12 Feb  9  2023 'Documents and Settings' -> /mnt/c/Users/
-????????? ? ?    ?         ?            ?  DumpStack.log.tmp
drwxrwxrwx 1 user user    512 May  7  2022  PerfLogs/
drwxrwxrwx 1 user user    512 Nov  8 10:40 'Program Files'/
drwxrwxrwx 1 user user    512 May  7  2022 'Program Files (x86)'/
drwxrwxrwx 1 user user    512 Feb  9  2023  ProgramData/
drwxrwxrwx 1 user user    512 Feb 10  2023  Recovery/
d--x--x--x 1 user user    512 Feb  9  2023 'System Volume Information'/
drwxrwxrwx 1 user user    512 Nov  8 10:38  Users/
drwxrwxrwx 1 user user    512 Nov  8 11:06  Windows/
-r-xr-xr-x 1 user user 442414 Feb  9  2023  bootmgr*
-????????? ? ?    ?         ?            ?  pagefile.sys
-????????? ? ?    ?         ?            ?  swapfile.sys


2$ printenv PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/OpenSSH:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/PowerShell/7/:/mnt/c/Program Files/rsync:/mnt/c/Program Files/OpenSSH:/mnt/c/Users/user/AppData/Local/Microsoft/WindowsApps:/snap/bin

たとえば、[automount]enabled=false[interop]appendWindowsPath=falseとしてみましょう。

/etc/wsl.conf
[boot]
systemd=true

[user]
default=user

[automount]
enabled=false

[interop]
appendWindowsPath=false

1度ディストリビューションを終了し、起動。

PS > wsl --terminate Ubuntu
The operation completed successfully.
PS > wsl

今度はWindows側のCドライブの内容やPATH環境変数の内容が見えなくなっています(/mnt/cまではあるようですが)。

$ ll /mnt/c
total 8
drwxrwxrwx 2 root root 4096 Nov  8 10:43 ./
drwxr-xr-x 5 root root 4096 Nov  8 10:43 ..```


$ printenv PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/snap/bin

.wslconfig

次は、.wslconfigファイルについて。

WSL での詳細設定の構成 / .wslconfig

.wslconfigファイルは、やはりデフォルトでは存在しないようです。

ここでは以下のようなことが設定できるようです。

  • カーネルの設定
  • WSLに割り当てるメモリの量
    • デフォルトではWindowsのメモリの50%
  • ネットワークモード
    • NAT、ミラーなど
  • Hyper-Vファイアーウォール
  • DNS
  • プロキシ
    • Windows側のプロキシ設定の使用

CPU数は設定できないのかな?と思ったのですが、それはGUIから設定するようです。

.wslconfigファイルよりもこちらを使うことの方が多いかもですね。

image.png

なんとなく、設定などがわかってきました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?