41

I have an existing Ubuntu distro, I want a 2nd one that is a fresh Ubuntu image.

I suspect I can:

  1. export my existing distribution to tar with wsl.exe --export
  2. unregister my existing distro with wsl unregister
  3. uninstall Ubuntu from Microsoft Store
  4. reinstall Ubuntu from Microsoft Store, giving me a fresh install
  5. import from tar via wsl --import

That should give me 2 distros, my existing plus one fresh. Is there an easier/better way?

This is for latest (as of writing) Windows 10 Build 19035

0

5 Answers 5

54

Important: The following procedure works only in Windows 10 Build 18305 or above. Please make sure wsl.exe has --import option in commandline. For older Windows 10 version, try this alternative method.

Procedure:

  • First we need the rootfs tarball from Ubuntu. Open https://cloud-images.ubuntu.com/releases/ (for Ubuntu < 22.04) or https://cloud-images.ubuntu.com/wsl/ (for Ubuntu >= 22.04) in a web browser. Go to the folder with the required Ubuntu version.

  • Download the ubuntu-x.x-server-cloudimg-amd64-wsl.rootfs.tar.gz file (x.x used as version number). Make sure the file name has amd64-wsl and the file type is .tar.gz (GZIP tarball). As example, the direct link will be like this:

https://cloud-images.ubuntu.com/releases/eoan/release/ubuntu-19.10-server-cloudimg-amd64-wsl.rootfs.tar.gz
  • Now we are going to install it using wsl.exe in commandline. Open Command Prompt. The actual command format will be like this:
wsl.exe --import <Distribution Name> <Install Folder> <.TAR.GZ File Path>
  • Run the command twice to install Ubuntu with different distribution name. The folder name and distribution name must be different otherwise there will be error shown in output. Here are the two example:
wsl.exe --import DistroA FolderA ubuntu-x.x-server-cloudimg-amd64-wsl.rootfs.tar.gz
wsl.exe --import DistroB FolderB ubuntu-x.x-server-cloudimg-amd64-wsl.rootfs.tar.gz
  • The installed distributions can be executed like this wsl.exe -d DistroA.

Notes:

  • The procedure can be used with any GNU/Linux distribution user space.

  • This does not use Windows Store. So, Windows Store neither show the name nor update the installation.

  • In Windows 10 ARM64 (AArch64), use the arm64-wsl cloud image for Ubuntu.

  • In this procedure the installation folder can be accessed by other users in that same machine. If you are using a shared machine, use proper permissions.

  • If you are willing to share the installation with other users of same machine then just export this registry HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss. Then import it in other users of same machine.

Further Readings:

4
  • This will not create the non-root user like the distro from the Windows Store does. See my answer for details on creating non-root user: superuser.com/a/1531220/714595
    – yobiscus
    Mar 8, 2020 at 15:40
  • 1
    "This will not create the non-root user" -- that depends on the user and it's not the topic of the question.
    – Biswapriyo
    Mar 8, 2020 at 15:41
  • 6
    Completely agree that it depends on the user, but if the goal is to have multiple distros similar to the one created by the Windows Store, I believe it is useful supplemental information.
    – yobiscus
    Mar 8, 2020 at 15:45
  • 2
    It seems as of now some WSL-specific images are at cloud-images.ubuntu.com/wsl. The 22.04 image is about 500 MiB in size, so about 100 MiB bigger than the ubuntu-x.x-server-cloudimg-amd64-root.tar.gz images at cloud-images.ubuntu.com/releases. Not sure how they differ...
    – philb
    Jan 2, 2023 at 17:07
32

Answer by Biswapriyo works great, but additional steps are required to change the default user. Note that this requires build 18980 and above.

Add the non-root user via adduser command:

PS C:\Users\Username> wsl -d DistroA
root@DESKTOP:/mnt/c/Users/Username# NEW_USER=username
root@DESKTOP:/mnt/c/Users/Username# adduser "${NEW_USER}"
Adding user `username' ...
Adding new group `username' (1000) ...
Adding new user `username' (1000) with group `username' ...
Creating home directory `/home/username' ...
Copying files from `/etc/skel' ...
New password: ****
Retype new password: ****
passwd: password updated successfully
Changing the user information for username
Enter the new value, or press ENTER for the default
        Full Name []: User Name
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n]

Enable sudoer privileges for ${NEW_USER}:

adduser ${NEW_USER} sudo

Add set the default user in /etc/wsl.conf:

tee /etc/wsl.conf <<_EOF
[user]
default=${NEW_USER}
_EOF

Exit WSL via logout, and then issue the WSL shutdown command for the changes to take effect:

wsl --shutdown DistroA

The next time wsl -d DistroA is invoked, the ${NEW_USER} user will be active.

2
  • 2
    Just a note - wsl --shutdown will shutdown all running distros, it doesn't take any arguments. Use wsl --terminate <distro> for shutting down a specific distro. Jan 14, 2022 at 12:55
  • @TimorGruber, it seemed to work for me. I created a 2nd 22.04 and called it jammy2. After running wsl --shutdown jammy2 I did a wsl -l -v and see that the default one Ubuntu is still running while the new jammy2 is not. Maybe things have changed since January. Nov 7, 2023 at 14:24
4

I recently had the same issue, as I am working with different companies at the same time, and needed a script that could easily create a new WSL2 instance, and just as easily delete it.

Deletion by itself is not hard, as the official command wsl --unregister <distro_name> works just fine, and gets rid of the WSL2 instances perfectly, without leaving any trace.

Creation however, can be troublesome or even tedious. My script does just this: https://github.com/IAL32/WSL2-Create-Distro

The following example creates a WSL2 instance using Ubuntu20.04 (Focal Fossa) using a previously downloaded tarball (the example uses the following one: https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-wsl.rootfs.tar.gz), creates a user with username myuser and adds it to the group sudo. It also sets the user password to the one given in the pipeline, and the password for the root user, also given in the pipeline.

.\CreateLinuxDistro.ps1 -INPUT_FILENAME .\focal-server-cloudimg-amd64-wsl.rootfs.tar.gz -OUTPUT_DIRNAME "$env:LOCALAPPDATA\Packages\ubuntu2004-test-1" -OUTPUT_DISTRONAME ubuntu2004-test-1 -CREATE_USER 1 -CREATE_USER_USERNAME myuser -ADD_USER_TO_GROUP 1 -ADD_USER_TO_GROUP_NAME sudo -SET_USER_AS_DEFAULT myuser

Under the hood, if a new user is created it will also change its default shell from /bin/sh to /bin/bash.

1
  • Thanks for sharing the scripts. It helps a lot. Thanks again
    – Senthil
    May 11, 2023 at 0:37
3

Maybe a bit late, but found your post and tried a different way.

I wanted 2 x a Ubuntu 20.04 LTS image. First one was easy, just the standard one from the Windows store. For the second one I installed an Ubuntu 18.04 LTS image for the Windows Store and did an "do-release-upgrade" to upgrade it to Ubuntu 20.04 LTS.

1

I'd suggest to use Ubuntu "base" images instead (less bloated than "cloud" images):

You download rootfs and import it:

wget http://cdimage.ubuntu.com/ubuntu-base/releases/22.04/release/ubuntu-base-22.04-base-amd64.tar.gz
wsl --import my-ubuntu c:\wsl\my-ubuntu ubuntu-base-22.04-base-amd64.tar.gz --version 2

UPDATE First line of /etc/passwd or the user defined in /etc/wsl.conf + passwd define an app that starts.

To troubleshoot you can specify entry point yourself with -e: wsl -d BaseUbuntu -e /bin/sh

You can look inside rootfs archive to see which apps are in /bin / /usr/bin. After finding the shell you can run image as root:

wsl -d my-ubuntu -u root -e /bin/sh

and update default shell with chsh or other available tools.

3
  • how do you launch the base image? using the cloud one I'm able to do wsl -d NAME and get a root shell, but with the base image nothing happens (just returns to the windows prompt/powershell)
    – NickSlash
    Jun 18, 2022 at 23:19
  • @NickSlash See UPDATE
    – gavenkoa
    Jun 19, 2022 at 7:46
  • not sure what I'm doing wrong but I still get nothing (using the same image from your example). The first line of passwd is the root user and there is no wsl.conf. Looking through /bin and /usr/bin both sh and bash exist but neither will run.
    – NickSlash
    Jun 19, 2022 at 23:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .