That's tough. The usual wsl -u root
recovery mechanism won't work without the root
user in /etc/passwd
, and when tetemp
was added, it became the default user (with no sudo
permissions).
I see two paths forward, at least. Either create a new instance (and copy files over) or attempt to recover the broken one:
First, after trying this in a test instance, I can still access the instance under Windows using the \\wsl$\Ubuntu
path. That won't let you fix /etc/passwd
, but it will at least let you copy out any files you need to recover from the instance.
You can install a second instance of the exact same Ubuntu distribution you installed from the Store:
Start PowerShell as an administrator
Run:
Get-ChildItem -Recurse 'C:\Program Files\WindowsApps\' | Where-Object {$_.Name -eq 'install.tar.gz' }
The output will have the location of the install.tar.gz
that was used to create the original instance.
Create a directory where the new instance will be created. I use something like %userprofile%\WSL\NewUbuntu
.
From a regular, non-admin PowerShell, run
wsl --import NewUbuntu ".\path\to\WSL\NewUbuntu" "<path_with_tarball>\intall.tar.gz" --version 2`
Start the new instance using wsl -d NewUbuntu
. You'll be root
by default.
Add your regular user with:
adduser <username>
usermod -aG adm,dialout,cdrom,floppy,sudo,audio,dip,video,plugdev,netdev <username>
This is the set of groups that is created for the default user in Ubuntu on WSL.
Make this user the default in WSL by creating /etc/wsl.conf
as mentioned in this answer.
You should now be able to copy over files from your old instance (\\wsl$\Ubuntu
) to the new one (\\wsl$\NewUbuntu
).
Ultimately, to delete the old copy, when you are sure you have all your critical files out, wsl --unregister <distroname>
.
There are possible options that I can think of that might allow you to repair/recover the existing instance. I have not tested these step-by-step, but most everything here is something I've done at one time or another:
This one I don't recommend, as it has the potential to leave the instance in an unstable state ultimately:
- Convert the instance to WSL1 using
wsl --set-version Ubuntu 1
- In Windows, navigate to the location of the WSL1 overlay file system in
%userprofile%\AppData\Local\Packages\Canonical...\LocalState\rootfs\etc\passwd
.
- Fix your
/etc/passwd
using a Windows app that handles Linux line-endings. Again, this can be problematic, as it can ultimately cause filesystem corruption in WSL. It might be easiest and safest to just delete the passwd
file, since we believe that doing so will let you log in as UID 0 again and then re-create the root
user.
- Convert it back to WSL2, if desired.
Or, use another instance to fix the first:
Create a tarball of your broken instance with wsl --export Ubuntu fixme.tar
.
Go back to the first option above where we created a new instance and do those steps. But call the instance something like "UbuntuTemp".
Inside that new (temporary) instance:
mkdir ~/fixme
cd ~/fixme
sudo tar --xattrs-include="security.capability" -xvf /mnt/c/path/to/fixme.tar
sudo cp /etc/passwd ~/fixme/etc/passwd
sudo tar --xattrs -cvf /mnt/c/path/to/fixed.tar .
Exit the temporary Ubuntu instance.
In PowerShell, create a directory for your fixed Ubuntu instance, then:
wsl --import Ubuntu20_04 "path/you/just/created" "path/to/fixed.tar" --version 2
Start the fixed (hopefully) instance with wsl -d Ubuntu20_04
.
Set your regular user as the default using /etc/wsl.conf
per this answer.
Assuming that it is working correctly, set the instance as the default using:
wsl --set-default Ubuntu20_04