NOTE: If you have Windows 11 there is now an official way to do this in WSL 2, use it if possible - see MS post here (WINDOWS 11 ONLY)
This guide will enable systemd to run as normal under WSL 2. This will enable services like microk8s, docker and many more to just work during a WSL session. Note: this was tested on Windows 10 Build 2004, running Ubuntu 20.04 LTS in WSL 2.
-
To enable
systemdunder WSL we require a tool calledsystemd-genie -
Copy the contents of
install-sg.shto a new file/tmp/install-sg.sh:cd /tmp wget --content-disposition \ "https://gist.githubusercontent.com/djfdyuruiry/6720faa3f9fc59bfdf6284ee1f41f950/raw/952347f805045ba0e6ef7868b18f4a9a8dd2e47a/install-sg.sh"
-
Make it executable:
chmod +x /tmp/install-sg.sh
-
Run the new script:
/tmp/install-sg.sh && rm /tmp/install-sg.sh -
Exit the WSL terminal and shutdown the WSL env:
wsl --shutdown
-
To open a new WSL terminal with
systemdenabled, run:wsl genie -s -
Prove that it works:
sudo systemctl status time-sync.target
I don't think so @redlinejoes, maybe you can explain more.
If "no one should use this guide", how can a WSL user use systemctl any other way? In my case, I couldn't use systemctl before (because PID 1 = init != systemd) and now I can with this script.
I just flagged a simple error with the systemd command and|or the configuration. I tried a few commands (including systemctl) that didn't work before, but now with this script, are perfectly working and solved some of my problems.
I found another method to start with systemd as PID 1 which is detailed here:
https://devblogs.microsoft.com/commandline/systemd-support-is-now-available-in-wsl/
But it throws similar errors as this method, and some more like "Failed to connect to bus: No such file or directory" when tried to execute systemctl without super user.
There is any method that u recommend to solve this?