将默认安装的 WSL2 迁移至指定目录

将默认安装的 WSL2 迁移至指定目录

WSL2 默认安装在 C 盘下,系统盘空间有限,推荐迁移安装目录。

1. 默认安装的 WSL2 目录

C:\Users\cheng\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx

在这里插入图片描述

2. 将安装的 WSL2 迁移至指定目录

  1. wsl --list --verbose - wsl -l -v

查看安装在 Windows 计算机上的 Linux 发行版列表,其中包括状态和运行发行版的 WSL 版本 (WSL 1 或 WSL 2)。

  1. wsl --shutdown

立即终止所有正在运行的发行版和 WSL 2 轻量级实用工具虚拟机。

Microsoft Windows [版本 10.0.22621.1265]
(c) Microsoft Corporation。保留所有权利。

C:\Users\cheng>wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2

C:\Users\cheng>
C:\Users\cheng>wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2

C:\Users\cheng>
C:\Users\cheng>wsl --shutdown

C:\Users\cheng>
C:\Users\cheng>wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-20.04    Stopped         2

C:\Users\cheng>
  1. wsl --export Ubuntu-20.04 f:\ubuntu20.04.tar

将指定 tar 文件导出为新的发行版。--export 选项后面是查看运行状态中 Linux 系统的名字。

C:\Users\cheng>wsl --export Ubuntu-20.04 f:\ubuntu2004.tar

C:\Users\cheng>

在这里插入图片描述

  1. Unregister or uninstall a Linux distribution (注销或卸载 Linux 发行版)

While Linux distributions can be installed through the Microsoft Store, they can’t be uninstalled through the store.
尽管可以通过 Microsoft Store 安装 Linux 发行版,但无法通过 Store 将其卸载。

To unregister and uninstall a WSL distribution

wsl --unregister <DistributionName>

Replacing <DistributionName> with the name of your targeted Linux distribution will unregister that distribution from WSL so it can be reinstalled or cleaned up. Caution: Once unregistered, all data, settings, and software associated with that distribution will be permanently lost. Reinstalling from the store will install a clean copy of the distribution. For example, wsl --unregister Ubuntu would remove Ubuntu from the distributions available in WSL. Running wsl --list will reveal that it is no longer listed.
如果将 <DistributionName> 替换为目标 Linux 发行版的名称,则将从 WSL 取消注册该发行版,以便可以重新安装或清理它。警告:取消注册后,与该分发版关联的所有数据、设置和软件将永久丢失。从 Store 重新安装会安装分发版的干净副本。 例如:wsl --unregister Ubuntu 将从可用于 WSL 的发行版中删除 Ubuntu。运行 wsl --list 将会显示它不再列出。

You can also uninstall the Linux distribution app on your Windows machine just like any other store application. To reinstall, find the distribution in the Microsoft Store and select Launch.
还可以像卸载任何其他应用商店应用程序一样卸载 Windows 计算机上的 Linux 发行版应用。若要重新安装,请在 Microsoft Store 中找到该发行版,然后选择 Launch

C:\Users\cheng>wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-20.04    Stopped         2

C:\Users\cheng>
C:\Users\cheng>wsl --unregister Ubuntu-20.04
正在注销...

C:\Users\cheng>
C:\Users\cheng>wsl --list --verbose
适用于 Linux 的 Windows 子系统没有已安装的分发版。
可以通过访问 Microsoft Store 来安装分发版:
https://aka.ms/wslstore

C:\Users\cheng>
  1. wsl --import Ubuntu-20.04 f:\ubuntu_20_04 f:\ubuntu2004.tar

将指定 tar 文件导入为新的发行版。

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

C:\Users\cheng>wsl --import Ubuntu-20.04 f:\ubuntu_20_04 f:\ubuntu2004.tar

C:\Users\cheng>

迁移成功之后,可在安装目录下查看 vhdx 文件。
F:\ubuntu_20_04\ext4.vhdx
在这里插入图片描述

  1. ubuntu2004.exe config --default-user yongqiang

设置默认登陆用户为安装时的用户名。

C:\Users\cheng>ubuntu2004.exe config --default-user yongqiang

C:\Users\cheng>
  1. 删除 f:\ubuntu2004.tar 文件
C:\Users\cheng>del f:\ubuntu2004.tar

C:\Users\cheng>

3. 通过终端打开 Ubuntu-20.04

  1. 在 Windows 图标 [开始] 处点击鼠标右键

在这里插入图片描述

  1. 终端 / 终端 (管理员)

在这里插入图片描述

References

https://yongqiang.blog.csdn.net/

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/129132581
今日推荐