Windows – WSL2 (Windows Subsystem for Linux)

Loading

測試環境為 Windows10 Pro (Version :1903 , Build :18990.1 ,要使用 WSL2 其 Windows 版本須大於 Build : 18917) , 虛擬機 (VMware Fusion 11.5) .

WSL2 使用 Hyper-V 的虛擬化技術來運行,透過 VMware Fusion 運行 Windows10 時,需要開啟 Nested Virtualization (巢狀虛擬化),在 Windows10 虛擬機 Settings / Processors and Memory / Advanced (進階選項) / Enable Hypervisor Applications in this virtual machine (在此虛擬機中啟用超級監督者應用程式) , Enables running modern virtualization applications by providing support for Intel VT-x/EPT inside this virtual machine (透過為此虛擬機器中的 Intel VT-x/EPT 提供支援,啟用執行中現代虛擬化應用程式)

WSL2 的架構
WSL (Windows Subsystem for Linux)

目前 WSL2 非正式版本,須使用 Windows 10 Insider Preview Builds 的版本.
到 Settings > Update & Security > Windows Insider Program 點擊 Get Started 同意使用測試版的 Windows
WSL2 Windows Subsystem for Linux

需選擇 Fast (可以優先使用新功能與改良功能) 才能取得最新測試版本.
WSL2 Windows Subsystem for Linux

回到 Settings > Update & Security > Windows Update 點擊 Check for updates ,就會安裝測試版本的 Windows 10.
WSL2 Windows Subsystem for Linux

需要重開機,架構的改變,需要一段長時間,並確認你的版本有大於 Build : 18917.

Microsoft Windows [Version 10.0.18990.1]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\ben>ver

Microsoft Windows [Version 10.0.18990.1]

WSL2 可以透過指令 (PowerShell) 或是用圖形介面 (Windows 設定) 安裝 Virtual Machine Platform & WSL (Windows Subsystem for Linux) ,須重新開機.

  • 指令 (PowerShell)
    透過 Windows PowerShell 執行下面指令需要有 Administrator 權限 (Windows PowerShell 右鍵 Run as Administrator)

    Windows PowerShell
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    PS C:\Windows\system32> Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
    PS C:\Windows\system32> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
    
  • 圖形介面 (Windows 設定)
    WSL2 Windows Subsystem for Linux

重開機後雖然已經安裝好了 WSL ,但還沒有安裝任何 Linux 系統,需至 Windows Store 上下載常用的 Linux 映像檔(我選擇的是 Ubuntu 18.04 LTS).

下載完成按下 launch (啟動),第一次需要設定帳號與密碼.

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: ben
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

除了 Linux 資料夾外還有 Windows 本身的磁碟也會被掛載進來 /mnt/c

ben@DESKTOP-18MHRKT:~$ mount
rootfs on / type lxfs (rw,noatime)
none on /dev type tmpfs (rw,noatime,mode=755)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,noatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,noatime)
devpts on /dev/pts type devpts (rw,nosuid,noexec,noatime,gid=5,mode=620)
none on /run type tmpfs (rw,nosuid,noexec,noatime,mode=755)
none on /run/lock type tmpfs (rw,nosuid,nodev,noexec,noatime)
none on /run/shm type tmpfs (rw,nosuid,nodev,noatime)
none on /run/user type tmpfs (rw,nosuid,nodev,noexec,noatime,mode=755)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noatime)
C: on /mnt/c type drvfs (rw,noatime,uid=1000,gid=1000,case=off)
ben@DESKTOP-18MHRKT:~$

下次要執行可以透過 執行(Run 快捷鍵 Win+R ), 輸入 bash, wsl 或是 ubuntu1804 都可以啟動 Linux Bash.

bash ( C:\Windows\System32\bash.ex )與 wsl ( C:\Windows\System32\wsl.exe ),都會進入預設的 Linux 版本. 唯有指定 ubuntu1804 ( C:\Users\ben\AppData\Local\Microsoft\WindowsApps\ubuntu1804.exe ),指定預設方式如下.

C:\Users\ben>wslconfig
Performs administrative operations on Windows Subsystem for Linux

Usage:
    /l, /list [Option]
        Lists registered distributions.
        /all - Optionally list all distributions, including distributions that
               are currently being installed or uninstalled.

        /running - List only distributions that are currently running.

    /s, /setdefault <DistributionName>
        Sets the distribution as the default.

    /t, /terminate <DistributionName>
        Terminates the distribution.

    /u, /unregister <DistributionName>
        Unregisters the distribution.

    /upgrade <DistributionName>
        Upgrades the distribution to the WslFs file system format.

也可以透過 wsl 指令來完成.

C:\Users\ben>wsl --help
Copyright (c) Microsoft Corporation. All rights reserved.

Usage: wsl.exe [Argument] [Options...] [CommandLine]

Arguments for running Linux binaries:

    If no command line is provided, wsl.exe launches the default shell.

    --exec, -e <CommandLine>
        Execute the specified command without using the default Linux shell.

    --
        Pass the remaining command line as is.

Options:
    --distribution, -d <Distro>
        Run the specified distribution.

    --user, -u <UserName>
        Run as the specified user.

Arguments for managing Windows Subsystem for Linux:

    --export <Distro> <FileName>
        Exports the distribution to a tar file.
        The filename can be - for standard output.

    --import <Distro> <InstallLocation> <FileName> [Options]
        Imports the specified tar file as a new distribution.
        The filename can be - for standard input.

        Options:
            --version <Version>
                Specifies the version to use for the new distribution.

    --list, -l [Options]
        Lists distributions.

        Options:
            --all
                List all distributions, including distributions that are currently
                being installed or uninstalled.

            --running
                List only distributions that are currently running.

            --quiet, -q
                Only show distribution names.

            --verbose, -v
                Show detailed information about all distributions.

    --set-default, -s <Distro>
        Sets the distribution as the default.

    --set-default-version <Version>
        Changes the default install version for new distributions.

    --set-version <Distro> <Version>
        Changes the version of the specified distribution.

    --shutdown
        Immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine.

    --terminate, -t <Distro>
        Terminates the specified distribution.

    --unregister <Distro>
        Unregisters the distribution.

    --help
        Display usage information.

–list, -l 檢視目前已安裝的 Linux Distributions (發行版).

C:\Users\ben>wsl -l
Windows Subsystem for Linux Distributions:
Ubuntu-18.04 (Default)
Ubuntu

–list, -l , –verbose, -v

C:\Users\ben>wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-18.04    Stopped         1
  Ubuntu          Stopped         1

–set-default, -s 改變預設使用的 Linux Distributions.

C:\Users\ben>wsl --setdefault Ubuntu

C:\Users\ben>wsl -l -v
  NAME            STATE           VERSION
* Ubuntu          Stopped         1
  Ubuntu-18.04    Stopped         1

–set-version ,設定 Linux 使用 WSL1 或 WSL2,可以使用 –set-default-version 設定固定使用 WSL1 或 WSL2.

C:\Users\ben>C:\Users\ben>wsl --set-version Ubuntu 2
Conversion in progress, this may take a few minutes...
For information on key differences with WSL 2 please visit https://aka.ms/wsl2
Conversion complete.

C:\Users\ben>wsl -l -v
  NAME            STATE           VERSION
* Ubuntu          Stopped         2
  Ubuntu-18.04    Stopped         1

–distribution, -d , 啟動一樣可以透過 wsl

C:\Users\ben>wsl -d ubuntu-18.04
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ben@DESKTOP-18MHRKT:/mnt/c/Users/ben$

WSL2 使用 Hyper-V 的虛擬化技術來運行,那安裝好 WSL2 (Windows Subsystem for Linux) 可以做什麼?
在 Windows 下的 PowerShell 提供不少指令工具可供使用,如果習慣 Linux 的 command line 工具,我們可以在 Windows 環境透過 WSL (Windows Subsystem for Linux) 來執行 Linux 的命令列工具.

執行方式如下:

  • wsl command
  • bash -c “command”
  • ubuntu1804 run command

常見範例:
Windows Powershell 下 執行結果由 Linux grep 來過濾處理.

PS C:\Windows\system32> Get-WindowsOptionalFeature -Online -FeatureName SMB1Protocol | wsl grep -i state
State            : Disabled

透過 Linux 的 find 查詢指定檔案(目錄)名稱.

PS C:\Windows\system32> wsl find /mnt/c/users -name ben
/mnt/c/users/ben
find: ‘/mnt/c/users/ben/AppData/Local/Packages/CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc/LocalState/rootfs’: Permission denied
find: ‘/mnt/c/users/ben/AppData/Local/Packages/CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc/LocalState/temp/{0820fb9f-f68b-4433-9421-643f095faf39}’: Permission denied

常見問題:
請先確認你的 CPU 處理器有支援虛擬化,如你的系統是透過虛擬機產生的,如 VMware Fusion 運行 Windows10 時,需要開啟 Nested Virtualization (巢狀虛擬化),在 Windows10 虛擬機 Settings / Processors and Memory / Advanced (進階選項) / Enable Hypervisor Applications in this virtual machine (在此虛擬機中啟用超級監督者應用程式) , Enables running modern virtualization applications by providing support for Intel VT-x/EPT inside this virtual machine (透過為此虛擬機器中的 Intel VT-x/EPT 提供支援,啟用執行中現代虛擬化應用程式),設定請參考前面.

C:\Users\ben>wsl --set-version Ubuntu 2
Conversion in progress, this may take a few minutes...
For information on key differences with WSL 2 please visit https://aka.ms/wsl2
Please enable the Virtual Machine Platform Windows feature and ensure virtualization is enabled in the BIOS.
For information please visit https://aka.ms/wsl2-install

還是使用 WSL1 ,請先確認你的 Windows 版本有大於 Build : 18917,需同意使用 Windows 10 Insider Preview Builds 才能得到最新版本,設定請參考前面.

C:\Users\ben>wsl --set-version Ubuntu 2
Invalid command line option: --set-default-version
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料