How to enable systemd on WSL2: Ubuntu 20 and CentOS 8

I have a work-around here but it doesn't work for those services explicitly require systemd:

Still using /init instead of /sbin/init on WSL2, works for regular services, not those explicitly require 'systemd'.

Find out where systemctl is:

which systemctl

Use the path to systemctl for the following commands.

Install Python 2 (if not available by default):

sudo dnf install python2 -y
sudo ln -s /usr/bin/python2 /usr/bin/python

Wrap systemctl in Python (or somehow let systemctl work with systemd not on PID 1):

sudo mv /usr/bin/systemctl /usr/bin/systemctl.old
curl https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py >temp
sudo mv temp /usr/bin/systemctl
sudo chmod +x /usr/bin/systemctl

Test systemctl:

sudo dnf install nginx -y
sudo systemctl start nginx
curl localhost
sudo systemctl stop nginx

The systemd-genie https://github.com/arkane-systems/genie is recommended for every distro supported and endorsed by systemd.io. It works both in the system (systemd --system) and user mode (systemd --user). t is well maintained and easy for integration with wsl and Windows terminal. No additional scripts are needed.


I found this to be helpful:

  • https://github.com/DamionGans/ubuntu-wsl2-systemd-script
    1. Script to install required tools
    2. Script to start systemd in separate namespace
    3. Script to run script in systemd namespace
  • https://github.com/arkane-systems/genie
    • Almost same ideology, but as single binary.
    • Requires DotNet
    • Requires machined (i.e. not working on CentOS7)