3 个回答
2022/9/26更新:
微软的 WSL 现已支持 systemd。
WSL 中使用 systemctl 命令会报错,报错信息如下。
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
WSL 并没有通过 systemd 启动,所以无法执行该操作。但是系统中是有 systemd 命令的,命令位置在 /usr/bin/systemctl。
详情可以查看GitHub:
Systemd support for Windows Subsystem for Linux (WSL) · Issue #8036 · systemd/systemd (github.com)
如果是WSL1的话,可以升级WSL2,按以下方法启用systemd。
要使用systemctl命令 ,要先启用systemd,即system daemon 。
这里选择安装Distrod来启用systemd。
Distrod
Distrod - WSL2 Distros with Systemd!
Distrod是一个基于systemd的WSL2元发行版,它允许您在一分钟内安装Ubuntu,Arch Linux,Gentoo和许多其他带有systemd的发行版,或者使您当前的发行版以systemd运行。
Distrod还提供内置的自动启动功能和端口转发 服务。这允许您在 Windows 启动时启动 systemd 托管服务,并使其可从 Windows 外部访问。
- 适用的发行版:以下发行版经过持续测试
- Ubuntu, Debian, Arch Linux, Fedora, CentOS, AlmaLinux, Rocky Linux, Kali Linux, Linux Mint, openSUSE, Amazon Linux, Oracle Linux, Gentoo Linux
(参见Integration test on Linux (distro_name))
- 其他发行版可能有效,也可能不起作用
安装方法一:Install a New Distro
安装新的发行版。
- 请确保您的默认 WSL 版本为 2。
wsl --set-default-version 2
- 下载并解压缩 https://github.com/nullpo-head/wsl-distrod/releases/latest/download/distrod_wsl_launcher-x86_64.zip,然后双击exe文件 。
- 按照向导安装新的发行版。
- [可选]若要使发行版在 Windows 启动时启动,请运行以下命令。
sudo /opt/distrod/bin/distrod enable --start-on-windows-boot
安装方法二:Make your Current Distro Run Systemd
让您当前的发行版运行Systemd。
通过此安装,systemd 已在 WSL 2 发行版中启用。
1. 下载并运行最新的安装程序 脚本。
2. curl -L -O "https://raw.githubusercontent.com/nullpo-head/wsl-distrod/main/install.sh"
3. chmod +x install.sh
sudo ./install.sh install
此脚本安装发行版,但尚未启用它。
2. 在发行版中启用Distrod
您有两种选择。如果要在 Windows 启动时自动启动Distrod,请通过以下命令启用 Distrod
/opt/distrod/bin/distrod enable --start-on-windows-boot
否则
/opt/distrod/bin/distrod enable
如果要在以后启用自动启动,则可以再次运行 with 。enable--start-on-windows-boot
3. 重新启动发行版
关闭 WSL 的终端。打开一个新的命令提示符 窗口,然后运行以下命令。
wsl --terminate Distrod
重新打开新的WSL窗口后,shell将在systemd会话中运行。
安装完毕后,可以运行以下命令进行查询
systemctl status | cat
如果帮助到了你,不妨点个赞或者点个关注。
WSL 中暂时不支持 systemctl,所以使用下面的命令会报错。
sudo systemctl
报错信息为
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
即系统没有通过 systemd 启动,所以不能操作。
WSL 的官方文档上也提到了这一点。
目前 WSL 不支持
systemd(Linux 中的
服务管理系统
)。
但是有折中的方法。
可以把 systemctl 命令换成 /etc/init.d/
例如
sudo systemctl status docker
换成下面这样
sudo /etc/init.d/docker status
也可以使用 service 命令
sudo service docker status
本文介绍了如何在Windows子系统Linux(WSL)中解决Ubuntu22.04不能使用systemctl的问题。通过安装Distrod,可以在WSL2环境下启用systemd,支持自动启动和端口转发。同时提供了安装新发行版和使现有发行版运行systemd的步骤。
![[object Object]](https://i-blog.csdnimg.cn/blog_migrate/ea3eb6d36c173aaab4cb59235daa992d.png)
![[object Object]](https://i-blog.csdnimg.cn/blog_migrate/be28149f6e0aa886444507591a0f2e9a.jpeg)
![[object Object]](https://i-blog.csdnimg.cn/blog_migrate/ea9a6a3f45858812906ab9068b1d4420.png)
![[object Object]](https://i-blog.csdnimg.cn/blog_migrate/48da6bd427a70696d89cd71940337821.jpeg)






731

被折叠的 条评论
为什么被折叠?



