[Fixed] Failed to connect to bus: No such file or directory in systemctl: Step-by-Step Troubleshooting Guide

Quick Summary: The error System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: No such file or directory occurs when running systemctl commands inside environments where systemd is not the active init system. This is standard inside Docker containers, chroot jails, Windows Subsystem for Linux (WSL), or minimal Linux images lacking an active D-Bus system daemon (/run/dbus/system_bus_socket).

Understanding the Root Causes

In standard Linux operating systems, systemd runs as the root process (Process ID 1) and communicates with utilities like systemctl via inter-process communication sockets managed by D-Bus. When you execute systemctl start <service>, the utility attempts to connect to /run/dbus/system_bus_socket or /run/systemd/private. The operation fails if:

  • Container Process Isolation: Standard Docker and OCI containers run your entrypoint (e.g., bash, node, or python) as PID 1 instead of systemd. Containers are designed for single-process lifecycles, not full operating system init hierarchies.
  • Inactive D-Bus Service: In minimal virtual machines or rescue shells, the dbus.service daemon is stopped, uninstalled, or corrupted.
  • Chroot Jails: A chrooted root filesystem lacks mounted pseudo-filesystems (/proc, /sys, /dev, and /run).
  • Legacy WSL Distros: Older WSL2 distributions configured without explicit systemd support in /etc/wsl.conf.

Step 1: Quick Fix – Use Direct Service Commands Inside Containers

If you are inside a Docker container, avoid systemctl entirely and start services using their native entrypoint scripts or the legacy service wrapper:

# Instead of: sudo systemctl start nginx
# Use the direct service wrapper:
service nginx start

# Or invoke the daemon binary directly in foreground/background:
nginx -g "daemon off;"

# For MySQL / MariaDB:
service mysql start
# Or direct daemon:
mysqld --user=mysql &

# For Apache:
apache2ctl -D FOREGROUND

Step 2: Use Multi-Process Managers (Supervisord or Tini) in Containers

When an application container requires running multiple auxiliary services (e.g., Nginx plus PHP-FPM, or background worker plus cron), use a lightweight container-native process supervisor instead of heavy systemd:

# 1. Install supervisor inside your Dockerfile
RUN apt-get update && apt-get install -y supervisor

# 2. Create /etc/supervisor/conf.d/supervisord.conf
[supervisord]
nodaemon=true

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true

[program:php-fpm]
command=/usr/sbin/php-fpm8.1 -F
autostart=true
autorestart=true

# 3. Set the container entrypoint in Dockerfile:
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

Step 3: Enable True systemd Integration (When Explicitly Required)

If you strictly require systemd for testing Ansible roles, building system packages, or running WSL2, configure the environment for true systemd execution:

# Scenario A: Enable systemd in WSL2
# Edit /etc/wsl.conf in your WSL distro:
sudo tee /etc/wsl.conf <<EOF
[boot]
systemd=true
EOF
# In Windows PowerShell, restart WSL:
wsl.exe --shutdown

# Scenario B: Run systemd inside Docker (Testing only)
docker run -d --privileged \
  -v /sys/fs/cgroup:/sys/fs/cgroup:rw \
  --cgroupns=host \
  --name systemd-test ubuntu:22.04 /lib/systemd/systemd

Verification & Testing Steps

Inspect Process ID 1 and verify whether the D-Bus communication socket is active:

# Check the process running as PID 1
ps -p 1 -o comm=

# Expected output on standard systemd host:
# systemd (or init)
# Output inside standard container:
# bash (or sh, node, python)

# Check the D-Bus socket presence
ls -l /run/dbus/system_bus_socket

# If on a full VM, restart D-Bus to repair the socket:
sudo systemctl restart dbus.service

Troubleshooting Summary Comparison

Environment Primary Root Cause Recommended Solution
Standard Docker Container PID 1 is application process, not systemd Use service <name> start or direct binary execution
Multi-Daemon Container systemd is too heavy for container design Deploy supervisord or s6-overlay
WSL2 Distro systemd disabled in /etc/wsl.conf Add [boot] systemd=true and run wsl --shutdown
Bare-metal / Virtual Machine D-Bus daemon service stopped or crashed Start dbus daemon via sudo service dbus start

Leave a Reply