什么是WSL

WSL(windows subsystem for linux)适用于Linux的Windows的子系统,可以在windows操作系统上使用linux环境。

wsl可以使用的功能:

  • 在 Microsoft Store 中选择你偏好的 GNU/Linux 分发版。
  • 运行 Bash shell 脚本和 GNU/Linux 命令行应用程序
  • 在 Windows 上调用 GNU/Linux 应用程序。

WSL1和WSL2比较

什么是WSL2

WSL 2 是适用于 Linux 的 Windows 子系统体系结构的一个新版本,它支持适用于 Linux 的 Windows 子系统在 Windows 上运行 ELF64 Linux 二进制文件。 它的主要目标是提高文件系统性能,以及添加完全的系统调用兼容性

这一新的体系结构改变了这些 Linux 二进制文件与Windows 和计算机硬件进行交互的方式,但仍然提供与 WSL 1(当前广泛可用的版本)中相同的用户体验。

单个 Linux 分发版可以在 WSL 1 或 WSL 2 体系结构中运行。 每个分发版可随时升级或降级,并且你可以并行运行 WSL 1 和 WSL 2 分发版。 WSL 2 使用全新的体系结构,该体系结构受益于运行真正的 Linux 内核。

WSL1与WSL2的比较

WSL第一代并不“完整”,WSL1 只是一个用于访问 Linux 环境的兼容层,并没有实现 Linux 内核的完整功能。遇到的问题如下:

  • 需要在 WSL 下单独安装一套 Docker 引擎,并进行额外配置
  • 在 WSL 下启动 VS Code 后,会出现目录中文件被占用的情况,导致无法重命名,必须要先退出 VS Code
  • 无法正常使用 Linux 下全部命令,比如netstat

所以说WSL2比WSL1的主要优势:

  • 提高文件系统性能
  • 支持完全的系统调用兼容性
  • WSL2 基于Hyper-V功能的子集提供了 真正的 Linux 内核

WSL2

启用适用于Linux的Windwos子系统

以管理员身份打开powershell,运行以下命令

1
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

这样就可以使用WSL第一代了

更新到WSL2

使用WSL2需要将windows10更新到较高的版本。

  • 启用虚拟机平台可选功能
1
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

重新启动 计算机,以完成 WSL 安装并更新到 WSL 2

  • 下载Linux内核更新包

适用于 x64 计算机的 WSL2 Linux 内核更新包

  • 将WSL2设置成默认版本
1
wsl --set-default-version 2
  • 安装所选的Linux分发版本

打开Microsoft Store,选择Linux发行版本,获取安装

为新的 Linux 分发版创建用户帐户和密码

若要查看可用 Linux 分发版的列表,请输入 wsl --list --online

WSL2删除linux子系统

1
2
3
wslconfig /l     # 列出所有子系统
# 从列表中选择要卸载的发行版(例如Ubuntu)并键入命令
wslconfig /u Ubuntu

wsl2支持从文件资源管理器访问 Linux 文件,路径为\\wsl$\

解决WSL2中Vmmem内存占用过大的问题

Windows的linux子系统最大占用可到本机器的80%,所以必须限制一下它的性能来达到优化目的。

按下Windows + R 键,输入 %UserProfile% 并运行进入用户文件夹

新建文件.wslconfig,然后将下列内容加入

1
2
3
4
[wsl2]
memory=4GB
swap=0
localhostForwarding=true

其中4GB为制作Vmmem进程使用内存的大小,然后关闭linux子系统然后在cmd运行 wsl --shutdown 后再次打开linux子系统即可生效

xshell连接WSL2

1
2
3
4
5
6
7
8
9
10
11
12
sudo apt-get remove openssh-server  # 卸载自带的 ssh
sudo apt-get install openssh-server # 再次安装
sudo vim /etc/ssh/sshd_config
# 修改如下内容
Port 1234 # 默认的是22,但是windows有自己的ssh服务用的也是22端口,与windows重复了
UsePrivilegeSeparation no
PasswordAuthentication yes # 密码验证
# 允许登陆的用户 AllowUsers name

sudo service ssh --full-restart # 重启ssh服务

# 在xshell上连接,主机为127.0.0.1,端口为1234

windows terminal

安装并添加到右键菜单

参考官方文档:https://docs.microsoft.com/zh-cn/windows/terminal/get-started

将terminal加入右键菜单:

Win+R运行regedit进入注册表目录HKEY_CLASSES_ROOT\Directory\Background\shell

配置美化

安装oh-my-posh

1
2
3
4
5
# 管理员身份运行“Windows PowerShell”
Install-Module posh-git
Install-Module oh-my-posh

set-ExecutionPolicy RemoteSigned

新建配置文件

C:\Users\你的用户名\Documents\WindowsPowerShell新建文件 Microsoft.PowerShell_profile.ps1,增加下列内容:

1
2
3
4
5
6
7
8
9
$l = Get-Location
if ($l.Path -eq "C:\WINDOWS\system32") {
Set-Location "~"
}

Import-Module posh-git
Import-Module oh-my-posh

Set-Theme Paradox

美化配置:

修改windowsterminal的settings.json文件

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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// This file was initially generated by Windows Terminal 1.4.3243.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
"$schema": "https://aka.ms/terminal-profiles-schema",

"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

// You can add more global application settings here.
// To learn more about global settings, visit https://aka.ms/terminal-global-settings

// If enabled, selections are automatically copied to your clipboard.
"copyOnSelect": false,

// If enabled, formatted data is also copied to your clipboard
"copyFormatting": false,

// A profile specifies a command to execute paired with information about how it should look and feel.
// Each one of them will appear in the 'New Tab' dropdown,
// and can be invoked from the commandline with `wt.exe -p xxx`
// To learn more about profiles, visit https://aka.ms/terminal-profile-settings
"profiles":
{
"defaults":
{
// Put settings here that you want to apply to all profiles.
"useAcrylic": true,
"background": "#1e1e1e",
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false,
"closeOnExit": true,
"acrylicOpacity": 0.85,
"fontFace": "Cascadia Code PL",
"fontSize": 11,
"cursorColor" : "#FFFFFF",
"cursorShape": "bar",
"padding": "0, 0, 0, 0",
"snapOnInput": true,
"startingDirectory": ".",
"historySize": 9001,
"colorScheme": "Builtin Tango Dark"

},
"list":
[
{
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false
},
{
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "Command Prompt",
"commandline": "cmd.exe",
"hidden": false
},
{
"guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
"hidden": false,
"name": "Ubuntu",
"source": "Windows.Terminal.Wsl"
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
},
{
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"hidden": false,
"cursorColor": "#ffffff",
"cursorShape": "filledBox",
"name": "pwsh",
"commandline": "C:/Program Files/PowerShell/7-preview/pwsh.exe -nologo",
"source": "Windows.Terminal.PowershellCore",
// 启动菜单一定要设置为 <.>,否则后面重要的一步将会无效!
"startingDirectory": ".",
// 字体
"fontFace": "Fira Code",
"fontSize": 11,
"historySize": 9001,
"padding": "5, 5, 20, 25",
"snapOnInput": true,
"useAcrylic": false,
// 颜色
"colorScheme": "Homebrew"
}

]
},

// Add custom color schemes to this array.
// To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
"schemes": [
{
"name": "Homebrew",
"black": "#000000",
"red": "#FC5275",
"green": "#00a600",
"yellow": "#999900",
"blue": "#6666e9",
"purple": "#b200b2",
"cyan": "#00a6b2",
"white": "#bfbfbf",
"brightBlack": "#666666",
"brightRed": "#e50000",
"brightGreen": "#00d900",
"brightYellow": "#e5e500",
"brightBlue": "#0000ff",
"brightPurple": "#e500e5",
"brightCyan": "#00e5e5",
"brightWhite": "#e5e5e5",
"background": "#283033",
"foreground": "#00ff00"
},
{
"name": "Afterglow",
"black": "#151515",
"red": "#ac4142",
"green": "#7e8e50",
"yellow": "#e5b567",
"blue": "#6c99bb",
"purple": "#9f4e85",
"cyan": "#7dd6cf",
"white": "#d0d0d0",
"brightBlack": "#505050",
"brightRed": "#ac4142",
"brightGreen": "#7e8e50",
"brightYellow": "#e5b567",
"brightBlue": "#6c99bb",
"brightPurple": "#9f4e85",
"brightCyan": "#7dd6cf",
"brightWhite": "#f5f5f5",
"background": "#212121",
"foreground": "#d0d0d0"
},

{
"name": "Chalk",
"black": "#7d8b8f",
"red": "#b23a52",
"green": "#789b6a",
"yellow": "#b9ac4a",
"blue": "#2a7fac",
"purple": "#bd4f5a",
"cyan": "#44a799",
"white": "#d2d8d9",
"brightBlack": "#888888",
"brightRed": "#f24840",
"brightGreen": "#80c470",
"brightYellow": "#ffeb62",
"brightBlue": "#4196ff",
"brightPurple": "#fc5275",
"brightCyan": "#53cdbd",
"brightWhite": "#d2d8d9",
"background": "#2b2d2e",
"foreground": "#d2d8d9"
},
{
"name": "AdventureTime",
"black": "#050404",
"red": "#bd0013",
"green": "#4ab118",
"yellow": "#e7741e",
"blue": "#0f4ac6",
"purple": "#665993",
"cyan": "#70a598",
"white": "#f8dcc0",
"brightBlack": "#4e7cbf",
"brightRed": "#fc5f5a",
"brightGreen": "#9eff6e",
"brightYellow": "#efc11a",
"brightBlue": "#1997c6",
"brightPurple": "#9b5953",
"brightCyan": "#c8faf4",
"brightWhite": "#f6f5fb",
"background": "#1f1d45",
"foreground": "#f8dcc0"
},
{
"name": "Breeze",
"black": "#31363b",
"red": "#ed1515",
"green": "#11d116",
"yellow": "#f67400",
"blue": "#1d99f3",
"purple": "#9b59b6",
"cyan": "#1abc9c",
"white": "#eff0f1",
"brightBlack": "#7f8c8d",
"brightRed": "#c0392b",
"brightGreen": "#1cdc9a",
"brightYellow": "#fdbc4b",
"brightBlue": "#3daee9",
"brightPurple": "#8e44ad",
"brightCyan": "#16a085",
"brightWhite": "#fcfcfc",
"background": "#31363b",
"foreground": "#eff0f1"
},

{
"name": "Builtin Solarized Dark",
"black": "#073642",
"red": "#dc322f",
"green": "#859900",
"yellow": "#b58900",
"blue": "#268bd2",
"purple": "#d33682",
"cyan": "#2aa198",
"white": "#eee8d5",
"brightBlack": "#002b36",
"brightRed": "#cb4b16",
"brightGreen": "#586e75",
"brightYellow": "#657b83",
"brightBlue": "#839496",
"brightPurple": "#6c71c4",
"brightCyan": "#93a1a1",
"brightWhite": "#fdf6e3",
"background": "#002b36",
"foreground": "#839496"
},
{
"name": "Builtin Tango Dark",
"black": "#000000",
"red": "#cc0000",
"green": "#4e9a06",
"yellow": "#c4a000",
"blue": "#3465a4",
"purple": "#75507b",
"cyan": "#06989a",
"white": "#d3d7cf",
"brightBlack": "#555753",
"brightRed": "#ef2929",
"brightGreen": "#8ae234",
"brightYellow": "#fce94f",
"brightBlue": "#729fcf",
"brightPurple": "#ad7fa8",
"brightCyan": "#34e2e2",
"brightWhite": "#eeeeec",
"background": "#000000",
"foreground": "#ffffff"
},
{
"name": "Flat UI Palette v1 Modified",
"background": "#1c2024",
"black": "#000000",
"blue": "#2980b9",
"foreground": "#ecf0f1",
"green": "#27ae60",
"red": "#e74c3c",
"white": "#ffffff",
"yellow": "#f1c40f",
"cyan": "#16a085",
"purple": "#8e44ad",
"brightBlack": "#52677c",
"brightBlue": "#3498db",
"brightCyan": "#1abc9c",
"brightGreen": "#2ecc71",
"brightPurple": "#9b59b6",
"brightRed": "#e67e22",
"brightWhite": "#ecf0f1",
"brightYellow": "#f1c40f"
},

{
"name": "Gruvbox Dark",
"black": "#1e1e1e",
"red": "#be0f17",
"green": "#868715",
"yellow": "#cc881a",
"blue": "#377375",
"purple": "#a04b73",
"cyan": "#578e57",
"white": "#978771",
"brightBlack": "#7f7061",
"brightRed": "#f73028",
"brightGreen": "#aab01e",
"brightYellow": "#f7b125",
"brightBlue": "#719586",
"brightPurple": "#c77089",
"brightCyan": "#7db669",
"brightWhite": "#e6d4a3",
"background": "#1e1e1e",
"foreground": "#e6d4a3"
}
],

// Add custom actions and keybindings to this array.
// To unbind a key combination from your defaults.json, set the command to "unbound".
// To learn more about actions and keybindings, visit https://aka.ms/terminal-keybindings
"actions":
[
// Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
// These two lines additionally bind them to Ctrl+C and Ctrl+V.
// To learn more about selection, visit https://aka.ms/terminal-selection
{ "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
{ "command": "paste", "keys": "ctrl+v" },

// Press Ctrl+Shift+F to open the search box
{ "command": "find", "keys": "ctrl+shift+f" },

// Press Alt+Shift+D to open a new pane.
// - "split": "auto" makes this pane open in the direction that provides the most surface area.
// - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
// To learn more about panes, visit https://aka.ms/terminal-panes
{ "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
]
}