为什么 systemd 在 WSL 中被禁用?

why systemd is disabled in WSL?

None 的 systemd 命令在 WSL 中运行(Ubuntu Bash 18.04)。当我运行sudo systemctl is-active kubelet时,错误是output: System has not been booted with systemd as init system (PID 1). Can't operate. : running command: sudo systemctl is-active kubelet

如何在 WSL 中启用 systemd 功能?有什么方法可以去掉System has not been booted with systemd

WSL 目前不支持 Systemd。可以找到更多信息 with this GitHub issue.

此外,Docker(在撰写本文时)不能在 WSL 之上工作,这意味着 Kubelet 不会有太大用处。为了在本地 运行 Kubernetes,您可以使用 Docker for Windows which includes Kubernetes support, or you can use Minikube 到 运行 具有 Hyper-V 或 Virtualbox 的虚拟机。

使用 WSL2 时,您可以使用:

sudo service docker start

这个命令基本上执行脚本/etc/init.d/docker

可以通过脚本 /etc/default/docker.

进行一些自定义,例如指定 HTTP 代理

在 WSL2 中破解 Systemd

Systemd 在 WSL2 中不是原生的,但 Shayne 找到了破解它的方法:https://github.com/shayne/wsl2-hacks

第一次使用的体验非常不稳定,但它适用于我的大部分使用:docker、minikube (--driver=none)、systemd 服务。

PS:请注意在使用前始终连接到您的用户 (bash $USER)。否则,您将无法访问您的配置文件配置~/.profile~/.bash_profile)。


使用 Systemd 精灵

Arkane 发布了一种在 systemd 中为 WSL2 编排命名空间(或瓶)的方法:https://github.com/arkane-systems/genie

安装后通过精灵连接到您的配置文件:

genie -c bash

目前只有Ubuntu18.04和19.10可用。暂时没有Ubuntu20.04的包(这下激动了)

Docker 和 Minikube 在此配置中也可以作为原生 (--driver=none) 使用。

Windows Subsystem for Linux (WSL) 2 introduces a significant architectural change as it is a full Linux kernel built by Microsoft, allowing Linux containers to run natively without emulation.

Before you install the Docker Desktop WSL 2 backend, you must complete the following steps:

Install Windows 10, version 1903 or higher. Enable WSL 2 feature on Windows.

来源 - Docker Desktop WSL 2 backend

要找出您的设备 运行 的 Windows 版本,请按 Windows 徽标键 + R,在打开框中键入 winver,然后select 好的。

WSL 不支持 Systemd,但有一个解决方法 - Script to enable systemd support on current Ubuntu WSL2 images from the Windows store

您可以使用 bubblewrap 在 WSL2 中相当轻松地启动 systemd:

# pacman -S bubblewrap  # or apt, etc.
# bwrap --dev-bind / / --unshare-pid --as-pid-1 /usr/lib/systemd/systemd

如果成功启动,它不会向当前 TTY 打印任何内容,但如果您 运行 htop 等在另一个 TTY 中,您将看到它启动并启动了配置的服务。

然后您可以通过符号链接 /usr/lib/systemd/system/sshd.service/etc/systemd/system/multi-user.target.wants/ 手动启用 OpenSSHd。在 /root/.ssh 中配置您的密钥,启动 systemd,您应该可以通过 SSH 登录。