WSL2开启systemctl命令简单方法
上一级页面:index-wsl
前言
我们如果想要拥有一个 systemd 环境的话,该怎麽办呢?
本文记录如何提供systemd支持,目的是能够正常使用systemctl命令,并能够正常运行一些应用程序
提供完整systemd的项目有很多:
- diddledani/one-script-wsl2-systemd: The one-script variant of the systemd hack for WSL2 (github.com)
- ubuntu-wsl2-systemd-script
- wsl2-hacks
- genie
- subsystemd
- Angea
这些项目的核心原理并无大异,无非是细节处理的好坏和附加功能的多少,但最重要的一点可能是工作层次不同。

前三个脚本都工作在图中的 shell 层,要依赖外置命令执行系统调用。genie 工作在 应用程序 层,它使用 C# 编写,通过 shell 执行系统调用,比前三个还多了一层。最后两个虽然也是 应用程序,但它们由 Rust 编写,不经 shell,而直接由 C system call wrapper 执行系统调用,依赖最少,动态链接编译后只有几百 K。
init
我们探讨的是WSL2的发行版,而WSL2的发行版,均拥有微软提供的 init,它是一种systemd的替代方案,不支持systemctl 命令,
WSL2 本身是由 Windows 负责运行的,因此使用 tree 或 ps 命令时会看到根进程不是 systemd,这将导致无法启动 Linux 系统服务的守护进程(deamon)。
当我们执行 systemctl 命令的时候,会显示出我们的 init system (PID 1) 并非 systemd,而是微软提供的 init。
systemctl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
ps u -q 1
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 908 592 ? Sl 10:31 0:00 /init我们如果想要拥有一个 systemd 环境的话,该怎麽办呢?
本文记录如何提供systemd支持,目的是能够正常使用systemctl命令,并能够正常运行一些应用程序
(推荐)官方方案
官方现在已经支持systemd,见Advanced settings configuration in WSL | Microsoft Learn
(推荐)方案一--使用**wsl-distrod**
推荐这种方案,阅读官方的README,nullpo-head/wsl-distrod:Distrod是WSL 2的元发行版,它可以在一分钟内安装Ubuntu,Arch,Debian,Gentoo等系统。Distrod还具有Windows启动和端口转发功能的内置自动启动功能。 (github.com)
使当前发行版运行wsl-distrod
通过此安装,systemd 已在 WSL 2 发行版中启用。
- 下载并运行最新的安装程序脚本。
curl -L -O "https://raw.githubusercontent.com/nullpo-head/wsl-distrod/main/install.sh"
chmod +x install.sh
sudo ./install.sh install此脚本安装发行版,但尚未启用它。
- 在发行版中启用发行版
您有两种选择。如果要在 Windows 启动时自动启动发行版,请通过以下命令启用 distrod
/opt/distrod/bin/distrod enable --start-on-windows-boot否则,如果不在 Windows 启动时自动启动发行版
/opt/distrod/bin/distrod enable如果要在以后启用自动启动,则可以再次运行
/opt/distrod/bin/distrod enable --start-on-windows-boot- 重新启动发行版
关闭 WSL 的终端。打开一个新的命令提示符窗口,然后运行以下命令。
wsl --terminate DistroName例如
wsl -t Ubuntu重新打开新的 WSL 窗口后,shell 将在 systemd 会话中运行。
另请参见
wsl2上的systemctl 模块初始化,必做!!
有许多模块需要处理,按以下列表进行处理
如何重启系统
使用 reboot命令重新引导系统: 用例见Linux Reboot Command Example - nixCraft (cyberciti.biz)
sudo systemctl rebootsystemctl使用方法
以sshd为例,用法如下:
sudo systemctl start sshsudo systemctl stop ssh在开机时启用服务
sudo systemctl enable ssh.service在开机时禁用服务
sudo systemctl disable ssh.service查看服务是否开机启动
sudo systemctl is-enabled ssh.servicesystemctl常用命令
对于用户来说,最常用的是下面这些命令,用于启动和停止 Unit(主要是 service)。
立即启动一个服务
sudo systemctl start apache.service立即停止一个服务
# 立即停止一个服务
sudo systemctl stop apache.service重启一个服务
# 重启一个服务
sudo systemctl restart apache.service显示服务的状态
sudo systemctl status test.service在开机时启用服务
sudo systemctl enable test.service在开机时禁用服务
sudo systemctl disable test.service查看服务是否开机启动
sudo systemctl is-enabled test.service查看已启动的服务列表
sudo systemctl list-unit-files|grep enabled查看各服务的预配置(VENDOR PRESET)
sudo systemctl list-unit-files查看启动失败的服务列表
sudo systemctl --failed杀死一个服务的所有子进程
# 杀死一个服务的所有子进程
sudo systemctl kill apache.service重新加载一个服务的配置文件
# 重新加载一个服务的配置文件
sudo systemctl reload apache.service重载所有修改过的配置文件
# 重载所有修改过的配置文件
sudo systemctl daemon-reload显示某个 Unit 的所有底层参数
# 显示某个 Unit 的所有底层参数
sudo systemctl show httpd.service显示某个 Unit 的指定属性的值
# 显示某个 Unit 的指定属性的值
sudo systemctl show -p CPUShares httpd.service设置某个 Unit 的指定属性
# 设置某个 Unit 的指定属性
sudo systemctl set-property httpd.service CPUShares=500reset-failed
您可能正在寻找reset-failed:
$ Sudo systemctl reset-failed
$从systemd手册页:
reset-failed [PATTERN…]重置指定单位的“失败”状态,或者如果没有传递单位名称,则重置所有单位的状态。当某个单元以某种方式出现故障时(即进程退出时出现非零错误代码,异常终止或超时),它将自动进入“失败”状态,并记录其退出代码和状态,以供管理员自省直至服务使用此命令重新启动或重置。
参考、引用、致谢
WSL2 的 Linux 中运行 systemctl 命令 - 知乎 (zhihu.com)
在 WSL2 上获得近乎完整的 Linux 体验 | Pan Xiao's Blog (ddupan.top)
https://github.com/MicrosoftDocs/WSL/issues/457#issuecomment-751351142