現在很多 Linux 套件都需要 systemd 來管理跟啟動服務. 由於 WSL2 Windows 下的一個 VM , 所以微軟實作了一個init (PID 1) 來做到快速啓動 WSL VM 以及作為與 windows 溝通的橋樑. 可以執行systemctl 確認是否有 systemd

  1. [root@dev workspace]# systemctl
  2. System has not been booted with systemd as init system (PID 1). Can't operate.
  3. Failed to connect to bus: Host is down
  4. [root@dev workspace]#

所幸, GitHub 上已經有幾個專案可以直接拿來建構有systemd(PID 1)環境, 這裡我們使用
DamionGans/ubuntu-wsl2-systemd-script, 只要按照說明操作就可以了

  1. [root@dev workspace]# cp -f /mnt/d/Projects/Github/linux/ubuntu-wsl2-systemd-script/ubuntu-wsl2-systemd-script.sh /root/workspace
  2. [root@dev workspace]# ls
  3. golang hello.c test.sh
  4. hello Makefile ubuntu-wsl2-systemd-script.sh
  5. [root@dev workspace]# chmod +x ubuntu-wsl2-systemd-script.sh
  1. [root@dev workspace]# bash ubuntu-wsl2-systemd-script.sh
  2. Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
  3. Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]Get:3 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
  4. Get:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
  5. Get:5 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,445 kB]
  6. Get:6 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [892 kB]
  7. Get:7 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [19.3 kB]
  8. Get:8 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [656 B]
  9. Fetched 2,692 kB in 6s (462 kB/s)
  10. Reading package lists... Done
  11. Selecting previously unselected package fontconfig.
  12. (Reading database ... 54217 files and directories currently installed.)Preparing to unpack .../fontconfig_2.13.1-2ubuntu3_amd64.deb ...
  13. Unpacking fontconfig (2.13.1-2ubuntu3) ...
  14. Setting up fontconfig (2.13.1-2ubuntu3) ...
  15. Regenerating fonts cache... done.
  16. Processing triggers for man-db (2.9.1-1) ...
  17. cp: cannot stat './start-systemd-namespace': No such file or directory
  18. cp: cannot stat './enter-systemd-namespace': No such file or directory
  19. chmod: cannot access '/usr/sbin/enter-systemd-namespace': No such file or directory
  20. /mnt/c/Windows/System32/cmd.exe: Invalid argument
  21. /mnt/c/Windows/System32/cmd.exe: Invalid argument

恭喜!!接近完整的Linux更近一步囉

原文链接

https://ithelp.ithome.com.tw/articles/10255920

  1. [root@dev workspace]# cp -f /mnt/d/Projects/Github/linux/install-sg.sh /root/workspace
  2. [root@dev workspace]# ls
  3. golang hello hello.c install-sg.sh Makefile test.sh ubuntu-wsl2-systemd-script.sh
  4. [root@dev workspace]# bash install-sg.sh

image.png

  • Exit the WSL terminal and shutdown the WSL env:

    1. PS C:\Users\Administrator> wsl --shutdown
  • To open a new WSL terminal with systemd enabled, run:

    1. PS C:\Users\Administrator>wsl genie -s
  • Prove that it works:

    1. [root@dev workspace]# sudo systemctl status time-sync.target

    image.png

    原文链接

    https://gist.github.com/djfdyuruiry/6720faa3f9fc59bfdf6284ee1f41f950