報(bào)錯(cuò):System has not been booted with systemd as init system
很多朋友在網(wǎng)上某些教程中學(xué)習(xí)了 systemd,然后實(shí)際練習(xí)的時(shí)候,使用 systemd 命令(比如 sudo systemctl start),可能會(huì)遇到一個(gè)報(bào)錯(cuò)信息:
這是什么意思呢?翻譯過來就是:“系統(tǒng)尚未以systemd作為初始系統(tǒng)啟動(dòng)”。
為什么會(huì)出現(xiàn)這樣的報(bào)錯(cuò)信息呢?
原因是當(dāng)你嘗試使用 systemd 命令來管理 Linux 系統(tǒng)上的服務(wù)的時(shí)候,但是系統(tǒng)中根本就沒有使用 systemd,而是(很可能)使用的 SysV init (sysvinit)。
這是怎么回事呢?
如果你是在 windows 中通過 WSL 使用的 Ubuntu,默認(rèn)情況下系統(tǒng)使用的是 SysV 而不是 systemd。當(dāng)你使用 systemctl 命令(適用于有 systemd init 的系統(tǒng))的時(shí)候,系統(tǒng)自然會(huì)報(bào)錯(cuò)。
那么怎樣查看到底用的是哪個(gè) init 系統(tǒng)呢?可以使用如下命令來檢查 PID 為 1 的進(jìn)程(即系統(tǒng)運(yùn)行的第一個(gè)進(jìn)程)名稱:
它應(yīng)該在輸出中顯示 init 或 sysv(或類似的東西)。如果你看到的是 init,那么你的系統(tǒng)就沒有使用 systemd,應(yīng)該使用 init 命令。
如何修復(fù) System has not been booted with systemd 報(bào)錯(cuò)信息?
最簡(jiǎn)單的方式就是不使用 systemctl 命令,而是使用 sysvinit 命令。
sysvinit 也不復(fù)雜,它與 systemctl 命令的語(yǔ)法相似。如下表格為兩個(gè)命令的對(duì)比:
Systemd command | Sysvinit command |
systemctl start service_name | service service_name start |
systemctl stop service_name | service service_name stop |
systemctl restart service_name | service service_name restart |
systemctl status service_name | service service_name status |
systemctl enable service_name | chkconfig service_name on |
systemctl disable service_name | chkconfig service_name off |
大家在初始學(xué)習(xí)的時(shí)候,如果遇到類似的錯(cuò)誤,可以嘗試使用上面表格中等效的命令,就不會(huì)看到 "System has not been booted with systemd as init system" 這樣的報(bào)錯(cuò)信息了。