WSL1 and WSL2 mutual conversion and installation path migration related issues

Table of contents

1. How to switch from WSL 1 to WSL 2?

2. How to switch back to WSL 1 from WSL 2?

3. After WSL1 is converted to WSL2, do the programs and libraries installed in WSL1 need to be reinstalled?

4. After WSL2 is converted to WSL1, do the programs and libraries installed in WSL2 need to be reinstalled?

5. How to backup WSL2?

6. Can one computer run WSL1 and WSL2 at the same time?

7. How to transfer WSL2 from C drive to D drive?

8. After importing to the D drive, is the original WSL still there? Do I have two wsls on my computer at the same time?

9. How to start a specific WSL when there are multiple WSLs?

10. There are two WSLs running on the computer, and I want to close one of them, but all of them are closed by using the --shutdown command. I think the other one is still running, how to do it?


1. How to switch from WSL 1 to WSL 2?

        During the process of switching from WSL 1 to WSL 2, the following steps need to be performed:

  1. Make sure the Windows version is 1903 or higher and virtualization is enabled.

  2. Download and install the WSL 2 update: Install WSL | Microsoft Learn

  3. To set WSL 2 as the default version, you can use the following command:wsl --set-default-version 2

  4. To update the WSL 1 distribution so that it works in WSL 2, use the following command:wsl --set-version <distribution name> 2

        Note that after switching to WSL 2, the Linux kernel for WSL 2 needs to be downloaded and installed, but the Linux distribution does not need to be reinstalled. Existing WSL 1 releases will be automatically converted to WSL 2 releases.

2. How to switch back to WSL 1 from WSL 2?

WSL 2 can be converted back to WSL 1 with the following steps:

  1. Open PowerShell and run the following command to set WSL 1 as the default version:

    wsl --set-default-version 1

  2. Then, run the following command to convert the WSL 2 release to the WSL 1 release. <distribution name>Replace with the name of the distribution you want to convert:

    wsl --set-version <distribution name> 1 , if the name of WSL2 on my side is Ubuntu-20.04-e , then the command I entered is: wsl --set-version Ubuntu-20.04-e 1, after a while, the conversion will be displayed:

  3. Wait for the conversion process to complete. This process may take some time, depending on the size of the distribution and the performance of your computer.

        Note that the conversion process will remove all state and files from WSL 2 and convert them to WSL 1. If you have any programs or libraries previously installed in WSL 2, you will need to reinstall them in WSL 1.

        Also, if you have previously removed the WSL 1 state or files, you may need to reinstall the WSL 1 distribution before converting back to WSL 1. The WSL 1 release can be reinstalled from the Microsoft Store or other appropriate channels.

3. After WSL1 is converted to WSL2, do the programs and libraries installed in WSL1 need to be reinstalled?

        Generally speaking, programs and libraries installed in WSL 1 can continue to be used in WSL 2 without reinstalling.

        When upgrading from WSL 1 to WSL 2, the WSL 1 distribution is automatically converted to the WSL 2 distribution, and already installed programs and libraries will remain in the converted distribution. These programs and libraries can be run in WSL 2 in the same way as WSL 1, but with improved performance and functionality.

        However, if a program or library relies on features or configurations specific to WSL 1, it may need to be modified or reinstalled in WSL 2 as appropriate. Additionally, the Linux kernel used in WSL 2 is different from the one used in WSL 1, so some specific Linux drivers or kernel modules may need to be reinstalled or recompiled to function properly in WSL 2.

4. After WSL2 is converted to WSL1, do the programs and libraries installed in WSL2 need to be reinstalled?

        After converting WSL 2 back to WSL 1, programs and libraries installed in WSL 2 are not automatically carried over to WSL 1, so they need to be reinstalled in WSL 1.

        When installing programs or libraries in WSL 2, they will be installed in the WSL 2 file system. However, after converting WSL 2 back to WSL 1, the WSL 1 file system will replace the WSL 2 file system, so programs and libraries previously installed in WSL 2 will not be accessible in WSL 1.

        If you need to use programs and libraries in WSL 1 that were previously installed in WSL 2, you will need to reinstall them in WSL 1. The process of installing programs and libraries in WSL 1 is similar to installing them in WSL 2, just open a terminal in WSL 1 and use the appropriate package manager (such as apt-get or yum) to install them.

        Please note that switching between WSL 1 and WSL 2 may involve file system and network configuration changes, so it is best to back up important data and understand the impact of these changes before switching.

5. How to backup WSL2?

        Before backing up WSL 2, you need to understand WSL 2's file system and state storage location. In WSL 2, the Linux filesystem is stored in a virtual hard disk (VHD) at a specific location within the Windows filesystem. Additionally, WSL 2 state information is stored in a specific location in the Windows registry.

        To back up WSL 2, the following steps need to be completed:

  1. Stop the WSL 2 distribution: Run the following command in PowerShell, where <distribution name>is the name of the distribution to back up:

    wsl --terminate <distribution name>

  2. Export the VHD file of WSL 2: Run the following command in PowerShell, where <Distribution Name>is the name of the distribution to back up and <FileName>is the path and name of the export file.

    wsl --export <Distribution Name> <FileName>

  3. Backup the state information of WSL 2: Open the Windows Registry Editor and navigate to the following location: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\<distribution GUID>. Right-click this location and select Export, then select a location and name to save the exported file.

  4. Once the backup is complete, the WSL 2 distribution can be restarted: run the following command in PowerShell, where <Distribution Name>is the name of the distribution to restart:

    wsl --start <Distribution Name>

        To restore a backup, simply do the following:

  1. Copy the exported VHD file to the computer to be restored and rename it to the name of the distribution to be restored.

  2. Run the following command in PowerShell, where <Distribution Name>is the name of the distribution to restore and <FileName>is the path and name of the file exported from the previous step.

    wsl --import <Distribution Name> <InstallLocation> <FileName>

    Note that <InstallLocation>it is the installation location of the distribution to be imported. If this location does not exist, WSL 2 will automatically create it.

  3. After a successful import, the WSL 2 distribution can be started with the following command:

    wsl --start <Distribution Name>

        Note that backing up and restoring WSL 2 may take some time, depending on the size of the distribution and the performance of your computer. Also, since WSL 2 uses dynamic virtual hard disk files, the size of the backup file may exceed the actual size of the distribution.

6. Can one computer run WSL1 and WSL2 at the same time?

        A single computer can have both WSL 1 and WSL 2 installed. In Windows 10, version 2004 and later, WSL 1 and WSL 2 can be enabled or disabled using the "Turn Windows features on or off" dialog:

        To use WSL 1 and WSL 2 at the same time, first make sure the WSL feature is enabled. Open Windows PowerShell and run it as administrator, then run the following command:

  dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all

        Next, WSL 2 needs to be enabled. Open Windows PowerShell and run it as administrator, then run the following command:

   dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

        Then, the WSL 2 update kernel needs to be downloaded and installed. The latest kernel update for your version of Windows can be downloaded from Microsoft's official website. After the installation is complete, restart your computer for the changes to take effect.

        Once both WSL 1 and WSL 2 are enabled and the latest WSL 2 update kernel is installed, you can use wsl --set-version <distribution name> <versionNumber>the command to switch a specific WSL distribution to the WSL 1 or WSL 2 version. Note that switching between WSL 1 and WSL 2 versions may take some time and require a restart of the WSL distribution to take effect.

        Using WSL 1 and WSL 2 on the same computer allows running different Linux distributions and applications in different environments with greater flexibility and compatibility.

7. How to transfer WSL2 from C drive to D drive?

        To transfer the WSL 2 installation from the C drive to the D drive, you can perform the following steps:

  1. Create a new directory on the D drive to store the WSL 2 installation files. For example, you can create a folder wslnamed .

  2. Open a Windows terminal and run it with administrator privileges.

  3. Find the GUID (Global Unique Identifier) ​​of the WSL 2 distribution you want to move with wsl --list --verbosethe command .

  4. In the Windows terminal, execute the following command to move the WSL 2 installation file from the C drive to the D drive:

  wsl --export <Distribution Name> <FileName>

        where <Distribution Name>is the name of the WSL 2 distribution you want to move, such as Ubuntu, <FileName>and is the path and name of the exported file, eg D:\wsl\ubuntu.tar.

        如:wsl --export Ubuntu-20.04 D:\WSL\Ubuntu-20.04-d

        Then, execute the following command to import WSL 2 from the exported file and install it on the D drive:

  1. wsl --import <Distribution Name> <InstallLocation> <FileName>where <Distribution Name>is the name of the WSL 2 distribution to import, e.g. Ubuntu, <InstallLocation>is the path to the new WSL 2 installation folder to be created on the D drive, e.g. D:\WSL, <FileName>is the path and name of the file exported from the previous step, e.g. D:\WSL\Ubuntu-20.04-d. The total command is: wsl --import Ubuntu-20.04-d D:\WSL D:\WSL\Ubuntu-20.04-d
  2. Then, use wsl --set-default <distribution name>the command to set the newly imported WSL 2 distribution as the default distribution.

        With these steps, it is possible to move the WSL 2 installation files from the C drive to the D drive, and re-import and install the WSL 2 distribution on the D drive. Note that during this process, you will need to ensure that the data and configuration of the WSL 2 release are backed up and migrated to the new installation folder.

8. After importing to the D drive, is the original WSL still there? Do I have two wsls on my computer at the same time?

        After importing to the D drive, the original WSL 2 distribution still exists on the C drive, and a new WSL 2 distribution is created on the D drive. Therefore, there are now two WSL 2 distributions on the computer.

        The details of all installed WSL distributions, including their GUID, status, path, and default state, can be viewed using wsl --list --verbosethe command . By default, a newly imported WSL 2 distribution may not automatically become the default WSL distribution, so it may be necessary to use wsl --set-default <distribution name>the command to set it as the default WSL distribution.

        Note that when creating a new WSL release, both the WSL 2 release and the WSL 1 release can exist on the same machine and run concurrently. However, when converting a WSL 2 distribution to a WSL 1 distribution or a WSL 1 distribution to a WSL 2 distribution using wsl --set-versionthe command , the WSL distribution needs to be restarted, which may cause applications and libraries to be reinstalled and reconfigured .

9. How to start a specific WSL when there are multiple WSLs?

        If you have multiple WSL 2 distributions installed on your computer at the same time, you can use the following steps to start the desired WSL distribution:

  1. Open a Windows terminal.
  2. Run wsl --list --verbosethe command to list all installed WSL distributions and their details.
  3. Look for the name of the WSL distribution you want to launch, it's in the first column.
  4. Run wsl --distribution <distribution name>the command , where <distribution name>is the name of the WSL distribution you want to start.

        For example, if you want to start a WSL distribution named "Ubuntu" separately , you can run the following command:

   wsl --distribution Ubuntu

        Note that the default WSL distribution can also be started with the following command:

    wsl

        This command will start the default WSL distribution that was set the last time wslthe command . If you want to change the default WSL distribution, use wsl --set-default <distribution name>the command , where <distribution name>is the name of the WSL distribution you want to set as the default.

10. There are two WSLs running on the computer, and I want to close one of them, but all of them are closed by using the --shutdown command. I think the other one is still running, how to do it?

        If you want to shut down one WSL distribution, but leave the other WSL distribution running, follow these steps:

  1. Open a Windows terminal.
  2. Run wsl --listthe command to list all installed WSL distributions and their names.
  3. Find the name of the WSL distribution you want to turn off.
  4. Run wsl --terminate <distribution name>the command , where <distribution name>is the name of the WSL distribution you want to shut down.

        For example, if you want to shut down a WSL distribution named "Ubuntu", you can run the following command:

  wsl --terminate Ubuntu

        Note that this command will immediately terminate all running processes in the WSL distribution, so make sure you have saved and closed any files and applications that need to be saved.

Guess you like

Origin blog.csdn.net/qq_15969343/article/details/129112516