标签: wsl systemd
Windows Subsystem for Linux (WSL) is like a gateway drug to the world of Linux and in this guide I’d like to get you started with one of the super user distributions: Debian.
In this tutorial I am assuming the following:
you are running the latest version of Windows 11 with WSL2 installed;
You have a basic understanding of what WSL is.
You have a basic understanding of what systemd is. Just know that it’s a thing is sufficient 😊
I will be focusing on getting a Debian WSL2 VM setup with System-D, this can then help you install and run things such as Docker or NixOS package manager or snapd…
First, Install Debian and register a user using the command:
$ wsl –install -d Debian
Update and Upgrade your Debian to make sure you are on the latest release with the command:
$ sudo apt-get update && sudo apt-get upgrade
Lets enable systemd on Debian. In WSL this is done by creating a wsl.conf file inside /etc/wsl.conf . The wsl.conf file is used to configure advanced settings options for your distribution. For a full list of possible options visit the Microsoft documentation. Use the command sudo nano /etc/wsl.confto create an empty config file and copy the configuration below into your wsl.conf.
[boot]
systemd=true
Now in order for these changes to take effect, shutdown the VM by executing the command: wsl.exe –shutdown Open a new terminal to continue.
Run the command:
$ systemctl –user status
You should get the following error: Failed to connect to bus: No such file or directory
This error occurs when dbus-user-session is not configured. So let’s set this up 😎
The first thing to do is to check that the systemd-logind service is running on your host:
$ sudo service systemd-logind status
You should get the User Login Management status as “inactive”, this will look something like this:
○ systemd-logind.service - User Login Management
Loaded: loaded (/lib/systemd/system/systemd-logind.service; static)
Active: inactive (dead)
Condition: start condition failed at Wed 2023-07-12 19:36:02 BST; 6min ago
├─ ConditionPathExists=|/lib/systemd/system/dbus.service was not met
└─ ConditionPathExists=|/lib/systemd/system/dbus-broker.service was not met
Docs: man:sd-login(3)
man:systemd-logind.service(8)
man:logind.conf(5)
man:org.freedesktop.login1(5)
Jul 12 19:36:02 UserName systemd[1]: systemd-logind.service - User Login Management was skipped because no trigger condi>
Installing the dbus-user-session package should help:
$ sudo apt install dbus-user-session
Then restart your WSL VM. In a new terminal update the package:
$ sudo apt install –reinstall libpam-systemd
Now run the command sudo service systemd-logind status again and you should see systemd-logind as active:
● systemd-logind.service - User Login Management
Loaded: loaded (/lib/systemd/system/systemd-logind.service; static)
Active: active (running) since Wed 2023-07-12 19:47:04 BST; 20s ago
Docs: man:sd-login(3)
man:systemd-logind.service(8)
man:logind.conf(5)
man:org.freedesktop.login1(5)
Main PID: 130 (systemd-logind)
Status: “Processing requests…”
Tasks: 1 (limit: 16713)
Memory: 1.8M
CGroup: /system.slice/systemd-logind.service
└─130 /lib/systemd/systemd-logind
Fixing Error — Shutdown Error: Failed to connect to bus: No such file or directory
On Debian and unofficial Linux distributions on WSL you may receive an error when you run the shutdown command: ‘Failed to connect to bus: No such file or directory’.
Start by installing dbus:
$ sudo apt-get 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
At the time of writing there is a bug within WSL2 where if you enable systemd on Debian or custom Linux distributions and try to run VSCode from the Linux terminal, you will get the error: cannot execute binary file: Exec format error. To fix this you need to create the following binformat.
In wsl create the file:
$ sudo nano /usr/lib/binfmt.d/WSLInterop.conf
With the contents:
:WSLInterop:M::MZ::/init:PF
Restart binformat related systemd services using the commands:
sudo systemctl restart systemd-binfmt
sudo systemctl restart binfmt-support
If you get an error on restarting, you might need to install binformat support first:
sudo apt update
sudo apt install binfmt-support
Check config, if you see:
sudo ls -Fal /proc/sys/fs/binfmt_misc
total 0
drwxr-xr-x 2 root root 0 Mar 24 11:11 ./
dr-xr-xr-x 1 root root 0 Mar 24 11:11 ../
-rw-r–r– 1 root root 0 Mar 24 11:35 WSLInterop
-rw-r–r– 1 root root 0 Mar 24 11:35 jar
-rw-r–r– 1 root root 0 Mar 24 11:35 python3.11
–w------- 1 root root 0 Mar 24 11:35 register
-rw-r–r– 1 root root 0 Mar 24 11:35 status
sudo cat /proc/sys/fs/binfmt_misc/WSLInterop
enabled
interpreter /init
flags: PF
offset 0
magic 4d5a
Everything should work fine with systemd enabled.
Thanks for reading! I hope this has helped you get started with systemd on WSL2 😊