WSL 使用基础

Innate_Joker Lv1

启用 WSL

其实如果用的是新版本 VSCode 而且电脑上没有安装 WSL 那么在每次启动 VSCode 时都会提示让你装 WSL 的。如果没有,那么请转到控制面板,启用 ‘适用于 Windows 的 Linux 子系统’ 。如果还未安装 WSL ,那么就会开始下载。

PS:本文用的是 Windows 11 24H2 家庭版 如果其他版本 Windows 系统有显著差异可以参考其他教程。

启用 WSL

安装分发

查看已安装列表

通过 wsl --list 命令可以查看已经安装的列表。比如我现在已经安装了,输出如下:

1
2
3
适用于 Linux 的 Windows 子系统分发:
docker-desktop (默认)
Ubuntu

查看可以安装的在线分发

接着通过 wsl --list --online 命令可以查看可以安装的在线分发列表。输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
以下是可安装的有效分发的列表。
使用 'wsl.exe --install <Distro>' 安装。

NAME FRIENDLY NAME
AlmaLinux-8 AlmaLinux OS 8
AlmaLinux-9 AlmaLinux OS 9
AlmaLinux-Kitten-10 AlmaLinux OS Kitten 10
AlmaLinux-10 AlmaLinux OS 10
Debian Debian GNU/Linux
FedoraLinux-42 Fedora Linux 42
SUSE-Linux-Enterprise-15-SP6 SUSE Linux Enterprise 15 SP6
SUSE-Linux-Enterprise-15-SP7 SUSE Linux Enterprise 15 SP7
Ubuntu Ubuntu
Ubuntu-24.04 Ubuntu 24.04 LTS
archlinux Arch Linux
kali-linux Kali Linux Rolling
openSUSE-Tumbleweed openSUSE Tumbleweed
openSUSE-Leap-15.6 openSUSE Leap 15.6
Ubuntu-20.04 Ubuntu 20.04 LTS
Ubuntu-22.04 Ubuntu 22.04 LTS
OracleLinux_7_9 Oracle Linux 7.9
OracleLinux_8_10 Oracle Linux 8.10
OracleLinux_9_5 Oracle Linux 9.5

安装分发

通过 wsl --install <分发名称> 就可以安装分发了。输出如下:

1
2
正在下载: Ubuntu
[==========================53.9% ]

启动分发

通过 wsl --distribution <分发名称> 就可以运行对应的分发名称啦!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Provisioning the new WSL instance Ubuntu
This might take a while...
Create a default Unix user account: <你想要的用户名>
New password:
Retype new password:
passwd: password updated successfully
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 24.04.3 LTS (GNU/Linux 5.15.167.4-microsoft-standard-WSL2 x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/pro

System information as of Sun Sep 14 12:28:32 CST 2025

System load: 0.0 Processes: 31
Usage of /: 0.1% of 1006.85GB Users logged in: 0
Memory usage: 5% IPv4 address for eth0: 172.20.216.108
Swap usage: 0%


This message is shown once a day. To disable it please create the
/home/<你的用户名>/.hushlogin file.
<你的用户名>>@localhost:/mnt/d/Project/Blog$

PS:在输入密码的时候,你会看不见密码字符,这是盲人输出,属于正常现象。

关闭指定分发

使用 wsl --terminate <分发名称>

WSL 命令

来自 wsl --help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
版权所有 (c) Microsoft Corporation。保留所有权利。
有关此产品的隐私信息,请访问 https://aka.ms/privacy。

用法: wsl.exe [Argument][Options...][CommandLine]

运行 Linux 二进制文件的参数:

如果未提供命令行,wsl.exe 将启动默认 shell。

--exec, -e <CommandLine>
在不使用默认 Linux shell 的情况下执行指定的命令。

--shell-type <standard|login|none>
使用提供的 shell 类型执行指定的命令。

--
按原样传递剩余的命令行。

选项:
--cd <Directory>
将指定目录设置为当前工作目录。
如果使用 ~,则将使用 Linux 用户的主路径。如果路径以
/ 字符开始,它将被解释为绝对 Linux 路径。
否则,该值必须是绝对 Windows 路径。

--distribution, -d <DistroName>
运行指定的分发版。

--distribution-id <DistroGuid>
运行指定的分发版 ID。

--user, -u <UserName>
以指定用户身份运行。

--system
为系统分发版启动 shell。

用于管理适用于 Linux 的 Windows 子系统的参数:

--help
显示使用情况信息。

--debug-shell
出于诊断目的打开 WSL2 调试 shell。

--install [Distro] [Options...]
安装适用于 Linux 的 Windows 子系统分发版。
有关有效分发版的列表,请使用 'wsl.exe --list --online'。

选项:
--enable-wsl1
启用 WSL1 支持。

--from-file <Path>
从本地文件安装分发版。

--legacy
使用旧分发版清单。

--location <Location>
设置分发版的安装路径。

--name <Name>
设置分发的名称。

--no-distribution
仅安装所需的可选组件,不安装分发版。

--no-launch, -n
安装后不要启动分发版。

--version <Version>
指定要用于新分发的版本。

--web-download
从 Internet 而不是 Microsoft Store 下载分发版。

--manage <Distro> <Options...>
更改发行版特定选项。

选项:
--move <Location>
将分发移到新位置。

--set-sparse, -s <true|false>
将发行版的 vhdx 设置为稀疏,从而允许自动回收磁盘空间。

--set-default-user <Username>
设置分发版的默认用户。

--mount <Disk>
在所有 WSL 2 分发版中附加和装载物理磁盘或虚拟磁盘。

选项:
--vhd
指定 <Disk> 引用虚拟硬盘。

--bare
将磁盘附加到 WSL2,但不要装载它。

--name <Name>
使用装入点的自定义名称装载磁盘。

--type <Type>
装载磁盘时要使用的文件系统(如果未指定)默认为 ext4。

--options <Options>
其他装载选项。

--partition <Index>
要装载的分区的索引(如果未指定)默认为整个磁盘。

--set-default-version <Version>
更改新分发版的默认安装版本。

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

--status
显示适用于 Linux 的 Windows 子系统状态。

--unmount [磁盘]
从所有 WSL2 分发版中卸载和分离磁盘。
如果在没有参数的情况下调用,则卸载和分离所有磁盘。

--uninstall
从此计算机卸载适用于 Linux 的 Windows 子系统包。

--update
更新适用于 Linux 的 Windows 子系统包。

选项:
--pre-release
下载预发行版本(如果可用)。

--version, -v
显示版本信息。

用于在适用于 Linux 的 Windows 子系统中管理分发版的参数:

--export <Distro> <FileName> [选项]
将分发版导出到 tar 文件。
文件名可以是 - for stdout。

选项:
--format <Format>
指定导出格式。支持的值: tar、tar.gz、vhd。

--import <Distro> <InstallLocation> <FileName> [选项]
将指定的 tar 文件作为新分发版导入。
文件名可以是 - for stdin。

选项:
--version <Version>
指定要用于新分发的版本。

--vhd
指定所提供的文件是 .vhdx 文件,而不是 tar 文件。
此操作在指定的安装位置创建 .vhdx 文件的副本。

--import-in-place <Distro> <FileName>
将指定的 .vhdx 文件作为新分发版导入。
必须使用 ext4 文件系统类型设置此虚拟硬盘的格式。

--list, -l [选项]
列出分发版。

选项:
--all
列出所有分发版,包括当前
正在安装或卸载的分发版。

--running
仅列出当前正在运行的分发版。

--quiet, -q
仅显示分发版名称。

--verbose, -v
显示有关所有分发版的详细信息。

--online, -o
显示适合通过 'wsl --install' 安装的可用分发版列表。

--set-default, -s <Distro>
将分布版设置为默认值。

--set-version <Distro> <Version>
更改指定分发版的版本。

--terminate, -t <Distro>
终止指定的分发版。

--unregister <Distro>
取消注册分发版并删除根文件系统。

完结撒花

因为这篇几乎都是命令行操作,所以几乎没有图片。而且展示大多为命令行输出,所以也没有语法高亮,不要在意啦。

  • 标题: WSL 使用基础
  • 作者: Innate_Joker
  • 创建于 : 2025-09-14 12:00:34
  • 更新于 : 2025-09-14 12:38:18
  • 链接: https://innatejoker.github.io/2025/09/14/WSL 使用基础/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。