The error ‘Could not connect to bus: No such file or directory
‘ is quite common on Linux operating systems. This error can occur when managing the service through systemctl or when the computer is switched off.
Systemctl: Failed to connect to bus: No such file or directory
For example, if you try to run any command in systemctl –user format:
systemctl --user status
systemctl --user
systemctl –--start|stop <servicename>
An error appears:
Failed to connect to bus: No such file or directory
The first thing to do is to check that the systemd-logind service is running on your host:
$ sudo service systemd-logind status
If the systemd-logind service is running, installing the dbus-user-session package should help.
$ sudo apt install dbus-user-session
Then restart your host. It is also a good idea to update the package:
$ sudo apt install --reinstall libpam-systemd
If you are switching to a user session from root or another account, you will need to edit the user’s .bashrc file and add two environment variables:
export XDG_RUNTIME_DIR=/run/user/$(id -u)
export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus"
When logging in as a user via SSH, the PAM subsystem sets the necessary environment variables. This is the default setting in the sshd_config file:
UsePAM yes
Shutdown Error: Failed to connect to bus: No such file or directory
On some Linux distros (including WSL/Windows Subsystem for Linux), you may receive an error when you run the shutdown command: ‘Failed to connect to bus: No such file or directory
‘. As a result, you cannot shut down your Linux host properly.
$ sudo shutdown
[sudo] password for user:
Failed to connect to bus: No such file or directory
Check that dbus is installed on your Linux host (for Ubuntu/Debian):
$ sudo dpkg -l dbus
If dbus is missing, install it:
$ sudo apt install dbus
If dbus is installed (dbus is already the newest version), try reinstalling it:
$ sudo apt-get install --reinstall dbus
To fix the problem, you need to start the dbus daemon through systemctl with the command:
$ sudo systemctl start dbus
On older Linux versions with init-based service management (in my case it is WSL Ubuntu 20.04 LTS), use the command:
$ sudo /etc/init.d/dbus start
After starting dbus, the shutdown command is executed without error.