windows开机启动 wsl子系统,尝试通过system账号来启动

一、如何重启wsl 系统
Restarting the Image
You may notice that reboot is no longer a usable function. Instead you will need to use PowerShell in Administrator mode.

  1. Open PowerShell as Administrator and run :
    Restart-Service LxssManager
  2. Then to re-enter the Terminal for the Ubuntu distribution you can just double click the Appx file you downloaded prior. The one used to install Ubuntu on Windows Subsystem.

二、wsl(ubuntu 18.04 ) ssh服务启动
wsl ubuntu 18.04 ssh提示:
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key

  • Restarting OpenBSD Secure Shell server sshd
    Could not load host key: /etc/ssh/ssh_host_rsa_key
    Could not load host key: /etc/ssh/ssh_host_ecdsa_key
    Could not load host key: /etc/ssh/ssh_host_ed25519_key
    解决方法如下:
    apt remove openssh-server
    apt install openssh-server 安装时提示是否保留现有的sshd_config文件,当然否
    修改sshd_config配置文件
    Port 22
    ListenAddress 0.0.0.0
    PasswordAuthentication yes
    PermitRootLogin yes
    PermitEmptyPasswords yes
    service ssh restart查看ssh服务状态,若是提示 sshd error: could not load host key 则须要从新生成 key:dpkg-reconfigure openssh-server
    当wsl子系统重新启动后,ssh状态为未运行
    如果需要允许root 空密码登录 需要设置PermitEmptyPasswords yes

三、以下3种开机启动脚本,在wsl debian9或ubuntu 18.04中测试无效
1)ntsysv 没有这个命令
2)Chkconfig 没有这个命令
--list:将目前的各项服务状态栏显示出来
--level:设置某个服务在该LEVEL下启动或者关闭
3)
/etc/rc.d/rc.local 没有这个文件
注意systemd也不能在wsl中使用

四、
尝试1:
首先想到的是通过wmic命令来定位特定wsl子系统的init进程的路径来判断特定wsl子系统是否运行(改进程在wsl子系统启动时时pid为0的首个进程)wmic process where caption="init" get executablepath /value | find "ubuntu-18.04" > count.txt(注意命令需要在管理员权限下运行)或者wmic process where caption="init" get commandline /value | find "ubuntu-18.04" > count.txt,发现不管是commandline还是executablepath值都为空,但是在任务管理器中是可以看到init的commandline还是executablepath实际值(不为空),这应该是wsl的bug。
尝试2:
找windows中任务管理工具,可以在windows命令行中过滤出相关进程,作为wsl系统启动的依据。待补充
尝试3:
可否通过反复运行bash 、wsl、子系统.exe(如ubuntu-18.04.exe)命令来进入wsl系统,但又不会fork出多余的进程?

尝试4:
目前只能通过普通的账号来执行运行wsl子系统。在任务计划中通过system账号来运行wsl子系统时无法执行成功。为了保证 wsl子系统在执行时避免干扰(通过在shell脚本中统统加入 >/dev/null 2>&1 )。通过psexec64.exe(PStools工具)来手动执行system,步骤如下:
psexec64 -i -s cmd.exe弹出运行窗口,在运行窗口中输入whoami,可以看到为system账号,在此账号下运行wsl -d debian -u root手动运行wsl子系统,提示 拒绝访问

任务计划:方式多种多样,但目前只能通过 普通账号来启动wsl子系统,通过system账号来启动wsl子系统目前没有测试成功。待继续跟踪测试

①、首先新建vbs C:Users用户名AppDataRoamingMicrosoftWindowsStart MenuProgramsStartup(可以使用cmd中的shell:startup打开)
Set ws = WScript.CreateObject("WScript.Shell")
cmd = "C:WindowsSystem32ash.exe -c ""bash /init.sh"""
'运行命令不显示cmd窗口
ws.Run cmd, 0, false
Set ws = Nothing
WScript.quit
②、在wsl的/根目录下新建init.sh

#!/bin/bash
pn=$(ps aux|grep -v grep|grep sshd|wc -l)
[ -d /var/run/sshd ] || mkdir /var/run/sshd
chmod 744 /var/run/sshd
if [ "${pn}" != "0" ]; then
    pid=$(ps aux|grep -v grep|grep /usr/sbin/sshd|awk '{print $2}')
    kill $pid
fi
/usr/sbin/sshd -D

③、ubuntu1804 config --default-user root

原文地址:https://www.cnblogs.com/weihua2020/p/13936033.html