zoukankan      html  css  js  c++  java
  • 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_x64ubuntu1804.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

  • 相关阅读:
    编写一个程序的步骤
    vue实现瀑布流
    Vue 解决动态生成列表点击事件无效的问题
    筛选分类列表展示
    php实现类似慕课网,php中文网的分类功能
    在一个页面修改数据,并且ajax刷新数据列表的数据实现。
    记账小程序系统简单规划
    茶叶项目---产品的规格添加
    茶叶商城开发
    后端图片上传
  • 原文地址:https://www.cnblogs.com/weihua2020/p/13923252.html
Copyright © 2011-2022 走看看