Or - how I tried to be smart and failed.

Sometimes, I forget that the WSL2 doesn't run exactly like normal Linux - systemd doesn't run, for example.
So, when I add some NFS mounts to /etc/fstab my whole distro stops responding the next time I start it up as its trying mount things it can't due to services not running.

To make matters worse, you can't jump in anywhere and edit the file back - you're locked out.

Here is one way of fixing it:

Exporting the faulty distro

WSL allows you to export your favorite distro as a tarball so you can bring it with you to other machines if you so desire. This is our savior in this case.

Create a directory for your distro - mkdir c:\Linux\Ubuntu, for example and then export using:

wsl --export Ubuntu-20.04 c:\temp\ubuntu.tar

This takes a while, depending on the size.

Fixing your mistake

Now you can open the tarball using your favorite tool - for example 7-zip will let you open it, right-click on /etc/fstab and choose edit.

Importing the fixed distro

Once you are happy with your changes, you can import this as a new WSL2 distro.

wsl --import MyFixedDistro c:\Linux\Ubuntu c:\temp\ubuntu.tar --version 2

Presto! You can now log into your newly imported (and fixed) distro - remember that your old one is still broken, so don't log into that one and think you did it wrong. You can actually just delete that by running wsl --unregister MyFaultyDistro

Fixing default user

If, when logging into your shell, you get root instead of your expected user - do the following:

  • Run id -u <your user> and take note of the id
  • Open regedit and navigate to HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\<id of the distro>
  • Change the DefaultUid value from (presumably) 0 to your user id from above. Remember to set the editor to decimal, as it defaults to hexadecimal which will not match your user id