{"id":6000,"date":"2025-09-19T13:53:49","date_gmt":"2025-09-19T05:53:49","guid":{"rendered":"https:\/\/benson80.eu.org\/?p=6000"},"modified":"2025-09-19T15:31:41","modified_gmt":"2025-09-19T07:31:41","slug":"%f0%9f%90%a7-%e4%b8%80%e9%94%ae%e8%bf%81%e7%a7%bb-wsl2-ubuntu-%e5%ae%9e%e4%be%8b%e5%88%b0%e6%96%b0%e7%9b%98%e7%ac%a6%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/benson80.eu.org\/?p=6000","title":{"rendered":"\u4e00\u952e\u8fc1\u79fb WSL2 Ubuntu \u5b9e\u4f8b\u5230\u65b0\u76d8\u7b26\u6559\u7a0b"},"content":{"rendered":"\n<p>\u5f88\u591a\u5c0f\u4f19\u4f34\u5b89\u88c5 WSL2 \u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u4f1a\u628a\u5b9e\u4f8b\u5b89\u88c5\u5728 <code>C:\\Users\\&lt;\u7528\u6237\u540d&gt;\\AppData\\Local\\Packages\\...<\/code> \u4e0b\uff0c\u968f\u7740\u4f7f\u7528\u65f6\u95f4\u589e\u957f\uff0cC \u76d8\u7a7a\u95f4\u4f1a\u8d8a\u6765\u8d8a\u7d27\u5f20\u3002 \u672c\u6587\u5c06\u6559\u4f60\u5982\u4f55\u7528 <strong>PowerShell \u811a\u672c\u4e00\u952e\u8fc1\u79fb<\/strong> WSL2 Ubuntu \u5b9e\u4f8b\u5230\u65b0\u78c1\u76d8\uff08\u4f8b\u5982 D \u76d8\uff09\uff0c\u540c\u65f6\u4f1a\u81ea\u52a8\u6ce8\u9500\u65e7\u5b9e\u4f8b\u5e76\u5b8c\u6210\u5bfc\u5165\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u524d\u63d0\u6761\u4ef6<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86 <strong>WSL2 \u548c Ubuntu \u5b9e\u4f8b<\/strong>\u3002 \u53ef\u4ee5\u7528\u4ee5\u4e0b\u547d\u4ee4\u786e\u8ba4\uff1awsl -l -v<\/li>\n<\/ol>\n\n\n\n<p>\u7ed3\u679c\u4f1a\u7c7b\u4f3c\uff1a<\/p>\n\n\n\n<p>NAME &nbsp; &nbsp; STATE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VERSION<br>* Ubuntu &nbsp; Running &nbsp; &nbsp; &nbsp; &nbsp; 2<\/p>\n\n\n\n<p>\ud83d\udc49 \u5176\u4e2d <code>Ubuntu<\/code> \u5c31\u662f\u5b9e\u4f8b\u7684\u540d\u5b57\uff08\u811a\u672c\u91cc\u7528\u7684 <code>$OldName<\/code>\uff09\u3002<\/p>\n\n\n\n<p>       2. \u786e\u8ba4\u76ee\u6807\u76d8\u7b26\uff08\u4f8b\u5982 <code>D:\\WSL\\Ubuntu<\/code>\uff09\u6709\u8db3\u591f\u7a7a\u95f4\u5b58\u653e\u6574\u4e2a\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u811a\u672c\u5185\u5bb9<\/h2>\n\n\n\n<p>\u628a\u4ee5\u4e0b\u811a\u672c\u4fdd\u5b58\u4e3a <code>MigrateWSL.ps1<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;#<br>.SYNOPSIS<br> &nbsp;  \u4e00\u952e\u8fc1\u79fb WSL2 Ubuntu \u5b9e\u4f8b\u5230\u65b0\u76d8\u7b26\uff08\u81ea\u52a8\u6ce8\u9500\u65e7\u5b9e\u4f8b\uff09<br>\u200b<br>.DESCRIPTION<br> &nbsp;  \u652f\u6301\u8fc1\u79fb\u76ee\u5f55\uff1aCanonicalGroupLimited.Ubuntu_79rhkp1fndgsc<br> &nbsp;  \u529f\u80fd\uff1a<br> &nbsp;  1. \u7ec8\u6b62 WSL \u5b9e\u4f8b<br> &nbsp;  2. \u5bfc\u51fa\u5b9e\u4f8b\u4e3a\u4e34\u65f6 tar \u6587\u4ef6<br> &nbsp;  3. \u6ce8\u9500\u65e7\u5b9e\u4f8b<br> &nbsp;  4. \u521b\u5efa\u76ee\u6807\u76ee\u5f55\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff09<br> &nbsp;  5. \u5bfc\u5165\u5230\u65b0\u4f4d\u7f6e<br> &nbsp;  6. \u5220\u9664\u4e34\u65f6\u5907\u4efd<br> &nbsp;  7. \u663e\u793a\u8fc1\u79fb\u540e\u5b9e\u4f8b\u72b6\u6001<br>#&gt;<br>\u200b<br># =========================<br># \u914d\u7f6e\u90e8\u5206<br># =========================<br>$OldName = \"Ubuntu\" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # WSL \u5b9e\u4f8b\u540d\u79f0\uff08\u5728 wsl -l -v \u67e5\u770b\uff09<br>$NewLocation = \"D:\\WSL\\Ubuntu\" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u65b0\u76d8\u7b26\u76ee\u6807\u8def\u5f84<br>$BackupTar = \"$env:TEMP\\Ubuntu_Backup.tar\"  # \u4e34\u65f6\u5bfc\u51fa\u6587\u4ef6<br># =========================<br>\u200b<br>try {<br> &nbsp;  Write-Host \"`n=== \u5f00\u59cb\u8fc1\u79fb WSL2 Ubuntu \u5b9e\u4f8b ===`n\"<br>\u200b<br> &nbsp;  # 1. \u7ec8\u6b62\u5b9e\u4f8b<br> &nbsp;  Write-Host \"1. \u7ec8\u6b62 WSL \u5b9e\u4f8b $OldName ...\"<br> &nbsp;  wsl --terminate $OldName<br>\u200b<br> &nbsp;  # 2. \u5bfc\u51fa\u5b9e\u4f8b\u5230\u4e34\u65f6\u6587\u4ef6<br> &nbsp;  Write-Host \"2. \u5bfc\u51fa WSL \u5b9e\u4f8b\u5230\u4e34\u65f6\u6587\u4ef6 $BackupTar ...\"<br> &nbsp;  wsl --export $OldName $BackupTar<br>\u200b<br> &nbsp;  # 3. \u6ce8\u9500\u65e7\u5b9e\u4f8b<br> &nbsp;  Write-Host \"3. \u6ce8\u9500\u65e7\u5b9e\u4f8b $OldName ...\"<br> &nbsp;  wsl --unregister $OldName<br>\u200b<br> &nbsp;  # 4. \u521b\u5efa\u76ee\u6807\u7236\u76ee\u5f55\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff09<br> &nbsp;  $ParentDir = Split-Path $NewLocation -Parent<br> &nbsp;  if (!(Test-Path $ParentDir)) {<br> &nbsp; &nbsp; &nbsp;  Write-Host \"4. \u521b\u5efa\u76ee\u6807\u7236\u76ee\u5f55 $ParentDir ...\"<br> &nbsp; &nbsp; &nbsp;  New-Item -ItemType Directory -Path $ParentDir | Out-Null<br> &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp;  Write-Host \"4. \u76ee\u6807\u7236\u76ee\u5f55 $ParentDir \u5df2\u5b58\u5728\"<br> &nbsp;  }<br>\u200b<br> &nbsp;  # 5. \u5bfc\u5165\u5b9e\u4f8b\u5230\u65b0\u4f4d\u7f6e<br> &nbsp;  Write-Host \"5. \u5bfc\u5165\u5b9e\u4f8b\u5230\u65b0\u4f4d\u7f6e $NewLocation ...\"<br> &nbsp;  wsl --import $OldName $NewLocation $BackupTar --version 2<br>\u200b<br> &nbsp;  # 6. \u5220\u9664\u4e34\u65f6\u5907\u4efd\u6587\u4ef6<br> &nbsp;  Write-Host \"6. \u5220\u9664\u4e34\u65f6\u5907\u4efd\u6587\u4ef6 $BackupTar ...\"<br> &nbsp;  Remove-Item $BackupTar -Force<br>\u200b<br> &nbsp;  # 7. \u663e\u793a\u8fc1\u79fb\u540e\u5b9e\u4f8b\u72b6\u6001<br> &nbsp;  Write-Host \"`n7. \u68c0\u67e5 WSL \u5b9e\u4f8b\u72b6\u6001 ...\"<br> &nbsp;  wsl -l -v<br>\u200b<br> &nbsp;  Write-Host \"`n=== \u8fc1\u79fb\u5b8c\u6210\uff01 ===\"<br> &nbsp;  Write-Host \"\u6ce8\u610f\uff1a\u5982\u679c\u65b0\u5b9e\u4f8b\u6b63\u5e38\u8fd0\u884c\uff0c\u53ef\u4ee5\u5220\u9664\u539f\u76ee\u5f55\u91ca\u653e C \u76d8\u7a7a\u95f4\uff1a\"<br> &nbsp;  Write-Host \"C:\\Users\\&lt;\u4f60\u7684\u7528\u6237\u540d&gt;\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\"<br>}<br>catch {<br> &nbsp;  Write-Host \"\u53d1\u751f\u9519\u8bef\uff1a\" $_.Exception.Message -ForegroundColor Red<br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4f7f\u7528\u6b65\u9aa4<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4fdd\u5b58\u811a\u672c<\/strong> \u628a\u811a\u672c\u4fdd\u5b58\u4e3a <code>D:\\MigrateWSL.ps1<\/code>\uff08\u8def\u5f84\u968f\u610f\uff09\u3002<\/li>\n\n\n\n<li><strong>\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c PowerShell<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u53f3\u952e\u5f00\u59cb\u83dc\u5355 \u2192 <strong>Windows PowerShell (\u7ba1\u7406\u5458)<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5141\u8bb8\u6267\u884c\u811a\u672c\uff08\u5982\u679c\u672a\u5f00\u542f\u8fc7\uff09<\/strong>Set-ExecutionPolicy RemoteSigned -Scope CurrentUser<\/li>\n\n\n\n<li><strong>\u6267\u884c\u8fc1\u79fb\u811a\u672c<\/strong>D:\\MigrateWSL.ps1<\/li>\n\n\n\n<li><strong>\u7b49\u5f85\u8fc1\u79fb\u5b8c\u6210<\/strong> \u811a\u672c\u4f1a\u81ea\u52a8\uff1a\n<ul class=\"wp-block-list\">\n<li>\u7ec8\u6b62\u5b9e\u4f8b<\/li>\n\n\n\n<li>\u5bfc\u51fa\u6210\u4e34\u65f6 <code>.tar<\/code><\/li>\n\n\n\n<li>\u6ce8\u9500\u65e7\u5b9e\u4f8b<\/li>\n\n\n\n<li>\u5bfc\u5165\u5230\u65b0\u76d8\u7b26<\/li>\n\n\n\n<li>\u5220\u9664\u4e34\u65f6\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u9a8c\u8bc1\u8fc1\u79fb\u662f\u5426\u6210\u529f<\/strong>wsl -l -v\u4f60\u4f1a\u770b\u5230 <code>Ubuntu<\/code> \u5df2\u7ecf\u6307\u5411\u65b0\u8def\u5f84\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u8fc1\u79fb\u540e\u7684\u68c0\u67e5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u65b0\u7684 Ubuntu \u5b9e\u4f8b\u76ee\u5f55\u5728\uff1aD:\\WSL\\Ubuntu<\/li>\n\n\n\n<li>\u65e7\u76ee\u5f55\u5728\uff1aC:\\Users\\&lt;\u4f60\u7684\u7528\u6237\u540d&gt;\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\u5982\u679c\u65b0\u5b9e\u4f8b\u8fd0\u884c\u6b63\u5e38\uff0c\u53ef\u4ee5\u653e\u5fc3\u5220\u9664\u65e7\u76ee\u5f55\u91ca\u653e\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u5e38\u89c1\u95ee\u9898<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5982\u4f55\u91cd\u65b0\u6253\u5f00\u65b0\u5b9e\u4f8b\uff1f<\/strong> \u5728\u547d\u4ee4\u884c\u8f93\u5165\uff1awsl -d Ubuntu<\/li>\n\n\n\n<li><strong>\u65e7\u76ee\u5f55\u80fd\u4e0d\u80fd\u76f4\u63a5\u5220\uff1f<\/strong> \u2705 \u53ef\u4ee5\uff0c\u4f46\u5fc5\u987b\u786e\u8ba4\u65b0\u5b9e\u4f8b\u8fd0\u884c\u6b63\u5e38\u540e\u518d\u5220\u3002<\/li>\n\n\n\n<li><strong>\u80fd\u4e0d\u80fd\u6362\u540d\u5b57\uff1f<\/strong> \u53ef\u4ee5\uff0c\u4f8b\u5982\u628a <code>$OldName<\/code> \u6539\u6210 <code>Ubuntu-24.04<\/code>\uff0c\u8fc1\u79fb\u540e\u5c31\u662f\u65b0\u540d\u5b57\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u8fd9\u6837\u4f60\u5c31\u80fd <strong>\u4e00\u952e\u8fc1\u79fb WSL2 Ubuntu \u5230\u4efb\u610f\u78c1\u76d8<\/strong>\uff0c\u65e2\u7701 C \u76d8\u7a7a\u95f4\uff0c\u4e5f\u4e0d\u7528\u624b\u52a8\u6298\u817e\u3002 \ud83d\ude80<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u5c0f\u4f19\u4f34\u5b89\u88c5 WSL2 \u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u4f1a\u628a\u5b9e\u4f8b\u5b89\u88c5 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-6000","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/6000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6000"}],"version-history":[{"count":8,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/6000\/revisions"}],"predecessor-version":[{"id":6008,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/6000\/revisions\/6008"}],"wp:attachment":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}