Windows – WSL1 (Windows Subsystem for Linux)

Loading

測試環境為 Windows10 Pro (Version :1809 , Build :17763)

Microsoft Windows [Version 10.0.17763.737]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\ben>ver

Microsoft Windows [Version 10.0.17763.737]

WSL1 架構圖如下 (圖來至 https://www.youtube.com/watch?v=lwhMThePdIo )
WSL (Windows Subsystem for Linux)

WSL2 的架構就不一樣了,想試用 WSL2 請參考官方網站 – https://docs.microsoft.com/zh-tw/windows/wsl/wsl2-install
WSL (Windows Subsystem for Linux)

WSL 可以透過指令 (PowerShell) 或是用圖形介面 (Windows 設定) 安裝 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 Microsoft-Windows-Subsystem-Linux
    >>
    Do you want to restart the computer to complete this operation now?
    [Y] Yes  [N] No  [?] Help (default is "Y"): y
    
  • 圖形介面 (Windows 設定)

重開機後雖然已經安裝好了 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 ),指定預設方式如下.

PS C:\Windows\system32> wslconfig
Performs administrative operations on Windows Subsystem for Linux

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

    /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.
PS C:\Windows\system32> wslconfig /l
Windows Subsystem for Linux Distributions:
Ubuntu-18.04 (Default)

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

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

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

Arguments to run 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 <DistributionName>
        Run the specified distribution.

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

Arguments to manage Windows Subsystem for Linux:

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

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

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

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

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

    --unregister <DistributionName>
        Unregisters the distribution.

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

    --help
        Display usage information.

WSL1 並非使用 Hyper-V 的虛擬化技術( WSL2 使用 Hyper-V 的虛擬化技術來運行, 請參考 – https://benjr.tw/102092 ),而是透過 Windows Kernel API 轉譯的方式來運行,那安裝好 WSL (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
沒有解決問題,試試搜尋本站其他內容

發佈留言

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

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