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

本文详细介绍了如何将WindowsSubsystemforLinux2(WSL2)的默认安装目录从C盘迁移到自定义位置,以释放系统盘空间。主要步骤包括:查看WSL2发行版信息、关闭WSL2、使用wsl--export导出Ubuntu-20.04到tar文件、注销Linux发行版、使用wsl--import将tar文件导入到新目录、设置默认用户以及删除原始tar文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

<think>好的,用户问的是如何安装迁移WSL。我需要先整理安装步骤,然后迁移方法。首先,安装WSL的话,记得有几种方法,比如通过PowerShell启用功能然后安装,或者从Microsoft Store下载。需要确保步骤正确,比如先启用虚拟机平台和WSL功能,然后安装发行版。可能还要提到更新到WSL2需要内核更新包的步骤。 然后是迁移部分,用户提到了两个引用,都是关于导出、卸载、导入的流程。根据引用[1]和[2],步骤大致是查看当前子系统名称,关闭WSL,导出为tar文件,卸载原系统,再导入到新路径。需要注意的是用户可能不清楚如何指定路径和名称,要详细说明命令中的参数,比如--export和--import的用法。 另外,用户可能对权限或路径问题有疑问,比如是否需要管理员权限,导出路径是否存在。应该提醒用户在PowerShell中以管理员身份运行命令,还有迁移后的默认用户设置问题,可能需要修改默认用户。 还要检查是否需要区分WSL1和WSL2,因为迁移步骤可能不同。但根据引用中的命令,可能都是通用的。不过最好还是确认一下是否需要指定版本,或者导入时是否需要--version参数。 最后,生成相关问题,可能涉及性能优化、使用场景、常见错误处理等,比如如何备份、空间不足怎么办,或者迁移后无法启动的解决方法。确保问题覆盖用户可能遇到的后续疑问。</think>### WSL安装迁移教程 #### 一、WSL安装步骤 1. **启用Windows功能** - 以管理员身份运行PowerShell,执行: ```powershell dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart ``` - 重启计算机完成功能启用[^1] 2. **安装Linux发行版** - 打开Microsoft Store,搜索并安装所需系统(如Ubuntu 22.04) - 首次启动时会自动完成初始化配置 3. **升级到WSL2(可选)** - 下载并安装[WSL2内核更新包](https://aka.ms/wsl2kernel) - 设置默认版本: ```powershell wsl --set-default-version 2 ``` #### 二、WSL系统迁移方法 1. **查看子系统信息** ```powershell wsl -l -v ``` 输出示例: ``` NAME STATE VERSION Ubuntu-22.04 Stopped 2 ``` 2. **关闭所有WSL实例** ```powershell wsl --shutdown ``` 3. **导出系统镜像** ```powershell wsl --export Ubuntu-22.04 D:\wsl_backup\ubuntu2204.tar ``` - `Ubuntu-22.04`为子系统名称 - `D:\wsl_backup\`建议使用NTFS格式分区 4. **卸载原系统** ```powershell wsl --unregister Ubuntu-22.04 ``` 5. **导入到新位置** ```powershell wsl --import Ubuntu-22.04 D:\wsl_new\ D:\wsl_backup\ubuntu2204.tar --version 2 ``` - 参数说明: - `Ubuntu-22.04`:新子系统名称 - `D:\wsl_new\`:目标存储路径 - `--version 2`:指定WSL2模式 6. **设置默认用户** 创建`D:\wsl_new\wsl.conf`文件并添加: ```ini [user] default=username ``` #### 三、验证迁移结果 ```powershell wsl -d Ubuntu-22.04 ``` 检查文件完整性和用户配置,确认系统正常运行[^2]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值