Score:1

Deleted all users in WSL

nu flag

I use Wsl Ubuntu 20.04 LTS. I don't know why but all users were deleted. I created a new user. I wish I had not done

passwd:

tetemp:x:1000:100::/home/tetemp:

and now

$ adduser root
adduser: Only root may add a user or group to the system.
$ sudo adduser root
sudo: unknown user: root
sudo: unable to initialize policy plugin

I can't change passwd because it is read only.

-How can i get root back.
-Can I delete tetemp? (wsl starts as root if i can)

in flag
You could just wipe the WSL image and start over again. That would be the fastest way to get things operational again.
NotTheDr01ds avatar
vn flag
What happens when you just run `wsl` (or `wsl <distro>`)? I just set up a new Ubuntu instance, created a new (second) user, deleted everything but that user from `/etc/passwd`. After exiting, I'm still UID 0 when I start the instance back up and can edit `/etc/passwd` in that state. I *cannot*, however, start up as `wsl -u root`, but I can without `-u root`.
Atduyar avatar
nu flag
@NotTheDr01ds I can edit passwd because its read only. and i try `-u root` PS C:\Users\Kullanic> wsl -u root => User not found.
NotTheDr01ds avatar
vn flag
@Atduyar Okay, I can reproduce it now, at least. I think I had to add the user *after* deleting `/etc/passwd`. The last time I added the user while `/etc/passwd` was still intact, then deleted all lines *but* the `tetemp` user.
Score:0
vn flag

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:

    1. Start PowerShell as an administrator

    2. Run:

       Get-ChildItem -Recurse 'C:\Program Files\WindowsApps\' | Where-Object {$_.Name -eq 'install.tar.gz' }
      
    3. The output will have the location of the install.tar.gz that was used to create the original instance.

    4. Create a directory where the new instance will be created. I use something like %userprofile%\WSL\NewUbuntu.

    5. From a regular, non-admin PowerShell, run

      wsl --import NewUbuntu ".\path\to\WSL\NewUbuntu" "<path_with_tarball>\intall.tar.gz" --version 2`
      
    6. Start the new instance using wsl -d NewUbuntu. You'll be root by default.

    7. 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.

    8. 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
      
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.