一、如何重啟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.
- Open PowerShell as Administrator and run :
Restart-Service LxssManager - 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\用戶名\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup(可以使用cmd中的shell:startup打開)
Set ws = WScript.CreateObject("WScript.Shell")
cmd = "C:\Windows\System32\bash.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