WSL: WSL2- Ubuntu 20.04 Snap store doesn't work due to systemd dependency

I just reinstalled ubuntu on WSL2 from scratch in order to upgrade to 20.04 with a clean slate. The main issue I’ve noticed has to do with a lot of packages moving to snap rather than being maintained on apt, which is how I imagine most people here install their packages.

In fact it seems like Ubuntu 20.04 LTS replaced the ubuntu software with snapcraft entirely. So it’s obvious snap is becoming the ‘default’ of the distro.

The issue is that snap doesn’t currently seem to work on wsl2 due to it’s reliance on systemd. If I understand it correctly, the snapd service should be running in the background for it to work, which snapd already comes preinstalled, however snapd can’t start due to WSL2 not having systemd.

So if I run the command snap find node for example, it gives me the following error message:

error: cannot communicate with server: Post http://localhost/v2/snaps/hello-world: dial unix /run/snapd.socket: connect: no such file or directory

Running the command sudo service snapd start simply responds with:

snapd: unrecognized service

However snapd is already installed.

Is there any plan to get this working in the future, or is it something that is in progress?

In my development flow, I like to use the chromium-browser on WSL2 with the X410 server in order to test my web apps in both Windows and Linux browsers, since they can reveal some bugs, like say, when uploading csv files files from a Windows browser, React can’t really tell since Windows changes the MIME Type of the file, whereas Linux and Mac keep it the same and so it’s easy to handle.

Now I can’t really use chromium though, since I can’t install it without snap anymore.

EDIT: After several hours of trying to fix it I came across several other issues from about 8-10 months ago, with the most popular solution being this, which seems to make some changed to PIDs to make it work, but it just seems so hacky, and it’s not a permanent solution, as a restart will remove it.

  • Windows Version: 10.0.19613.1000
  • Ubuntu 20.04 WSL2

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 320
  • Comments: 164

Most upvoted comments

just try:

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

snap version

Here is the link to the scripted version of the workaround @sirredbeard mentioned: https://github.com/DamionGans/ubuntu-wsl2-systemd-script

just try:

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

snap version

That worked for me 😃, but i don’t understand why, can anyone explain please?

not working, always shows error: access denied (see 'snap help login') when trying to install or login

not working, always shows error: access denied (see 'snap help login') when trying to install or login

just login to your snap account/ ubuntu one account using snap login youremail@whatever.com command

This is something that is being looked at.

Implementing in a non-hacky way that can be supported long-term without a lot of secondary issues takes planning.

There is a workaround to get snaps working.

not working, always shows error: access denied (see 'snap help login') when trying to install or login

@mahfuzullah I had this problem. I executed wsl --shutdown from windows cmd and then executed those commands again. Now without any login, everything works perfectly.

just try:

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

snap version

I tried that on kali and it did not work. It basically kills the bash on the third command and I am back in the Powershell. Is there an official ETA to really fix that?

Perhaps you may wait for 22.04 release that comes with experimental systemd support. https://bugs.launchpad.net/ubuntu/+source/wsl-setup/+bug/1962556

this works on 22.04 now. i upgraded my 20.04 installation using do-release-upgrade -d.

there’s no real documentation on how to set it up, as out of the box it only works if you use the “Ubuntu 22.04” app from the MS store.

  1. you need to have the wsl-setup package installed (the upgrade did this automatically for me).
  2. you need the following in your /etc/wsl.conf file (in your Ubuntu file system, not in Windows):
    [boot]
    command="/usr/libexec/wsl-systemd"
    
  3. launch your shells with “/usr/libexec/nslogin /bin/bash”, eg from Windows:
    wsl -e /usr/libexec/nslogin /bin/bash
    
    the “Ubuntu 22.04” app does this automatically, but if you’re launching WSL another way you need to run nslogin yourself.

just try: sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME snap version

I tried that on kali and it did not work. It basically kills the bash on the third command and I am back in the Powershell. Is there an official ETA to really fix that?

I had the same problem in Pengwin under WSL2, but before it exits the shell, I get the following message:

nsenter: cannot open /proc/1189/ns/time: No such file or directory

However, running snap version show valid information. Now when I run sudo snap list, I get:

error: access denied (see 'snap help login')

When I run sudo snap login <email>, I get error: access denied (see 'snap help login').

Any ideas?

881 days after I opened this issue, WSL finally gets official systemd support. Happy to see this issue wasn’t just forgotten about.

Thanks, WSL team! I feel this issue can now be closed.

is this something that is planned to be supported natively out of the box at some point? or is it going to continue to require external manual intervention? i’d prefer to avoid messing with things that i will need to externally maintain over time.

Systemd support is now available in WSL!

I followed the simple instruction in the above article, and sudo snap install firefox worked like charm!

My /etc/wsl.conf file includes just 2 lines:

[boot]
systemd=true

just try: sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME snap version

I tried that on kali and it did not work. It basically kills the bash on the third command and I am back in the Powershell. Is there an official ETA to really fix that?

I had the same problem in Pengwin under WSL2, but before it exits the shell, I get the following message:

nsenter: cannot open /proc/1189/ns/time: No such file or directory

However, running snap version show valid information. Now when I run sudo snap list, I get:

error: access denied (see 'snap help login')

When I run sudo snap login <email>, I get error: access denied (see 'snap help login').

Any ideas?

replace exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME with exec sudo nsenter -t $(pidof systemd) -m -p su - $LOGNAME

Did you replace the placeholders with your values? You need to execute the first two commands. Then use top (or htop) to find out the PID of systemd and then use it to replace the $(pidof systemd) in the third command and replace $LOGNAME with your login name.

just try:

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

snap version

Your script worked flawlessly, many thanks!

Systemd support is now available in WSL!

I followed the simple instruction in the above article, and sudo snap install firefox worked like charm!

My /etc/wsl.conf file includes just 2 lines:

[boot]
systemd=true

Only thing that worked for me!! thanks!

exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

When I tried to execute this command, nsenter: failed to execute 69: No such file or directory

[process exited with code 127 (0x0000007f)]

That worked for me 😃, but i don’t understand why, can anyone explain please?

see https://discourse.ubuntu.com/t/using-snapd-in-wsl2/12113

Systemd support is now available in WSL!

I followed the simple instruction in the above article, and sudo snap install firefox worked like charm!

My /etc/wsl.conf file includes just 2 lines:

[boot]
systemd=true

can confirm this works!

Perhaps you may wait for 22.04 release that comes with experimental systemd support. https://bugs.launchpad.net/ubuntu/+source/wsl-setup/+bug/1962556

this works on 22.04 now. i upgraded my 20.04 installation using do-release-upgrade -d. there’s no real documentation on how to set it up, as out of the box it only works if you use the “Ubuntu 22.04” app from the MS store.

  1. you need to have the wsl-setup package installed (the upgrade did this automatically for me).

  2. you need the following in your /etc/wsl.conf file (in your Ubuntu file system, not in Windows):

    [boot]
    command="/usr/libexec/wsl-systemd"
    
  3. launch your shells with “/usr/libexec/nslogin /bin/bash”, eg from Windows:

    wsl -e /usr/libexec/nslogin /bin/bash
    

    the “Ubuntu 22.04” app does this automatically, but if you’re launching WSL another way you need to run nslogin yourself.

Windows Terminal did the last automatically for me after updating. Inside VSCode, I needed to add this to settings.json:

 "terminal.integrated.defaultProfile.linux": "zsh",
 "terminal.integrated.profiles.linux": {
  "zsh": {
   "path": "/usr/libexec/nslogin" // Adding zsh at the end doesn't work
  }
 },

Unless someone finds a more straightfoward way to do it.

For Anyone stuck on

ERROR: Systemd is not running. Please terminate this instance by running “wsl -t <distro>” from Windows shell and try again.

https://docs.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings states

The Boot setting is only available on Windows 11.

if anybody knows of a way to make this available on win 10 i wouldn’t mind at all

Did you replace the placeholders with your values? You need to execute the first two commands. Then use top (or htop) to find out the PID of systemd and then use it to replace the $(pidof systemd) in the third command and replace $LOGNAME with your login name.

Yes, I have tried it with the actual PID replacing the $(pidof systemd) and my username (rob) in place of the $LOGNAME. It had the same result. I also shutdown WSL 2 with wsl --shutdown and then tried again; I got the same result. I would love any further help with this. Thanks!

For people trying to upgrade from Ubuntu 20.10 to Ubuntu21.04 (on WSL2) but failed due to snapd problem

Just to share my workarounds.

In short …

try this

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig
sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
exec sudo nsenter -m -u -i -n -p -C -r -w -t $(pidof systemd) -a su - $LOGNAME

NOTICE : the last line was changed from the original ones.

backgrouds

I bumped into this snapd problem when I upgradeing from 20.04 to 20.10. The release-upgrade process failed since snapd was not working. I fixed it with the workaround below, as everyone else might did.

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig
sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

This has been working fine but I faced the same problem when I was upgrading from 20.10 to 21.04. based on this post , I removed some namespace option from the last line of the code, and it worked.

I would appreciate any suggestion or option. Thanks in advance.

For what it’s worth, I’m using Windows 10, build 19044.2364, and I was able to install Firefox via Snap after doing wsl --update.

Confirming this works on Windows 10 😃

You need the store WSL version and you should use ubuntu 22.04 which works out of the box

Note that Ubuntu 22.04 still requires the whole /etc/wsl.conf thing to enable systemd. For a trully out-of-the-box, no-steps-required experience use Ubuntu-Preview. It also fixes a couple services that fail on WSL.

I have systemd=true in /etc/wsl.conf, and I had snap working briefly. Now I get:

Interacting with snapd is not yet supported on Windows Subsystem for Linux.
This command has been left available for documentation purposes only.

just try:

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

snap version

Crash terminal immediately after: exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

😦 … !

Also getting access denied when I run snap login, after running the workaround script and even using a custom kernel.

Important: Do not forget to exit wsl via wsl --shutdown after editing /etc/wsl.conf Just exiting wsl via exit and entering via wsl is not enough

yeah you’ll have to disable the workaround - how you do that really depends on what workaround you used. you can try launching with wsl -e /bin/sh or wsl -e /bin/bash --noprofile norc which might get you into a shell where you can remove the workaround.

I managed to:

  1. Run sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig and sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target to get systemd working
  2. Use exec sudo nsenter -m -u -i -n -p -C -r -w -t $(pidof -s systemd) -a su - $LOGNAME to upgrade from 20.04 to 21.04 (had to change from ‘lts’ to ‘normal’, it’ll give a warning telling you where to change)
  3. Change it to exec sudo nsenter -m -u -i -n -p -C -r -w -t $(pidof -s systemd) -m -p su - $LOGNAME and then upgrade again from 21.04 to 21.10

Be sure to backup your VM file though, last time I tried this I had to re-install my entire distro. (We’re supposed to update it manually like that, it’s on the Store’s page) Unfortunately we still need that command in order to use snap, even though I read somewhere that supposedly it should be supported on one of the newer Ubuntu versions.

just try:

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

snap version

Thanks, I almost lost my mind.

@sunliusi and @ride4sun and others . . .

I had to include -s[ingle shot - this instructs the program to only return one pid] switch in the following command

exec sudo nsenter -t $(pidof -s systemd) -a su - $LOGNAME

to keep it from terminating my wsl2 ubuntu 20.04 bash terminal session. On that setup pidof systemd returns three pids.

Any insights on what aspect of my standard issue wsl2 ubuntu 20.04 distribution install might be different that would cause me to have to include that switch when it appears others in this thread did not have to do that?

On my wsl2 kali-linux install pidof systemd only returns one hit so that command terminates my wsl2 kali-linux 2021.1 bash terminal session with and without the -s switch included.

just try:

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

snap version

Thanks! Works for me!

➜  ~ snap version
snap    2.46.1+20.04
snapd   2.46.1+20.04
series  16
ubuntu  20.04
kernel  5.8.3-microsoft-standard+

@mmiric-endava Do you have more info about wsl --update? Any docs maybe.

Yes, there are some details displayed by wsl --help and on Microsoft docs as well:

Update your WSL version to the latest version. Options include:

--web-download: Download the latest update from the GitHub rather than the Microsoft Store.

Essentially your wsl package is updated to the latest version.

The script you run was the fixed version ( exec sudo nsenter -m -u -i -n -p -C -r -w -t $(pidof systemd) -a su - $LOGNAME ) or the original one ( exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME )?

GitHub really should have a pin feature for issues. Successfully logged in to Snapd with that updated command. Hopefully I won’t have to run this one anymore. Ty for perfecting my WSL setup even more @chari8

just try: sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME snap version

I tried the exact steps after doing a wsl --shutdown and got the following results can someone help? Screenshot 2021-04-01 172105 What am I doing wrong??

You didn’t install snapd

find a way to retain the environment.

first, install the dependency sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig

then add following commands into the file /etc/bash.bashrc (/etc/zsh/zshrc for zsh):

SYSTEMD_EXE="/lib/systemd/systemd --system-unit=basic.target"
SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
if [ -z "$SYSTEMD_PID" ] || [ "$SYSTEMD_PID" != 1 ]; then
    if [ -z "$SYSTEMD_PID" ]; then
      sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
    fi
    # wait for systemd started
    for i in {1..10}
    do
      SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
      if [ -z "$SYSTEMD_PID" ]; then
        sleep 1
      else
        break
      fi
    done
   
    SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
    exec sudo nsenter -t $SYSTEMD_PID -a su - $LOGNAME
fi

FYI: if you can’t enter the wsl2 after changing the setting file, you can run the command wsl sh to bypass above commands.

I use this under Ubuntu 20.04.1 LTS. Maybe it doesn’t work under Pengwin. Maybe you make a mistake. I’ll show you my approach. Please reboot your pc to get a clean start point. I use the new terminal in windows.

The first command shows some output, no errors, ends with ‘done’. The second and third command show no output. snap version and snap list work fine. No need to use snap login.

Ensure you use the right PID. After the second command use htop. Use F10 to filter the output. Type systemd. There are about eleven entries. Type a space (so you are filtering for 'systemd ', not for ‘systemd’). Now there are only two entries. Use the pid for /lib/systemd/systemd.

If you done it all right I can’t help you, it seems to be a problem with Pengwin, sorry.

I went through all the steps outlined on this article Systemd support is now available in WSL! and it did not work. However I was doing this from a VSCode terminal running WSL. I opened a “normal” wsl window and it worked like a charm (Windows 10).

Exited wsl and ran wsl --update in Powershell. Docker installed with snap after that with no issues.

Systemd support is now available in WSL!

I followed the simple instruction in the above article, and sudo snap install firefox worked like charm!

My /etc/wsl.conf file includes just 2 lines:

[boot]
systemd=true

March 2023 - Still works like a charm! Thanks!

For what it’s worth, I’m using Windows 10, build 19044.2364, and I was able to install Firefox via Snap after doing wsl --update.

@Archsx

Hello,what you mean store WSL version ?

looks like WSL now officially supports systemd: https://devblogs.microsoft.com/commandline/systemd-support-is-now-available-in-wsl/

Did you read this?

I have systemd=true in /etc/wsl.conf, and I had snap working briefly. Now I get:

Interacting with snapd is not yet supported on Windows Subsystem for Linux.
This command has been left available for documentation purposes only.

Looks like this is being discussed here: https://github.com/ubuntu/WSL/issues/299

881 days after I opened this issue, WSL finally gets official systemd support.

On Windows 10 21H1, build 19043.2130, following the instructions I have edited:

$ cat /etc/wsl.conf
[boot]
systemd=true

Restarted WSL:

wsl --shutdown

But I still get the error of the original post:

$ snap list
error: cannot list snaps: cannot communicate with server: Get "http://localhost/v2/snaps": dial unix /run/snapd.socket: connect: no such file or directory

$ snap services
error: cannot communicate with server: Get "http://localhost/v2/apps?select=service": dial unix /run/snapd.socket: connect: no such file or directory
> wsl --status
Default Distribution: Ubuntu-22.04
Default Version: 2

Windows Subsystem for Linux was last updated on 26-Mar-22
WSL automatic updates are on.

Kernel version: 5.10.102.1

I tried on Windows 11 and it worked.

You don’t have to install xdg-utils. What happens if you run firefox within WSL terminal?

I get this kind of error, /usr/bin/firefox: 12: xdg-settings: not found Error: cannot open display: :0

Does Firefox work on 22.04 with https://github.com/microsoft/WSL/issues/5126#issuecomment-1111957720 ? After snap install firefox:

$ firefox
mkdir: cannot create directory ‘/run/user/1000’: Permission denied
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :0

That cannot open display thing reminds me the old days when we had no GUI support 🤔

Perhaps you may wait for 22.04 release that comes with experimental systemd support. https://bugs.launchpad.net/ubuntu/+source/wsl-setup/+bug/1962556

this works on 22.04 now. i upgraded my 20.04 installation using do-release-upgrade -d.

there’s no real documentation on how to set it up, as out of the box it only works if you use the “Ubuntu 22.04” app from the MS store.

  1. you need to have the wsl-setup package installed (the upgrade did this automatically for me).

  2. you need the following in your /etc/wsl.conf file (in your Ubuntu file system, not in Windows):

    [boot]
    command="/usr/libexec/wsl-systemd"
    
  3. launch your shells with “/usr/libexec/nslogin /bin/bash”, eg from Windows:

    wsl -e /usr/libexec/nslogin /bin/bash
    

    the “Ubuntu 22.04” app does this automatically, but if you’re launching WSL another way you need to run nslogin yourself.

Windows Terminal did the last automatically for me after updating. Inside VSCode, I needed to add this to settings.json:

 "terminal.integrated.defaultProfile.linux": "zsh",
 "terminal.integrated.profiles.linux": {
  "zsh": {
   "path": "/usr/libexec/nslogin" // Adding zsh at the end doesn't work
  }
 },

Unless someone finds a more straightfoward way to do it.

Thank you @hyoretsu . That would have been obvious if I had investigated the Heroku URL; I realize now that Heroku was throwing the “NoSuchKey” error, not snap. Great work @edmondium ! The proposed solution worked.

After running the above commands with the modification proposed by @edmondium, snap version worked, but trying to actually use snap, I then got the error below: (Windows 11 Pro + WSL2 + Ubuntu focal)

$ sudo snap install heroku --classic
error: cannot perform the following tasks:
- Run configure hook of "heroku" snap if present (run hook "configure": 
-----
heroku: Updating CLI... !
Error: HTTP Error 404 for GET https://cli-assets.heroku.com/wsl-x64
...
<li>Code: NoSuchKey</li>
<li>Message: The specified key does not exist.</li>

Here is the link to the scripted version of the workaround @sirredbeard mentioned: https://github.com/DamionGans/ubuntu-wsl2-systemd-script

This worked for me

But broke wslg… all gui apps failed

Try this command (Mine’s aliased under x11 command for quick access)

sudo rm -rf /tmp/.X11-unix && ln -s /mnt/wslg/.X11-unix /tmp/.X11-unix

just try:

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

snap version

Close command line open PowerShell and enter the following command then re-open the ubuntu command line wsl.exe --terminate ubuntu

The best solution I found so far (doesn’t fully automate the snapd start but helps a lot). Assuming you installed the daemonize dbus-user-session dependency previously mentioned in the issue, else install it. Edit your ~/.bashrc file with the following:

if [ -z "$(pidof systemd)" ]; then
    sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
fi;

alias ssnap="exec sudo nsenter -t $(pidof -s systemd) -a su - $LOGNAME"

this will create an alias to initialize the snapd service, so whenever you need to create a new terminal, connect to wsl, etc just run the command ssnap, and it will start the snapd. I tried to place this command to run directly on .bashrc but for some reason it ends up in an infinite loop.

Started getting nsenter: cannot open /proc/9286/ns/time: No such file or directory when running ssnap after upgrading Ubuntu to v21.04. Trying to start Docker throws Transport endpoint is not connected

EDIT: Apparently it is stuck in a state of endless boot

State: starting
     Jobs: 27 queued
   Failed: 12 units
    Since: Thu 2021-09-30 09:33:19 -03; 46s ago
   CGroup: /
           ├─init.scope 
           │ ├─ 862 /lib/systemd/systemd --system-unit=basic.target
           │ └─3492 /lib/systemd/systemd --system-unit=basic.target
           └─system.slice 
             ├─containerd.service 
             │ ├─1251 /usr/bin/containerd
             │ └─3873 /usr/bin/containerd
             ├─systemd-networkd.service 
             │ ├─1040 /lib/systemd/systemd-networkd
             │ └─3621 /lib/systemd/systemd-networkd
             ├─systemd-udevd.service 
             │ ├─1025 /lib/systemd/systemd-udevd
             │ └─3607 /lib/systemd/systemd-udevd
             ├─cron.service 
             │ └─1652 /usr/sbin/cron -f -P
             ├─polkit.service 
             │ ├─1095 /usr/libexec/polkitd --no-debug
             │ └─3690 /usr/libexec/polkitd --no-debug
             ├─networkd-dispatcher.service 
             │ ├─1094 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
             │ └─3687 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
             ├─accounts-daemon.service 
             │ ├─1085 /usr/lib/accountsservice/accounts-daemon
             │ └─3676 /usr/lib/accountsservice/accounts-daemon
             ├─wpa_supplicant.service 
             │ ├─1107 /sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
             │ └─3701 /sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
             ├─ModemManager.service 
             │ ├─1207 /usr/sbin/ModemManager
             │ └─3762 /usr/sbin/ModemManager
             ├─systemd-journald.service 
             │ ├─1002 /lib/systemd/systemd-journald
             │ └─3535 /lib/systemd/systemd-journald
             ├─power-profiles-daemon.service 
             │ ├─1096 /usr/libexec/power-profiles-daemon
             │ └─3692 /usr/libexec/power-profiles-daemon
             ├─unattended-upgrades.service 
             │ ├─1246 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
             │ └─3871 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
             ├─ssh.service 
             │ └─1672 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
             ├─NetworkManager.service 
             │ ├─1090 /usr/sbin/NetworkManager --no-daemon
             │ └─3683 /usr/sbin/NetworkManager --no-daemon
             ├─switcheroo-control.service 
             │ ├─1101 /usr/libexec/switcheroo-control
             │ └─3695 /usr/libexec/switcheroo-control
             ├─rsyslog.service 
             │ ├─1100 /usr/sbin/rsyslogd -n -iNONE
             │ └─3694 /usr/sbin/rsyslogd -n -iNONE
             ├─console-getty.service 
             │ └─1686 /sbin/agetty -o -p -- \u --noclear --keep-baud console 115200,38400,9600 xterm-256color
             ├─cups-browsed.service 
             │ ├─1285 /usr/sbin/cups-browsed
             │ └─3931 /usr/sbin/cups-browsed
             ├─cups.service 
             │ ├─1245 /usr/sbin/cupsd -l
             │ ├─1274 /usr/lib/cups/notifier/dbus dbus://
             │ ├─1275 /usr/lib/cups/notifier/dbus dbus://
             │ ├─1276 /usr/lib/cups/notifier/dbus dbus://
             │ ├─1277 /usr/lib/cups/notifier/dbus dbus://
             │ ├─1278 /usr/lib/cups/notifier/dbus dbus://
             │ ├─1279 /usr/lib/cups/notifier/dbus dbus://
             │ ├─1280 /usr/lib/cups/notifier/dbus dbus://
             │ ├─1281 /usr/lib/cups/notifier/dbus dbus://
             │ ├─1282 /usr/lib/cups/notifier/dbus dbus://
             │ ├─3868 /usr/sbin/cupsd -l
             │ ├─3889 /usr/lib/cups/notifier/dbus dbus://
             │ ├─3890 /usr/lib/cups/notifier/dbus dbus://
             │ ├─3891 /usr/lib/cups/notifier/dbus dbus://
             │ ├─3892 /usr/lib/cups/notifier/dbus dbus://
             │ ├─3914 /usr/lib/cups/notifier/dbus dbus://
             │ ├─3919 /usr/lib/cups/notifier/dbus dbus://
             │ ├─3920 /usr/lib/cups/notifier/dbus dbus://
             │ ├─3922 /usr/lib/cups/notifier/dbus dbus://
             │ ├─3923 /usr/lib/cups/notifier/dbus dbus://
             │ └─3930 /usr/lib/cups/notifier/dbus dbus://
             ├─systemd-resolved.service 
             │ ├─1220 /lib/systemd/systemd-resolved
             │ └─3663 /lib/systemd/systemd-resolved
             ├─udisks2.service 
             │ ├─1106 /usr/libexec/udisks2/udisksd
             │ └─3700 /usr/libexec/udisks2/udisksd
             ├─dbus.service 
             │ ├─1087 @dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
             │ └─3680 @dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
             ├─NetworkManager-wait-online.service 
             │ └─3753 /usr/bin/nm-online -s -q
             ├─system-getty.slice 
             │ └─getty@tty1.service 
             │   └─1685 /sbin/agetty -o -p -- \u --noclear tty1 linux
             ├─avahi-daemon.service 
             │ ├─1086 avahi-daemon: running [Hyoretsu-2.local]
             │ ├─1110 avahi-daemon: chroot helper
             │ ├─3678 avahi-daemon: running [Hyoretsu-2.local]
             │ └─3702 avahi-daemon: chroot helper
             └─systemd-logind.service 
               ├─1105 /lib/systemd/systemd-logind
               └─3699 /lib/systemd/systemd-logind

find a way to retain the environment.

first, install the dependency sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig

then add following commands into the file /etc/bash.bashrc (/etc/zsh/zshrc for zsh):

SYSTEMD_EXE="/lib/systemd/systemd --system-unit=basic.target"
SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
if [ -z "$SYSTEMD_PID" ] || [ "$SYSTEMD_PID" != 1 ]; then
    if [ -z "$SYSTEMD_PID" ]; then
      sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
    fi
    # wait for systemd started
    for i in {1..10}
    do
      SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
      if [ -z "$SYSTEMD_PID" ]; then
        sleep 1
      else
        break
      fi
    done
   
    SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
    exec sudo nsenter -t $SYSTEMD_PID -a su - $LOGNAME
fi

FYI: if you can’t enter the wsl2 after changing the setting file, you can run the command wsl sh to bypass above commands.

Hi! Very cool script, and it managed to make snapd work on my machine(Windows11 build 22000.100, WSL2, Ubuntu 20.04 LTS, Kernel Version 5.10.43.3)! But it seemingly breaks wslg supports meanwhile, all GUI programs reported that they cannot connect to DISPLAY :0. I have tried reinstalling my WSL distro, and wslg works again. Running the script, and wslg breaks again. Removing the script and restarting wsl instance(i.e. wsl --shutdown, logout-and-login doesn’t help) recovers wslg supports but, of course, the systemd is gone. I have searched around google and found that nobody else encounters the same problem. Isn’t this script meant to work with wslg? Or this is an unknown mysterious bug of the Dev-Channel build? Is there any workaround?

EDIT: This is fixed by running sudo mount --bind /mnt/wslg/.X11-unix /tmp/.X11-unix

just try:

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

snap version

After running this solution, I was able to get snap working BUT, VS Code Server stopped working. When I type “code” on the command prompt, I get a message asking to install “code” from snap.

just try:

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

snap version

How can I do so that I don’t have to execute these commands every time I reboot?

Here is the link to the scripted version of the workaround @sirredbeard mentioned: https://github.com/DamionGans/ubuntu-wsl2-systemd-script

broke my distro. had to delete and reinstal.