How To Back Up And Restore A Windows Subsystem For Linux (WSL) Distro

It’s always a good idea to back up your PC occasionally. You never know when something will go wrong, so whether you’re using Linux, Windows 11, or macOS, regular backups … Read more

Taylor Bell

Taylor Bell

Published on Jul 06, 2024

How To Back Up And Restore A Windows Subsystem For Linux (WSL) Distro

It’s always a good idea to back up your PC occasionally. You never know when something will go wrong, so whether you’re using Linux, Windows 11, or macOS, regular backups are a good thing. And if you’re using the Windows Subsystem for Linux (WSL), it’s also a good idea to back up your Linux instances.

Backing up WSL instances is very easy, and it can save you some time setting up WSL on a new PC or if you’ve reset your laptop and want to get back to a working state quickly. However, there is a small downside, as an imported WSL distribution isn’t added to the Start menu the same way as a distribution installed from the Microsoft Store would be. You’ll always have to launch that distribution through Windows Terminal.

Microsoft and Linux text with heart in the middle

Related

How to setup and install WSL 2 on Windows 10 and 11

To use Linux on your Windows PC first you need to install WSL 2. Here’s how to do just that on both Windows 10 and 11 in minutes.

How to back up a Windows Subsystem for Linux (WSL) distro

Since the Windows Subsystem for Linux is kind of like a virtual machine, backing up your data actually doesn’t require you to do anything in Linux itself. All you need to use is Windows Terminal (or Windows PowerShell).

  1. Open Windows Terminal into a PowerShell profile (it should be the default behavior).
  2. Run the command below inside PowerShell to print a list of all currently installed Linux distros. It’s important that you know the exact name to create a backup. For example, you may have Ubuntu-22.04 rather than just Ubuntu.
    wsl -l
    Screenshot of Windows Terminal displaying a list of installed Linux distros

  3. To export a distribution, use the following command, replacing (distribution) and (file path) with the name of the distribution you want to backup and the path where you want to store the backup, respectively.
    wsl --export (distribution) (file path)

    Replacing the placeholders with the correct information, you’ll get something like this:

    wsl --export Ubuntu "C:UsersjoaocDocumentsLinux backupsubuntubackup.tar"
    Screenshot of Windows Terminal exporting a WSL distribution

    Keep in mind that the path you selected needs to exist. The command won’t automatically create a folder for you.

  4. Wait a few minutes for the backup to finish.

That’s it for the backup process. The file you have now created can be used either on that same PC or another one to set up an exact copy of your Linux distro. You do this using the import command in PowerShell.

How to import a WSL distro from a backup

Unegister the Linux distro (if needed)

If you’re exporting your Linux distro, you probably want to restore it at some point. You can do this on any PC that already has WSL installed, but you can’t have installed the same distro you’re importing, otherwise the process will fail. If you’re trying to restore it on the same PC for whatever reason, and you haven’t formatted your PC, you need to unregister it first. Since WSL automatically installs Ubuntu when you first set it up, you’ll need to do this if you’re importing an Ubuntu distro.

To remove the distribution from your PC, follow these steps:

  1. Right-click the distribution on your Start menu and select Uninstall.
  2. Open Windows Terminal and type in the following command:
    wsl -v
  3. You’ll see that the version you uninstalled is still listed. To remove it completely, enter the following command, replacing with the name of the distro you want to remove:
    wsl --unregister 
    Screenshot of Windows Terminal with a command to unregister Ubuntu from WSL

You may want to wait a little bit for all the files to be properly remove and then reboot your PC to make sure all the files are properly removed.

Importing the backed up distro

To import your Linux backup, you’ll need to use the following command in Windows Terminal, replacing (distribution), (install location)and (file path) with the respective information.

wsl --import (distribution) (install location) (file path)

So, using the same information we used for the import, the command would look like this:

wsl --import Ubuntu "C:Linux" "C:UsersjoaocDocumentsLinux backupsubuntubackup.tar"
Screenshot of Windows Terminal importing a WSL backup

After a moment, your distro will be restored. Again, it won’t be added to your Start menu, but you can run the wsl -l command in Terminal to make sure it was installed.

Once it’s imported, you can launch your Linux distribution through Windows Terminal. Just enter the following command, replacing (distribution) with the name of the distro you want to launch:

wsl --distribution (distribution)

You don’t need to start over every time with WSL

That’s all there is to backing up and importing your Linux distros in WSL. It’s a relatively painless process, as long as you don’t care very much about launching Linux from the Start menu. But if you’re using Linux, there’s a good chance you don’t care about using the Start menu that much anyway.

Partager cet article

Inscrivez-vous à notre newsletter