WSL 错误 System has not been booted with systemd as init system (PID 1). Can‘t operate

12 篇文章 1 订阅
5 篇文章 2 订阅

描述

项目描述
操作系统Windows 10 专业版
版本WSL 2

前情

适用与 Linux 的 Windows 的子系统(WSL) 默认情况下是无法使用 systemctl 命令,使用该命令 WSL 将抛出如下错误。

System has not been booted with systemd as init system (PID 1). Can’t operate.

我们通常可以使用 service 命令 或通过 /etc/init.d 来替代 systemctl 命令。
具体操作如下(仅部分,这里通过操作 postgresql 服务来演示):

项目systemctlservice/etc/init.d
开启systemctl start postgresqlservice postgresql start/etc/init.d/postgresql start
关闭systemctl stop postgresqlservice postgresql stop/etc/init.d/postgresql stop
状态systemctl status postgresqlservice postgresql status/etc/init.d/postgresql status

直面挑战

使用其他命令可以代替 systemctl,但这终归是一场逃亡。这不,在使用 gvm-setup 下载 gvm(即漏洞扫描工具 OpenVAS 的新名称)相关数据时,gvm-setup 非要使用 systemctl 命令来启动 postgresql,即使你已经人为启动了 postgresql。

错误信息

使 WSL2 能够使用 systemctl 命令(非临时)

1. 在该操作完成后 WSL 2 可能会发生一些变化,如 每次登录都需要输入密码(即使是普通用户),如果无法接受请不要执行下述操作或在执行完成后 “原路返回”。

普通登录需要输入密码

2. 目前 WSL 仅支持使用 WSL 2 的发行版使用 systemctl 命令,如果不知道需更改的 Linux 发行版使用的 WSL 版本,请键入如下命令进行查看。
wsl -l -v
# 或
wsl --list --verbose

查看 Linux 发行版使用的 WSL 版本
通过终端的输出可以发现,我使用的是 kali-linux,使用的版本是 WSL 2。

若你仍在使用 WSL 1,可在 cmd(以管理员身份运行)中输入如下命令升级 WSL。
wsl --update
在升级完成后,请键入如下命令以更改对应的 Linux 发行版使用的 WSL 版本。
wsl --set-version <Linux 发行版名称> 2

以 kali-linux 为例,将其使用的 WSL 升级到 WSL 2使用的命令为

wsl --set-version kali-linux 2

获取软件包 daemonize 及 fontconfig

请在进入 Linux 后,在终端输入如下命令:

apt install daemonize fontconfig

在 /etc/profile 文件末尾(并不强制)加入下述代码

SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
if [ -z "$SYSTEMD_PID" ]; then
  sudo /usr/sbin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
  SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
fi
if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
  exec sudo /usr/bin/nsenter -t $SYSTEMD_PID -a su - $LOGNAME
fi

/etc/sudoers 文件末尾(并不强制)加入下述代码

%sudo ALL=(ALL) NOPASSWD: /usr/sbin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
%sudo ALL=(ALL) NOPASSWD: /usr/bin/nsenter -t [0-9]* -a su - [a-zA-Z0-9]*

使修改生效

sudo -s
source /etc/profile

修改后遇到的一些问题

在修改后你可能会遇到下面的错误(仅在 root 用户下发生),请不要慌张,此前曾以为这种现象而认为这种方法没有效果。
问题
遇到这种现象后请先使用 exit 命令退出 Linux 终端。
退出后再次进入 Linux 终端,不要使用 su root 命令切换用户,使用 sudo -s 命令来切换为 root 用户。

此时,可正常使用 systemctl 命令。
正常使用 systemctl 命令

至于发生上述错误的具体原因暂时还未查明,如有读者清楚,还望不吝赐教。

  • 15
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
调通sina33下的AP6212A0(WIFI+BT) 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 wb4916 AT qq.com 完成时间:2017/6/28 12:13 版本:V1.3 本文参照: 《A33 wifi移植说明书.pdf》 还有就是全志R16平台的parrotv1.1的官方SDK(Android4.4.2) 1、打开AP6212的BT,关闭rtl8723bs的BT(其它都是按照比对软件的字母顺序了): [ 3.141273] Bluetooth: HCI UART driver ver 2.2 [ 3.146210] Bluetooth: HCI H4 protocol initialized [ 3.151563] Bluetooth: HCI BCSP protocol initialized [ 3.157154] usbcore: registered new interface driver btusb [ 3.163282] Bluetooth: Generic Bluetooth SDIO driver ver 0.1 [ 3.169599] Bluetooth: BlueSleep Mode Driver Ver 1.1 [ 3.175402] Bluetooth: get rtl8723bs rtl8723bs_bt_host_wake gpio failed [ 3.953017] Bluetooth: RFCOMM TTY layer initialized [ 3.958456] Bluetooth: RFCOMM socket layer initialized [ 3.964183] Bluetooth: RFCOMM ver 1.11 [ 3.968340] Bluetooth: BNEP (Ethernet Emulation) ver 1.3 [ 3.968638] [mmc]: sdc2 set ios: clk 25000000Hz bm PP pm ON vdd 3.3V width 1 timing LEGACY(SDR12) dt B [ 3.968734] [mmc]: mclk 0xf1c20090 0xc100000b [ 3.989421] Bluetooth: BNEP filters: protocol multicast [ 3.995242] Bluetooth: HIDP (Human Interface Emulation) ver 1.2 [ 4.001921] L2TP core driver, V2.0 [ 4.005706] PPPoL2TP kernel driver, V2.0 [ 4.010070] L2TP IP encapsulation support (L2TPv3) [ 4.015468] L2TP netlink interface [ 4.019264] L2TP ethernet pseudowire support (L2TPv3) [ 4.023860] [mmc]: sdc2 set ios: clk 25000000Hz bm PP pm ON vdd 3.3V width 1 timing MMC-HS(SDR20) dt B [ 4.023929] [mmc]: mclk 0xf1c20090 0xc100000b [ 4.040272] VFP support v0.3: implementor 41 architecture 2 part 30 variant 7 rev 5 [ 4.048780] ThumbEE CPU extension supported. [ 4.053550] Registering SWP/SWPB emulation handler [ 4.059269] [rfkill]: rfkill set power 1 [ 4.063652] gpio ap6xxx_bt_regon set val 0, act val 0 正常启动的BT加载: [ 3.207764] Bluetooth: HCI UART driver ver 2.2 [ 3.212725] Bluetooth: HCI H4 protocol initialized [ 3.218045] Bluetooth: HCI BCSP protocol initialized [ 3.223671] us

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BinaryMoon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值