wsl (1)-含wsl子系統各啟動命令


一、介紹:
WSL是“Windows Subsystem for Linux”的縮寫,顧名思義,WSL就是Windows系統的Linux子系統,其作為Windows組件搭載在Windows10周年更新(1607)后的Windows系統中。

二、實際安裝:
借鑒安裝方法:Win10手動安裝.appx格式UWP應用的方法 https://windows10.pro/install-appx-appbundle-uwp/
進入“設置 - 更新和安全 - 針對開發人員”設置界面。選中“旁加載應用”.然后我們就可以像安裝傳統的.exe程序一樣雙擊.appx或.appbundle格式的UWP應用安裝包進行安裝了,按照提示點擊“安裝”和“啟動”按鈕即可。當然,你也可以運行PowerShell命令進行安裝 ,命令格式如下:Add-AppxPackage .appx格式文件路徑及名稱.

實際安裝方法(win10 LTSC 1809 17763 1518中安裝的應該是wsl1版本而非功能更為完善的wsl2版本):
win10 LTSC 1809 17763 1518(版本查看可以通過ver或者winver命令)安裝wsl步驟:
1)在appwiz.cpl中安裝wsl 或者powershell中運行Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

2)下載https://aka.ms/wsl-ubuntu-1804 得到Ubuntu_1804.2019.522.0_x64.appx在shellpower中運行 Add-AppxPackage d:\Ubuntu_1804.2019.522.0_x64.appx(不確定這一步是否有用)。將Ubuntu_1804.2019.522.0_x64.appx文件改為Ubuntu_1804.2019.522.0_x64.zip,解壓縮到D:\Ubuntu_1804.2019.522.0_x64下,運行ubuntu1804.exe

非管理員運行ubuntu1804.exe出現下面的錯誤:
Installing, this may take a few minutes...
WslRegisterDistribution failed with error: 0x80070005
Error: 0x80070005 ?????
解決方法:運行以上的Ubuntu.exe時采用管理員權限

管理員權限安裝,ok
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: ywh
Enter new UNIX password: ywh
Retype new UNIX password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo ".
See "man sudo_root" for details.
此時就進入到了bash的界面,可以修改root密碼了,如下:
sudo passwd root

三、幾個比較重要的命令
3.1、D:\Ubuntu_1804.2019.522.0_x64\ubuntu1804.exe 其實是可以帶參數進行運行的ubuntu config --default-user root每次啟動采用root運行 這個命令只是設置好以root啟動,設置完成后還需要運行ubuntu1804.exe:
D:\Ubuntu_1804.2019.522.0_x64>ubuntu1804.exe /?
Launches or configures a Linux distribution.

ubuntu1804.exe Usage:據我的觀察:
第一個不加參數,運行后會安裝並進入shell,提示輸入非root用戶名和密碼。
第2個參數install不加--root參數,就是安裝后提示輸入非root用戶和密碼然后退出;加上install --root,直接安裝完畢,不進入shell,也不提示給root用戶設置密碼(實際上root是設置了我們不知道的初始root密碼,需要passwd修改),進入時直接bash即可。

ubuntu18.04.exe -h參數如下:
    <no args>
        Launches the user's default shell in the user's home directory.

    install [--root]
        Install the distribuiton and do not launch the shell when complete.
          --root
              Do not create a user account and leave the default user set to root.

    run <command line>
        Run the provided command line in the current working directory. If no
        command line is provided, the default shell is launched.

    config [setting [value]]
        Configure settings for this distribution.
        Settings:
          --default-user <username>
              Sets the default user to <username>. This must be an existing user.

    help
        Print usage information.

D:\Ubuntu_1804.2019.522.0_x64>ubuntu1804.exe run 'service ssh start '
/bin/bash: service ssh start : command not found
D:\Ubuntu_1804.2019.522.0_x64>ubuntu1804.exe run "bash -c 'service ssh start '"
Starting OpenBSD Secure Shell server sshd [ OK ]
D:\Ubuntu_1804.2019.522.0_x64>ubuntu1804.exe run "bash -c 'service ssh stop '"
Stopping OpenBSD Secure Shell server sshd [ OK ]
以上運行成功后,即便把ubuntu運行命令的窗口關閉,wsl還繼續運行並不會退出。

3.2、wsl的使用

使用: wsl.exe [選項] ......
選項:
    -d, --distribution <DistributionName>
        啟動指定的分發。

    -e, --exec <CommandLine>
        執行指定的 Linux 命令。其余的參數
        用作命令行執行。

    -u, --user <UserName>
        作為指定用戶運行。

    --幫助
        顯示使用信息。

    --
        停止解析參數,並將其余部分傳遞給 Linux 進程。	
wsl -h顯示幫助			

案例1:
wsl -u root -d "ubuntu-18.04" -e "service ssh stop" 沒有作用
wsl -u root -d "ubuntu-18.04" -e "service ssh stop" 沒有作用
wsl -u root -d "Ubuntu-18.04" service ssh start
錯誤: 沒有帶有所提供名稱的分發。
wsl -u root -d Ubuntu-18.04 service ssh start
Starting OpenBSD Secure Shell server sshd [ OK ]
以上命令wsl -u root -d Ubuntu-18.04 service ssh start產生1個init和1個sshd進程,注意沒有bash進程,也就是不進入shell環境命令運行后直接退出到windows環境中。多次運行相同的命令也只會有1個init和1個sshd進程。
如果想一次性運行2個service,比如wsl -u root -d Ubuntu-18.04 service ssh start service cron start只會啟動ssh不會啟動cron,要解決這個問題,可以:
分2次運行命令:wsl -u root -d Ubuntu-18.04 service ssh start及wsl -u root -d Ubuntu-18.04 service cron start或者
wsl -u root -d Ubuntu-18.04 bash -c /run.sh(啟動run.sh是bash腳本,在其中啟動ssh和cron服務)

案例2:
wsl -u root -d Ubuntu-18.04 使用root用戶進入特定的wsl子系統並運行bash(2個init和1個bash進程)
進入shell,直接關閉shell窗口或者在shell窗口使用exit退出,此時所有相關的wsl進程會退出。

案例3:
wsl -u root -d Ubuntu-18.04,此時有2個init和1個bash,不關閉shell窗口(窗口1)此時假定為狀態1,接着wsl -d Ubuntu-18.04 進入默認wsl shell(窗口2),此時多了1個init和1個bash(這里新產生的init和bash進程應該分別是上面的init和bash的子進程),如果此時關閉窗口2,那么就會恢復
為狀態1;如果此時關閉窗口1,此時也只剩2個init和1個bash進程。

案例4:
如果:wsl -d Ubuntu-18.04進入shell窗口,exit退出或shell窗口關閉,此時wsl子系統完全退出。
如果:wsl -d Ubuntu-18.04進入shell窗口,然后service sshd start后exit退出或shell窗口關閉,init、ssh等進程還在,此時可以通過wslconfig /t ubuntu-18.04殺掉init進程。

總結wsl如果不指定命令,那么就進入shell,如果指定命令,那么運行命令后就退出。 有點docker的意思。

測試開機運行2個wsl子系統 分別為Ubuntu-18.04以及debian:
在wsl Ubuntu-18.04中的/root建立run.sh,內容

#!/bin/bash
service ssh start >/dev/null 2>&1
service cron start >/dev/null 2>&1

在wsl debian中的/root建立run.sh,內容

#!/bin/bash
service ssh start >/dev/null 2>&1
service cron start >/dev/null 2>&1

3.3、 bash的使用
bash -c 'service ssh start' 只有1個init以及1個sshd進程,注意不會進入shell,所以就不會有bash進程。-結論:即使再重復運行多次也不會有fork進程(保持1個init進程和sshd進程)
root>bash 進入默認wsl系統shell后,有2個init進程和1個bash進程,如果此時exit,wsl子系統將完全退出。-結論:可以理解為一次性進入默認wsl系統的行為,關閉shell窗口或者從shell exit登出時默認wsl子系統將完全退出。

如何啟動wsl,有2種方式,直接運行Ubuntu.exe,這種方式的默認工作目錄是/home/ywh;另一種方式,在powershell或者cmd中運行wsl或bash,此時用戶進入的默認方式是cmd或者powershell所在目錄。

3.4、wslconfig
/t 參數使用:如果wsl ubuntu系統shell已經登錄了 ,此時wslconfig /t ubuntu-18.04會將已經登錄的shell踢掉。
/upgrade 參數使用如下:
D:\Ubuntu_1804.2019.522.0_x64>wslconfig /upgrade ubuntu-18.04
正在升級...
升級已成功完成。

    /l, /list [/all]
        列出已注冊的分發版。
        /all - 有選擇地列出所有分發版,包括
               當前正在安裝或卸載的分發版。

    /s, /setdefault <DistributionName>
        將該分發版設為默認。

    /t, /terminate <DistributionName>
        終止分發。

    /u, /unregister <DistributionName>
        取消分發版注冊。

    /upgrade <DistributionName>
        將分發版升級至 WslFs 文件系統格式。

四、wsl掛載U盤
掛載u盤(實際為fat32格式,實際為在windows中的路徑為e:盤),如下在wsl子系統中操作:
mkdir -p /mnt/e
mount -t drvfs e: /mnt/e
mount輸出如下:
C: on /mnt/c type drvfs (rw,noatime,uid=0,gid=0,case=off)
D: on /mnt/d type drvfs (rw,noatime,uid=0,gid=0,case=off)
e: on /mnt/e type drvfs (rw,relatime,case=off)
取消U盤掛載需要現在wsl中umount,然后在win10中彈出U盤。
參考:win10 wsl linux子系統可以掛載ntfs文件夾以及網絡磁盤 https://www.v2ex.com/t/356158
virtio-9p (drvfs) 的穩定性改進。 https://blogs.windows.com/windowsexperience/2020/06/03/announcing-windows-10-insider-preview-build-19640/

五、參考:
Win10 LTSC安裝WSL+oh-my-zsh https://www.cnblogs.com/wufeiyun/p/12095520.html
參考網站:
https://devblogs.microsoft.com/commandline/
https://ask.ms
https://docs.microsoft.com/zh-cn/windows/wsl/install-on-server


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM