amtoaer

晓风残月

叹息似的渺茫,你仍要保存着那真!
github
x
telegram
steam
nintendo switch
email

Enable systemd perfectly in WSL2.

There are at least three methods to enable systemd in WSL2:

  1. genie
  2. subsystemctl
  3. wsl-distrod

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:

  1. Install a new distribution with systemd
  2. Add systemd support to an existing distribution

The following sections describe each option.

Install a new distribution with systemd#

  1. Download and extract distrod_wsl_launcher, then execute the extracted binary file.

  2. Follow the prompts to install a new distribution.

  3. [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#

  1. 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
    
  2. Enable distrod in your distribution

    There are two choices:

    1. Run your distribution on Windows startup:

      /opt/distrod/bin/distrod enable --start-on-windows-boot
      
    2. 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.

  3. 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:

  1. Modifies the rootfs of the specific distribution to make it compatible with WSL and systemd.
    1. Modifies systemd services to make them compatible with WSL.
    2. Adds /opt/distrod/bin/distrod and other resources to the rootfs.
    3. Registers the Distrod binary as the login shell.
  2. When Distrod is started as the login shell by the WSL init process, Distrod:
    1. Starts systemd in the simple container.
    2. Launches your actual shell inside this container.
    3. 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:

  1. After installation and enabling, systemd will automatically start when WSL is started.
  2. 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!~

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.