import{_ as e,c as a,a as i,o as n}from"./app-7lyk19d4.js";const l={};function t(d,s){return n(),a("div",null,[...s[0]||(s[0]=[i(`<p>这篇文章主要讲WSL2虚拟机核心操作哦～它基于轻量级Hyper-V运行，像贴心小精灵默默工作～还能用命令行精细控制！接下来讲启动/关闭、实例管理、资源配置、网络操作、备份迁移这五大操作，是不是很期待呢～♪</p><h2 id="启动-关闭-wsl2-虚拟机" tabindex="-1"><a class="header-anchor" href="#启动-关闭-wsl2-虚拟机"><span>启动/关闭 WSL2 虚拟机</span></a></h2><p>WSL2的虚拟机平时是由Windows自动管理的呢，不过我们也可以通过命令手动控制它的生命周期哦～</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-"><span class="line"><span># 启动指定的发行版</span></span>
<span class="line"><span>wsl -d Ubuntu-20.04</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 关闭指定的发行版</span></span>
<span class="line"><span>wsl --terminate Ubuntu-20.04</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果想要完全关闭WSL2的所有虚拟机，也是可以的哦～</p><p>使用这个命令后，所有的发行版都会被强制终止，内存、网络、文件句柄都会被全部释放呢～</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-"><span class="line"><span>wsl --shutdown</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><h2 id="管理正在运行的-wsl-实例" tabindex="-1"><a class="header-anchor" href="#管理正在运行的-wsl-实例"><span>管理正在运行的 WSL 实例</span></a></h2><p>想知道当前有哪些WSL实例在运行吗？很简单哦～使用下面这个命令就可以查看啦：</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-"><span class="line"><span>wsl -l -v</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><p>输出结果大概是这个样子的哦～</p><div class="language-text line-numbers-mode" data-highlighter="shiki" data-ext="text" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-text"><span class="line"><span>  NAME              STATE           VERSION</span></span>
<span class="line"><span>* Ubuntu            Running         2</span></span>
<span class="line"><span>  docker-desktop    Running         2</span></span>
<span class="line"><span>  Ubuntu-20.04      Stopped         2</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>看到那个带星号(*)的了吗？那就是当前的默认系统哦～如果想要设置新的默认发行版，可以使用这个命令：</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-"><span class="line"><span>wsl --set-default Ubuntu-20.04</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><p>嘿嘿～接下来讲讲怎么删除一个 WSL 实例哦～如果想和某个WSL实例说拜拜，就可以用下面这个命令哒～</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-"><span class="line"><span>wsl --unregister Ubuntu-20.04</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><p>注意，删除后无法恢复哦～请谨慎操作～</p><h2 id="配置-wsl2-资源" tabindex="-1"><a class="header-anchor" href="#配置-wsl2-资源"><span>配置 WSL2 资源</span></a></h2><p>WSL2的资源也是可以自定义配置的哦～我们可以通过编辑<code>%USERPROFILE%\\.wslconfig</code>文件来设置内存、CPU核心数等参数呢～</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-"><span class="line"><span>[wsl2]</span></span>
<span class="line"><span># 内存限制（默认：80% 物理内存）</span></span>
<span class="line"><span>memory=4GB</span></span>
<span class="line"><span></span></span>
<span class="line"><span># CPU 核心数限制（默认：全部核心）</span></span>
<span class="line"><span>processors=2</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 交换空间大小（默认：25% 内存）</span></span>
<span class="line"><span>swap=2GB</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 是否启用 localhost 端口转发（默认 true）</span></span>
<span class="line"><span>localhostForwarding=true</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要注意哦～修改完配置后，必须重启WSL2虚拟机才能让这些设置生效呢～</p><h2 id="网络相关配置" tabindex="-1"><a class="header-anchor" href="#网络相关配置"><span>网络相关配置</span></a></h2><p>WSL2使用的是虚拟NAT网络哦，它和Windows主机是相互隔离的，但又可以互相通信呢～是不是很神奇～</p><table><thead><tr><th>方向</th><th>访问方式</th><th>说明</th></tr></thead><tbody><tr><td>Windows -&gt; WSL2</td><td>localhost:port</td><td>自动端口转发（需要<code>localhostForwarding=true</code>）</td></tr><tr><td>WSL2 -&gt; Windows</td><td><code>host.docker.internal</code> 或 <code>$(cat /etc/resolv.conf | grep nameserver | awk &#39;{print $2}&#39;)</code></td><td>获取主机的IP地址</td></tr><tr><td>外部机器 -&gt; WSL2</td><td>默认不可达</td><td>需要手动端口转发或使用 Windows 防火墙规则</td></tr></tbody></table><p>想知道WSL2的IP地址吗？可以用这个命令查看哦～（不过要注意，每次启动WSL2后，IP地址可能会变呢～）</p><p>注意哦～这个命令需要进入到WSL2虚拟机内部执行才行呢～</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-"><span class="line"><span>hostname -I</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><p>如果需要访问WSL2中的服务，多亏了<code>localhostForwarding</code>的配置，我们直接访问<a href="http://localhost:port" target="_blank" rel="noopener noreferrer">localhost:port</a>就可以啦～超方便的～</p><p>如果需要手动设置端口转发，可以在Powershell中添加防火墙规则和端口转发哦～不过要记得以管理员方式打开Powershell呢～</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-"><span class="line"><span># 获取 WSL2 当前IP地址</span></span>
<span class="line"><span>$wslIp = (wsl hostname -I).Trim()</span></span>
<span class="line"><span></span></span>
<span class="line"><span>$wslIp</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 添加端口转发（以 8000 端口为样例）</span></span>
<span class="line"><span>netsh interface portproxy add v4tov4 listenport=8000 listenaddress=0.0.0.0 connectport=8000 connectaddress=$wslIp</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 开放 Windows 防火墙</span></span>
<span class="line"><span>New-NetFirewallRule -DisplayName &quot;WSL2 Port 8000&quot; -Direction Inbound -LocalPort 8000 -Protocol TCP -Action Allow</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 撤销端口转发</span></span>
<span class="line"><span>netsh interface portproxy delete v4tov4 listenport=8000 listenaddress=0.0.0.0</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 撤销开放的防火墙规则</span></span>
<span class="line"><span>Remove-NetFirewallRule -DisplayName &quot;WSL2 Port 8000&quot;</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="备份和迁移" tabindex="-1"><a class="header-anchor" href="#备份和迁移"><span>备份和迁移</span></a></h2><p>WSL2发行版也是可以备份和迁移的哦～我们可以把整个发行版导出成tar文件，这样就可以用于备份、迁移或者分享给其他小伙伴啦～</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-"><span class="line"><span># 导出指定发行版到 tar 文件</span></span>
<span class="line"><span>wsl --export &lt;发行版名称&gt; &lt;输出文件路径.tar&gt;</span></span>
<span class="line"><span>wsl --export Ubuntu-20.04 D:\\backups\\ubuntu-2004.tar</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 导入 tar 文件为新的发行版</span></span>
<span class="line"><span>wsl --import &lt;新发行版名称&gt; &lt;安装目录&gt; &lt;tar 文件路径&gt; [选项]</span></span>
<span class="line"><span>wsl --import Ubuntu-Backup C:\\wsl\\Ubuntu-Backup D:\\backups\\ubuntu-2004.tar --version 2</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要注意哦～导入完成后，默认用户会变成root呢～如果想要设置回原来的用户，可以使用这个命令：</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-"><span class="line"><span>ubuntu2004.exe config --default-user username</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><p>悄悄告诉大家一个小秘密～WSL2是可以同时存在多个同源发行版的哦～这样我们就可以在不同的环境中进行不同的实验啦～是不是很方便呢～</p>`,36)])])}const r=e(l,[["render",t]]),c=JSON.parse('{"path":"/article/operate-wsl2/","title":"WSL2 核心操作指南","lang":"zh-CN","frontmatter":{"title":"WSL2 核心操作指南","createTime":"2025/09/30 16:08:32","cover":"https://image.simengweb.com/elysia_cover_image/2MEiWEhvsx8OtKeJ.png","permalink":"/article/operate-wsl2/","description":"这篇文章主要讲WSL2虚拟机核心操作哦～它基于轻量级Hyper-V运行，像贴心小精灵默默工作～还能用命令行精细控制！接下来讲启动/关闭、实例管理、资源配置、网络操作、备份迁移这五大操作，是不是很期待呢～♪ 启动/关闭 WSL2 虚拟机 WSL2的虚拟机平时是由Windows自动管理的呢，不过我们也可以通过命令手动控制它的生命周期哦～ 如果想要完全关闭W...","head":[["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"WSL2 核心操作指南\\",\\"image\\":[\\"https://image.simengweb.com/elysia_cover_image/2MEiWEhvsx8OtKeJ.png\\"],\\"dateModified\\":\\"2025-10-01T07:21:10.000Z\\",\\"author\\":[]}"],["meta",{"property":"og:url","content":"https://notes.simengweb.com/article/operate-wsl2/"}],["meta",{"property":"og:site_name","content":"仲夏夜之梦"}],["meta",{"property":"og:title","content":"WSL2 核心操作指南"}],["meta",{"property":"og:description","content":"这篇文章主要讲WSL2虚拟机核心操作哦～它基于轻量级Hyper-V运行，像贴心小精灵默默工作～还能用命令行精细控制！接下来讲启动/关闭、实例管理、资源配置、网络操作、备份迁移这五大操作，是不是很期待呢～♪ 启动/关闭 WSL2 虚拟机 WSL2的虚拟机平时是由Windows自动管理的呢，不过我们也可以通过命令手动控制它的生命周期哦～ 如果想要完全关闭W..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:image","content":"https://image.simengweb.com/elysia_cover_image/2MEiWEhvsx8OtKeJ.png"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:updated_time","content":"2025-10-01T07:21:10.000Z"}],["meta",{"name":"twitter:card","content":"summary_large_image"}],["meta",{"name":"twitter:image:src","content":"https://image.simengweb.com/elysia_cover_image/2MEiWEhvsx8OtKeJ.png"}],["meta",{"name":"twitter:image:alt","content":"WSL2 核心操作指南"}],["meta",{"property":"article:modified_time","content":"2025-10-01T07:21:10.000Z"}]]},"readingTime":{"minutes":3.74,"words":1123},"git":{"createdTime":1759223056000,"updatedTime":1759303270000,"contributors":[{"name":"si-meng-spec","username":"si-meng-spec","email":"simeng@simengweb.com","commits":3,"avatar":"https://avatars.githubusercontent.com/si-meng-spec?v=4","url":"https://github.com/si-meng-spec"}]},"autoDesc":true,"filePathRelative":"blog/technology/Operate_WSL2.md","headers":[],"categoryList":[{"id":"126ac9","sort":10000,"name":"blog"},{"id":"750eb7","sort":10001,"name":"technology"}]}');export{r as comp,c as data};
