Windows wsl2 supports systemd

background

Many Linux distributions use systemd to manage program processes, but in WSL, init is used by default to manage processes.

In order to comply with long-term usage habits and save unnecessary learning costs, systemd is supported in the WSL distribution (I installed Ubuntu20.04 here), and you can use the systemctl command to start and stop the process.

related information

There are many projects that can support the use of the systemctl command and provide complete systemd:

The core principles of these projects are not much different, it is nothing more than the quality of detail processing and the number of additional functions, but the most important point may be the different levels of work.

The first three scripts all work at the shell layer in the figure and rely on external commands to execute system calls.

Genie works at the application layer. It is written in C# and executes system calls through the shell. It is one more layer than the first three.

Although the last two are also applications, they are written in Rust. They do not go through the shell, but directly execute system calls by the C system call wrapper. They have the least dependencies and only have a few hundred K after dynamic linking and compilation.

systemd

On newer Linux systems, systemd is used to manage the process and becomes the first process of the system (PID equals 1), and other processes are its child processes.

systemd provides a complete solution for system startup and management. It provides a set of commands. The letter d is the abbreviation of daemon.

init

All WSL2 distributions have init provided by Microsoft, which is an alternative to systemd and does not support the systemctl command.

WSL2 itself is run by Windows, so when you use the tree or ps command, you will see that the root process is not systemd, which will result in the inability to start the daemon of the Linux system service.

When we execute the systemctl command, it will be shown that our init system (PID 1) is not systemd, but the init provided by Microsoft.

systemctl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

Install

First, you need to confirm that you are using the WSL2 version. You can execute the following command in PowerShell to check:

wsl -l -v

If it is displayed as 1, you can use the following command to configure it, where <Version> can be changed to 2:

wsl --set-default-version <Version>

You can also use the following method to set the default Linux distribution, where <distro name> is replaced with the name of the Linux distribution to be configured.

For example, wsl --set-version Ubuntu-20.04 will set the Ubuntu20.04 distribution to use WSL2:

wsl --set-version <distro name> 

There are many supported methods found on the Internet. The simplest is the method officially provided by Microsoft, which only requires adding a configuration file.

  1. Open the PowerShell command line and update WSL.

    wsl --update
    
  2. Open Ubuntu 22.04, /etc/create a new file in the directory wsl.conf, and add the following content:

    [boot]
    systemd=true
    
  3. Close the Ubuntu window.

  4. Open the PowerShell command line and restart WSL.

    wsl --shutdown
    
    
  5. Re-open the Ubuntu distribution in Windows Terminal and enter the following command to check whether it is successful:

    systemctl
    

    The above result indicates that systemd is successfully supported.


Guess you like

Origin blog.csdn.net/qq_37770674/article/details/132451026