Fix "System has not been booted with systemd as init system"
systemctl says "System has not been booted with systemd" when PID 1 is not systemd. Start services directly in containers; on WSL, enable it in wsl.conf.
System has not been booted with systemd as init system (PID 1). Can't operate.Failed to connect to bus: Host is downsystemctl only sends requests to systemd running as PID 1, so when PID 1 is something else it can do nothing. This is common inside Docker containers and on WSL with systemd disabled. The fix is not systemctl itself but how you start the service.
[1] Check what PID 1 is. If it is not systemd, the error is expected. The container below was started with a shell script, so bash is PID 1.
# ps -p 1 -o pid,comm,args PID COMMAND COMMAND 1 bash bash /in.sh# cat /proc/1/commbashYou can also confirm you are in a container. On a real server this prints none.
# systemd-detect-virt --containerdocker[2] Pick the path by where it happens.
| Environment | PID 1 | What to do |
|---|---|---|
| Docker/Podman container | a shell or the image's CMD | [3] start the service directly |
| WSL 2 (systemd off) | WSL init | [5] enable it in wsl.conf |
| Real server or VM | should be systemd | check whether you are inside a chroot or rescue mode |
[3] In a container, use service when the package ships an init script. Ubuntu's nginx package installs /etc/init.d/nginx, so this works.
# service nginx start * Starting nginx nginx ...done.# service nginx status * nginx is runningFor programs without an init script, run the binary yourself. Test the config first.
# nginx -t && nginx && sleep 1 && ps -C nginx -o pid,args && nginx -s stopnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful PID COMMAND 3112 nginx: master process nginx[4] When building an image, make the service the container's main process. Docker's docs define the main process as the ENTRYPOINT and/or CMD at the end of the Dockerfile and recommend one service per container (Docker docs). For nginx, keep it in the foreground with CMD ["nginx", "-g", "daemon off;"] so the container does not exit immediately. If you must run several processes in one container, the same page points to a process manager such as supervisord. If you only need zombie reaping, docker run --init inserts a tiny init as PID 1.
[5] On WSL 2, add the following to /etc/wsl.conf inside the distribution and restart WSL. Per Microsoft's docs, WSL 0.67.6 or newer is required (check with wsl --version), and the current Ubuntu installed by wsl --install already uses systemd by default.
[boot]systemd=trueThen run wsl.exe --shutdown from PowerShell on the Windows side to stop all WSL instances. Every open WSL terminal and whatever runs in it ends, so save your work first. Reopen the distribution; systemctl status should show running. The docs add that Debian/Ubuntu need the systemd-sysv package as well as systemd (WSL systemd docs). To revert, set systemd=false or remove the lines and run --shutdown again.
[6] If a service still fails once systemd is running, that is a different problem. Continue with debugging a failed systemd service.