There are at least three methods to enable systemd in WSL2:
This article mainly introduces the third method and discusses the advantages of choosing the third method in the following sections.
Installation Methods#
There are two options for installation:
- Install a new distribution with systemd
- Add systemd support to an existing distribution
The following sections describe each option.
Install a new distribution with systemd#
-
Download and extract distrod_wsl_launcher, then execute the extracted binary file.
-
Follow the prompts to install a new distribution.
-
[Optional] Use the following command to make your distribution automatically run on Windows startup:
sudo /opt/distrod/bin/distrod enable --start-on-windows-boot
Add systemd support to an existing distribution#
-
Download and run the latest installation script:
curl -L -O "https://raw.githubusercontent.com/nullpo-head/wsl-distrod/main/install.sh" chmod +x install.sh sudo ./install.sh install
-
Enable distrod in your distribution
There are two choices:
-
Run your distribution on Windows startup:
/opt/distrod/bin/distrod enable --start-on-windows-boot
-
Otherwise:
/opt/distrod/bin/distrod enable
Note: You can still make your distribution run on Windows startup by executing
/opt/distrod/bin/distrod enable --start-on-windows-boot
later.
-
-
Restart your distribution
Close your WSL window and execute the following command in PowerShell:
wsl --shutdown
Then open a new WSL window, and your shell will run in a systemd session.
Comparison with other methods#
In the official documentation, Distrod describes its working principle:
In short, Distrod is a binary that creates a simple container, starts systemd as the init process in that container, and launches your WSL session inside that container. To do this, Distrod does the following:
- Modifies the rootfs of the specific distribution to make it compatible with WSL and systemd.
- Modifies systemd services to make them compatible with WSL.
- Adds
/opt/distrod/bin/distrod
and other resources to the rootfs.- Registers the Distrod binary as the login shell.
- When Distrod is started as the login shell by the WSL init process, Distrod:
- Starts systemd in the simple container.
- Launches your actual shell inside this container.
- Establishes a bridge between the systemd session and the WSL interaction environment.
In fact, Distrod works similarly to genie and subsystemctl: it creates a container, starts systemd as PID 1, and uses a shell. It is also mentioned in their official documentation:
Running Distrod as a standalone one-shot command: In this case, Distrod works like genie and subsystemctl.
However, Distrod does more work in terms of auto-start. With the above operations, Distrod achieves the following:
- After installation and enabling, systemd will automatically start when WSL is started.
- By adding the
--start-on-windows-boot
parameter, Distrod registers a Windows task to run WSL on Windows startup.
Compared to other methods that require manually writing shell scripts for auto-start, Distrod, which is easier to install and use, is undoubtedly more favored by users.
Conclusion#
If you encounter any issues during installation or usage, you can refer to the official documentation.
If you find any errors in the article, please feel free to point them out in the comments!~