18

I'm on WSL2 (Windows Subsystem for Linux, v2) and got the CentOS 8 WSL from:
https://github.com/yuk7/CentWSL

And the Ubuntu 20 WSL downloaded from Windows Store has no systemd enabled too.

Got it run as usual, however, systemd is not running, although it does exist in the file system:

$ cat /etc/*-release | grep NAME
NAME="CentOS Linux"
PRETTY_NAME="CentOS Linux 8 (Core)"
CPE_NAME="cpe:/o:centos:centos:8"

$ stat /lib/systemd/systemd
  File: /lib/systemd/systemd
  Size: 2523648         Blocks: 4936       IO Block: 4096   regular file
Device: 2h/2d   Inode: 1407374884100769  Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-05-31 13:17:53.497597100 +0700
Modify: 2020-04-10 04:52:32.000000000 +0700
Change: 2020-05-30 12:06:52.791586300 +0700
 Birth: -

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

/sbin/init is already a symbolic link to systemd:

$ stat /sbin/init
  File: /sbin/init -> ../lib/systemd/systemd
  Size: 22              Blocks: 0          IO Block: 4096   symbolic link
Device: 2h/2d   Inode: 2251799814381086  Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-10 04:52:20.000000000 +0700
Modify: 2020-04-10 04:52:20.000000000 +0700
Change: 2020-05-30 12:06:53.461589000 +0700
 Birth: -

I found 2 init files:

/init
/sbin/init --> /lib/systemd/systemd

Possibly WSL2 Linux kernel loads /init instead of /sbin/init?

How to enable systemd in this distro, for daemons like sshd, nginx, etc. to run? Need to touch /sbin/init or that kind of file?

Dan D
  • 759

4 Answers4

14

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
Dan D
  • 759
7

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.

5

I found this to be helpful:

Speeddymon
  • 242
  • 2
  • 9
1

Now wsl takes care of systemd natively: see here