安装 WSL2

WSL2 在 Windows 上提供 Linux 环境,利用 Hyper-V 和完整的 Linux 内核。由于 WSL2 使用真正的 Linux 内核,而不是原始 WSL 中的系统调用转换,因此支持 Singularity 等容器平台。

按照 WSL2 安装说明在默认的 Ubuntu 20.04 环境中启用 WSL2。在 Windows 11 和 Windows 10 的最新版本上,这就像打开管理员命令提示符或 Powershell 窗口并输入以下内容一样简单:

1
2
3
4
5
wsl --install
wsl --update
# 低版本下需要手动开启
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

按照提示操作。需要重新启动,下载 适用于 x64 计算机的 WSL2 Linux 内核更新包(这一步是将wsl1 更新到 wsl2)

1
2
# 运行上一步中下载的更新包。 (双击以运行 - 系统将提示你提供提升的权限,选择“是”以批准此安装。)
wsl --set-default-version 2

安装ubuntu

20.04(D盘)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mkdir D:\Linux
cd D:\Linux
# 下载ubuntu20.04 文件有4G多,等一会是正常的 等他下载完即可
Invoke-WebRequest -Uri https://wsldownload.azureedge.net/Ubuntu_2004.2020.424.0_x64.appx -OutFile Ubuntu20.04.appx -UseBasicParsing

Rename-Item .\Ubuntu20.04.appx Ubuntu.zip
Expand-Archive .\Ubuntu.zip -Verbose
cd .\Ubuntu\

# 安装
.\ubuntu2004.exe
Installing, this may take a few minutes...

# 看看自己安装的版本
.\ubuntu2004.exe config --default-user root
wsl -l -v
# wsl后查看linux发行版本信息
lsb_release -a

22.04 LTS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 下载 Ubuntu-22.04 LTS 镜像文件 下载的文件是 wsl.rootfs.tar.gz
# 具体版本视系统架构而定 , 例如英特尔及AMD处理器就下载AMD64版
ubuntu-22.04-server-cloudimg-amd64-wsl.rootfs.tar.gz
https://cloud-images.ubuntu.com/releases/
# 将下载的文件放入创建好的目录 D:\Linux\ubuntu_v2204
# 安装ubuntu 22.04
wsl --import Ubuntu-22.04 D:\Linux\ubuntu_v2204 D:\ubuntu-22.04-server-cloudimg-amd64-wsl.rootfs.tar.gz
# 启动 ubuntu
wsl -d Ubuntu-22.04 -u root
# 为 root 设置密码
passwd root
# 添加用户
adduser username # 添加普通用户 username 是要添加的用户名
usermod -a -G sudo username # 添加用户 username 到 sudo 组,使其可以执行 sudo 指令
su username # 切换到用户 username
passwd # 设置用户密码
# 以普通用户启动 ubuntu
wsl -d Ubuntu-22.04 -u username

当您第一次打开“Ubuntu”应用程序时,系统会要求您为 Linux 环境设置用户名和密码。

换源

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
# 换成清华源 另外请注意,wsl的Ubuntu证书是过期的,如果你想手动还源的话请记得先更新证书
# 直接在Linux里执行,即可换成清华源
# wget https://gitee.com/lin-xi-269/tools/raw/master/os/QHubuntu20.04 && bash QHubuntu20.04
#阿里源
$ sudo vim /etc/apt/sources.list

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse


## 2204 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

# 版本更新
sudo apt-get update
sudo apt-get upgrade

sudo apt install vim wget curl git -y

将分发设置为默认值

1
2
3
4
5
wsl -l -v
# <DistributionName> config --default-user root
Ubuntu config --default-user root
# 将分发设置为默认值
wslconfig /setdefault Ubuntu

打开终端机,输入下指令更新 Ubuntu

1
2
3
sudo apt update
sudo apt upgrade
sudo apt full-upgrade

升级GLIBC高版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
strings /lib/x86_64-linux-gnu/libc.so.6 |grep GLIBC_
# 最高只到2.31

# 解决方案:添加一个高级版本系统的源,直接升级libc6.
# 编辑源
sudo vi /etc/apt/sources.list
# 添加高版本的源
deb http://mirrors.aliyun.com/ubuntu/ jammy main #添加该行到文件
# 运行升级
sudo apt update
sudo apt install libc6

# 查看结果
strings /lib/x86_64-linux-gnu/libc.so.6 |grep GLIBC_

终端配置

WSL(Ubuntu) 默认终端是 bash, zsh(oh-my-zsh) 功能比 bash 要强大得多,但是配置起来太难了。国外的一个程序员看不下去一直这么单调的 zsh,就开发了 oh my zsh。配置起来,不要太简单。可谓是高档大气上档次,狂拽炫酷吊炸天。

配置为zsh后 ,不要随便卸载,否则登录还是使用/bin/zsh会导致登录不上

卸载zsh

1
2
3
4
5
# 先 cat /etc/passwd 复制到本地备份一下
root:x:0:0:root:/root:/bin/zsh
# 然后 echo "<你修改后的内容>" > /etc/passwd
root:x:0:0:root:/root:/bin/bash
# 然后重启sshd service sshd restart 就完事了

安装 zsh

  • 检查环境

    1
    2
    3
    4
    5
    6
    # 查看当前系统的 shell
    echo $SHELL
    # 查看系统安装的 shell
    cat /etc/shells
    # 安装 zsh 若有 zsh,则切换未 zsh
    chsh -s/bin/zsh
  • WSL(Ubuntu) 默认是未安装 zsh 的,使用以下指令安装并切换:

    1
    2
    3
    sudo apt-get install zsh -y
    chsh -s /bin/zsh
    exec /bin/zsh

安装 oh my zsh

使用以下指令下载并安装 oh my zsh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sudo apt install zsh -y
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

# 国内gitee 安装 oh my zsh
sh -c "$(curl -fsSL https://gitee.com/pocmon/ohmyzsh/raw/master/tools/install.sh)"


# bash 切换到 zsh 环境变量丢失问题
# 在~/.zshrc 开头添加一下配置
export PATH=$HOME/bin:/usr/local/bin:$PATH
source $HOME/.bashrc
source $HOME/.bash_profile


# 安装字体 https://github.com/romkatv/powerlevel10k#manual-font-installation


# 更新配置
source ~/.zshrc

❌ 注意:若有人在这里遇到错误:curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused ,那是因为 github 被墙了。解决方法如下(亲测有效): 输入网址(先确能进 github):https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh 进入后是一个文件,将该 .zsh 文件下载下来,保存为 install.zsh到一个目录。接下来在 WSL 进入该目录,执行命令:

1
2
chmod +x install.sh
./install.sh

切换主题

常用命令:

1
2
3
4
5
# 查看 oh my zsh 自带了哪些主题:
ls ~/.oh-my-zsh/themes

# 查看当前主题:robbyrussell
echo $ZSH_THEME

输入以下指令,进入 .zshrc 配置文件。 注意:ZSH_THEME=与主题名之间没有空格!!!

1
2
3
4
5
6
7
8
9
# 进入 `.zshrc` 后修改 `ZSH_THEME` 为指定主题
# 推荐一些个人觉得好看的主题:robbyrussell,agnoster,muse,af-magic",rkj-repos
sed -i "s/ZSH_THEME=\".*/ZSH_THEME=\"muse\"/g" $HOME/.zshrc
# 设置为随机主题:
ZSH_THEME=random
# 设置为在指定主题间切换:
ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
# 更新一下缓存
source ~/.zshrc

外部主题

在其仓库wiki中有一些额外的主题, 推荐笔者常用的spaceship主题,安装方法如下:

1
2
3
4
5
6
7
8
9
# 拉取项目仓库,可以通过上面提到的方法进行加速,此处直接放镜像地址,如果因为时间问题镜像失效,请使用其github官方地址
git clone https://gitee.com/xiaoqqya/spaceship-prompt.git "$ZSH_CUSTOM/themes/spaceship-prompt" --depth=1

# 添加软链接
ln -s "$ZSH_CUSTOM/themes/spaceship-prompt/spaceship.zsh-theme" "$ZSH_CUSTOM/themes/spaceship.zsh-theme"

# 更改主题,按照上面提到的方法在.zshrc配置中修改ZSH_THEME变量为 spaceship,然后重启终端生效
sed -i "s/ZSH_THEME=\".*/ZSH_THEME=\"spaceship\"/g" $HOME/.zshrc
source ~/.zshrc

安装 P10k

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装 P10k
# Gitee 镜像
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
# github镜像
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

sed -i 's/ZSH_THEME.*/ZSH_THEME="powerlevel10k\/powerlevel10k"/g' .zshrc
# ZSH_THEME="powerlevel10k/powerlevel10k"
source ~/.zshrc
# 设置 P10k
p10k configure

## 配置参考 New config: ~/.p10k.zsh.
https://gitee.com/luoxian1011/files/raw/master/.p10k.zsh

安装插件

插件与主题一样,同样是在.zshrc文件中进行配置。ohmyzsh默认自带了很多插件放置在~/.oh-my-zsh/plugins目录下面,具体每个插件的作用可以参考其仓库wiki:

1
2
# 查看可用插件
ls ~/.oh-my-zsh/plugins/

默认只启用了git插件,打开~/.zshrc配置文件,找到plugins变量,将您想启用的插件加入进入即可,不通插件名称之间以空格隔开,保存后重开终端或者执行exec $SHELL命令即可生效。除ohmyzsh自带的插件外,您还可以安装一些额外的插件,在其仓库wiki中介绍了一些,可以自行去查看,此处推荐笔者经常使用的插件:

  • zsh-autosuggestions:能在输入命令时根据历史记录提供自动建议的插件
  • zsh-syntax-highlighting 当你输入命令的时候,提供语法高亮的插件
  • conda-zsh-completion 提供conda虚拟环境名称补全的插件
  • pippython包管理器pip提供补全
  • ufw 为防火墙管理工具ufw命令提供补全
  • sudo 通过双击Esc键快速为当前命令添加sudo前缀
  • dockerdocker命令添加自动补全支持
  • docker-composedocker-compose命令添加自动补全支持
  • extract 一条extract命令解压多种压缩包格式,使用方法:extract <filename>
  • command-not-found 此插件在zsh找不到命令时提供建议的安装包
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
# 启用插件
# 拉取项目仓库,可以通过上面提到的方法进行加速,此处直接放镜像地址,如果因为时间问题镜像失效,请使用其github官方地址
git clone https://gitee.com/xiaoqqya/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://gitee.com/xiaoqqya/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
git clone https://gitee.com/xiaoqqya/conda-zsh-completion.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/conda-zsh-completion

# 设置zshrc
cat <<EOF >> $HOME/.zshrc
# 关闭 git 显示
SPACESHIP_GIT_SHOW=true
# 关闭 node 显示
SPACESHIP_NODE_SHOW=true
# 关闭 maven 显示
SPACESHIP_MAVEN_SHOW=true
# 关闭 package 显示
SPACESHIP_PACKAGE_SHOW=true

zstyle ':completion:*:*:docker:*' option-stacking yes
zstyle ':completion:*:*:docker-*:*' option-stacking yes
EOF

# 启用插件
sed -i "s/plugins=(git)/plugins=(git sudo zsh-autosuggestions zsh-syntax-highlighting conda-zsh-completion pip ufw docker docker-compose extract command-not-found)/g" $HOME/.zshrc

exec zsh -l

自动安装脚本安装

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
### 为了便于安装,笔者将安装 oh-my-zsh 以及常用主题和插件的过程写成了脚本,内容如下: autozsh.bash

#!/bin/bash
if ! which git > /dev/null 2>&1; then
echo -e "\ngit is not found.\nexit with code 1.\n"
exit 1
elif ! which curl > /dev/null 2>&1; then
echo -e "\ncurl is not found.\nexit with code 1.\n"
exit 1
elif ! which zsh > /dev/null 2>&1; then
echo -e "\nzsh is not found.\nexit with code 1.\n"
exit 1
fi

curl -k -sSL https://gitee.com/xiaoqqya/ohmyzsh/raw/master/tools/install.sh | sed "s/\$REMOTE/https:\/\/gitee.com\/xiaoqqya\/ohmyzsh.git/g" | sed "/.*exec zsh.*/d" > $HOME/.temp

cat <<EOF >> $HOME/.temp

git clone https://gitee.com/xiaoqqya/spaceship-prompt.git "\$ZSH/custom/themes/spaceship-prompt" --depth=1
ln -s "\$ZSH/custom/themes/spaceship-prompt/spaceship.zsh-theme" "\$ZSH/custom/themes/spaceship.zsh-theme"
echo -e "\n"

git clone https://gitee.com/xiaoqqya/zsh-autosuggestions.git \${ZSH:-~/.oh-my-zsh}/custom/plugins/zsh-autosuggestions
echo -e "\n"

git clone https://gitee.com/xiaoqqya/zsh-syntax-highlighting.git \${ZSH:-~/.oh-my-zsh}/custom/plugins/zsh-syntax-highlighting
echo -e "\n"

git clone https://gitee.com/xiaoqqya/conda-zsh-completion.git \${ZSH:-~/.oh-my-zsh}/custom/plugins/conda-zsh-completion
EOF

sh $HOME/.temp
rm -rf $HOME/.temp

sed -i "s/ZSH_THEME=\".*/ZSH_THEME=\"spaceship\"/g" $HOME/.zshrc
sed -i "s/plugins=(git)/plugins=(git sudo zsh-autosuggestions zsh-syntax-highlighting conda-zsh-completion pip ufw docker docker-compose extract command-not-found)/g" $HOME/.zshrc

cat <<EOF >> $HOME/.zshrc

# 关闭 git 显示
SPACESHIP_GIT_SHOW=true
# 关闭 node 显示
SPACESHIP_NODE_SHOW=true
# 关闭 maven 显示
SPACESHIP_MAVEN_SHOW=true
# 关闭 package 显示
SPACESHIP_PACKAGE_SHOW=true

zstyle ':completion:*:*:docker:*' option-stacking yes
zstyle ':completion:*:*:docker-*:*' option-stacking yes
export ZSH_COMPDUMP=$ZSH/cache/.zcompdump-$HOST
compinit -d ~/.cache/zsh/zcompdump-$ZSH_VERSION
EOF

exec zsh -l

nvm设置

1
2
3
4
5
6
7
8
# 修改 环境变量
sudo vi ~/.bashrc
# 在最后添加淘宝镜像
cat <<EOF >> $HOME/.zshrc
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/
EOF

source $HOME/.zshrc

Windows Termian

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
{
"$help": "https://aka.ms/terminal-documentation",
"$schema": "https://aka.ms/terminal-profiles-schema",
"actions": [
{
"command": {
"action": "copy",
"singleLine": false
},
"keys": "ctrl+c"
},
{
"command": "paste",
"keys": "ctrl+v"
},
{
"command": {
"action": "splitPane",
"split": "auto",
"splitMode": "duplicate"
},
"keys": "alt+shift+d"
},
{
"command": "find",
"keys": "ctrl+shift+f"
}
],
"copyFormatting": "none",
"copyOnSelect": false,
"defaultProfile": "{ab3373b3-0437-4f68-a6ed-e8bc54754699}",
"newTabMenu": [
{
"type": "remainingProfiles"
}
],
"profiles": {
"defaults": {
"closeOnExit": "graceful",
"font": {
"face": "MesloLGS NF",
"size": 9.0
},
"historySize": 9001,
"opacity": 85,
"snapOnInput": true,
"useAcrylic": true
},
"list": [
{
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"hidden": false,
"name": "Windows PowerShell"
},
{
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"hidden": true,
"name": "Command Prompt"
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
},
{
"colorScheme": "Campbell",
"commandline": "C:\\WINDOWS\\system32\\wsl.exe -d Ubuntu22.04.3",
"guid": "{ab3373b3-0437-4f68-a6ed-e8bc54754699}",
"hidden": false,
"icon": "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png",
"name": "Ubuntu22.04.3",
"startingDirectory": "~"
}
]
},
"schemes": [
{
"background": "#0C0C0C",
"black": "#0C0C0C",
"blue": "#0037DA",
"brightBlack": "#767676",
"brightBlue": "#3B78FF",
"brightCyan": "#61D6D6",
"brightGreen": "#16C60C",
"brightPurple": "#B4009E",
"brightRed": "#E74856",
"brightWhite": "#F2F2F2",
"brightYellow": "#F9F1A5",
"cursorColor": "#FFFFFF",
"cyan": "#3A96DD",
"foreground": "#CCCCCC",
"green": "#13A10E",
"name": "Campbell",
"purple": "#881798",
"red": "#C50F1F",
"selectionBackground": "#FFFFFF",
"white": "#CCCCCC",
"yellow": "#C19C00"
},
{
"background": "#012456",
"black": "#0C0C0C",
"blue": "#0037DA",
"brightBlack": "#767676",
"brightBlue": "#3B78FF",
"brightCyan": "#61D6D6",
"brightGreen": "#16C60C",
"brightPurple": "#B4009E",
"brightRed": "#E74856",
"brightWhite": "#F2F2F2",
"brightYellow": "#F9F1A5",
"cursorColor": "#FFFFFF",
"cyan": "#3A96DD",
"foreground": "#CCCCCC",
"green": "#13A10E",
"name": "Campbell Powershell",
"purple": "#881798",
"red": "#C50F1F",
"selectionBackground": "#FFFFFF",
"white": "#CCCCCC",
"yellow": "#C19C00"
},
{
"background": "#282C34",
"black": "#282C34",
"blue": "#61AFEF",
"brightBlack": "#5A6374",
"brightBlue": "#61AFEF",
"brightCyan": "#56B6C2",
"brightGreen": "#98C379",
"brightPurple": "#C678DD",
"brightRed": "#E06C75",
"brightWhite": "#DCDFE4",
"brightYellow": "#E5C07B",
"cursorColor": "#FFFFFF",
"cyan": "#56B6C2",
"foreground": "#DCDFE4",
"green": "#98C379",
"name": "One Half Dark",
"purple": "#C678DD",
"red": "#E06C75",
"selectionBackground": "#FFFFFF",
"white": "#DCDFE4",
"yellow": "#E5C07B"
},
{
"background": "#FAFAFA",
"black": "#383A42",
"blue": "#0184BC",
"brightBlack": "#4F525D",
"brightBlue": "#61AFEF",
"brightCyan": "#56B5C1",
"brightGreen": "#98C379",
"brightPurple": "#C577DD",
"brightRed": "#DF6C75",
"brightWhite": "#FFFFFF",
"brightYellow": "#E4C07A",
"cursorColor": "#4F525D",
"cyan": "#0997B3",
"foreground": "#383A42",
"green": "#50A14F",
"name": "One Half Light",
"purple": "#A626A4",
"red": "#E45649",
"selectionBackground": "#4F525D",
"white": "#FAFAFA",
"yellow": "#C18301"
},
{
"background": "#002B36",
"black": "#002B36",
"blue": "#268BD2",
"brightBlack": "#073642",
"brightBlue": "#839496",
"brightCyan": "#93A1A1",
"brightGreen": "#586E75",
"brightPurple": "#6C71C4",
"brightRed": "#CB4B16",
"brightWhite": "#FDF6E3",
"brightYellow": "#657B83",
"cursorColor": "#FFFFFF",
"cyan": "#2AA198",
"foreground": "#839496",
"green": "#859900",
"name": "Solarized Dark",
"purple": "#D33682",
"red": "#DC322F",
"selectionBackground": "#FFFFFF",
"white": "#EEE8D5",
"yellow": "#B58900"
},
{
"background": "#FDF6E3",
"black": "#002B36",
"blue": "#268BD2",
"brightBlack": "#073642",
"brightBlue": "#839496",
"brightCyan": "#93A1A1",
"brightGreen": "#586E75",
"brightPurple": "#6C71C4",
"brightRed": "#CB4B16",
"brightWhite": "#FDF6E3",
"brightYellow": "#657B83",
"cursorColor": "#002B36",
"cyan": "#2AA198",
"foreground": "#657B83",
"green": "#859900",
"name": "Solarized Light",
"purple": "#D33682",
"red": "#DC322F",
"selectionBackground": "#073642",
"white": "#EEE8D5",
"yellow": "#B58900"
},
{
"background": "#000000",
"black": "#000000",
"blue": "#3465A4",
"brightBlack": "#555753",
"brightBlue": "#729FCF",
"brightCyan": "#34E2E2",
"brightGreen": "#8AE234",
"brightPurple": "#AD7FA8",
"brightRed": "#EF2929",
"brightWhite": "#EEEEEC",
"brightYellow": "#FCE94F",
"cursorColor": "#FFFFFF",
"cyan": "#06989A",
"foreground": "#D3D7CF",
"green": "#4E9A06",
"name": "Tango Dark",
"purple": "#75507B",
"red": "#CC0000",
"selectionBackground": "#FFFFFF",
"white": "#D3D7CF",
"yellow": "#C4A000"
},
{
"background": "#FFFFFF",
"black": "#000000",
"blue": "#3465A4",
"brightBlack": "#555753",
"brightBlue": "#729FCF",
"brightCyan": "#34E2E2",
"brightGreen": "#8AE234",
"brightPurple": "#AD7FA8",
"brightRed": "#EF2929",
"brightWhite": "#EEEEEC",
"brightYellow": "#FCE94F",
"cursorColor": "#000000",
"cyan": "#06989A",
"foreground": "#555753",
"green": "#4E9A06",
"name": "Tango Light",
"purple": "#75507B",
"red": "#CC0000",
"selectionBackground": "#555753",
"white": "#D3D7CF",
"yellow": "#C4A000"
},
{
"background": "#300A24",
"black": "#171421",
"blue": "#0037DA",
"brightBlack": "#767676",
"brightBlue": "#08458F",
"brightCyan": "#2C9FB3",
"brightGreen": "#26A269",
"brightPurple": "#A347BA",
"brightRed": "#C01C28",
"brightWhite": "#F2F2F2",
"brightYellow": "#A2734C",
"cursorColor": "#FFFFFF",
"cyan": "#3A96DD",
"foreground": "#FFFFFF",
"green": "#26A269",
"name": "Ubuntu-ColorScheme",
"purple": "#881798",
"red": "#C21A23",
"selectionBackground": "#FFFFFF",
"white": "#CCCCCC",
"yellow": "#A2734C"
},
{
"background": "#000000",
"black": "#000000",
"blue": "#000080",
"brightBlack": "#808080",
"brightBlue": "#0000FF",
"brightCyan": "#00FFFF",
"brightGreen": "#00FF00",
"brightPurple": "#FF00FF",
"brightRed": "#FF0000",
"brightWhite": "#FFFFFF",
"brightYellow": "#FFFF00",
"cursorColor": "#FFFFFF",
"cyan": "#008080",
"foreground": "#C0C0C0",
"green": "#008000",
"name": "Vintage",
"purple": "#800080",
"red": "#800000",
"selectionBackground": "#FFFFFF",
"white": "#C0C0C0",
"yellow": "#808000"
}
],
"themes": []
}

Centos7下zsh

1
2
3
4
5
6
7
8
9
10
yum install zsh
git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"

# 创建文件
setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done

chsh -s /bin/zsh

WSL配置

设置网络、主机名、开启systemd

1
2
3
4
5
6
7
8
9
10
11
12
13
$ sudo vim /etc/wsl.conf
[interop]
appendWindowsPath = false
[network]
generateResolvConf = false
# 设置主机名
hostname=node1
# WSL2 开启Systemd
[boot]
systemd=true
# 设置默认用户
[user]
default=root

设置DNS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 临时设置
# $ sudo vim /etc/resolv.conf
# nameserver 114.114.114.114
echo "nameserver 114.114.114.114" > /etc/resolv.conf


# 永久设置
cat <<EOF >> $HOME/.zshrc
# Configure WSL Ubuntu-20.04 default DNS
sudo touch /etc/resolv.conf
sudo chmod 646 /etc/resolv.conf
sudo echo "nameserver 114.114.114.114" > /etc/resolv.conf
sudo chmod 644 /etc/resolv.conf
EOF

重启生效

1
2
# Windows PowerShell下运行 (关闭后重启) WSL
wsl --shutdown

开启新特性

更新到 WSL2 2.0, 配置 .wslconfig 文件开启新特性:

启用镜像网络后,WSL2 和 Windows 主机将使用相同的网络,并且可以通过 localhost 访问本机系统上的服务。这意味着你可以在 WSL2 中访问本机系统上运行的应用程序,以及通过本机系统访问在 WSL2 中运行的应用程序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# windows user/fulsun 目录下配置 .wslconfig 文件开启新特性
[experimental]
autoMemoryReclaim=gradual # 开启自动回收内存,可在 gradual, dropcache, disabled 之间选择
networkingMode=mirrored # 开启镜像网络
dnsTunneling=true # 开启 DNS Tunneling
firewall=true # 开启 Windows 防火墙
autoProxy=true # 开启自动同步代理
sparseVhd=true # 开启自动释放 WSL2 虚拟硬盘空间

[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=8GB
# Sets the VM to use two virtual processors
processors=8

重启 WSL

1
2
3
# 重启 WSL
wsl --shutdown
wsl -t Name

更新内核

检查 WSL 内核版本: 通过在 PowerShell 中运行以下命令,确保具有最新的内核:

1
2
3
$ wsl cat /proc/version

Linux version 5.15.153.1-microsoft-standard-WSL2 (root@941d701f84f1) (gcc (GCC) 11.2.0, GNU ld (GNU Binutils) 2.37) #1 SMP Fri Mar 29 23:14:13 UTC 2024

升级内核: https://github.com/microsoft/WSL2-Linux-Kernel,下载最新版本

1
2
3
4
5
6
7
8
9
10

git config --global http.postBuffer 5242880000
# 下载源码
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
git clone https://ghproxy.net/https://github.com/microsoft/WSL2-Linux-Kernel.git
# 查看git tags
git tag
# 检出对应的内核版本
git checkout tags/linux-msft-wsl-6.6.36.3
# 编译内核模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sudo apt install build-essential bc flex bison dwarves libssl-dev libelf-dev
# 然后执行以下命令来使用全部线程编译内核:CPU i7-12700F 拥有 12 核 20 线程,所以我可以使用 -j20 选项
# make KCONFIG_CONFIG=Microsoft/config-wsl -j$(nproc)
make KCONFIG_CONFIG=Microsoft/config-wsl -j20
# 途中的编译配置选择一路按回车即可.
# 耐心等待安装完成后,可以在标准输出内找到类似字段。 Kernel: 后的路径即为内核路径。
Kernel: arch/x86/boot/bzImage is ready (#2)
DESCEND objtool
DESCEND bpf/resolve_btfids
CALL scripts/checksyscalls.sh
DEPMOD /lib/modules/6.1.9-linux-standard-WSL2
INSTALL /boot

# 编译modules_install 完成后会自动创建软链接
make KCONFIG_CONFIG=Microsoft/config-wsl -j20 modules_install
# DEPMOD /lib/modules/6.6.36.3-microsoft-standard-WSL2
mkdir -p /lib/modules/$(uname -r)/build
ll /lib/modules/6.6.36.3-microsoft-standard-WSL2
lrwxrwxrwx 1 sun sun 51 Jul 23 11:36 build -> /home/sun/WSL2-Linux-Kernel-linux-msft-wsl-6.6.36.3/

将其复制到你想要的位置。

1
2
3
4
# 注意这个位置应该存在于你的主硬盘而不是 wsl 中
cp arch/x86/boot/bzImage /mnt/d/Linux/CustomKernel/linux-msft-wsl-6.6.36.3
# 然后将文件名改为 kernel
mv /mnt/d/Linux/CustomKernel/linux-msft-wsl-6.6.36.3/bzImage /mnt/d/Linux/CustomKernel/linux-msft-wsl-6.6.36.3/kernel

打开你的用户目录,修改 .wslconfig 文件,在 [wsl2] 标签中添加 kernel 字段,它的值为带有转义反斜杠的 Windows 路径。

1
2
3
4
5
6
7
8
修改 \Users\<UserName>\.wslconfig

[wsl2]
# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
kernel=D:\\Linux\\CustomKernel\\linux-msft-wsl-6.6.36.3\\kernel

# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
# kernelCommandLine = vsyscall=emulate

重启wsl查看内核

1
2
3
4
5
wsl --shutdown
wsl cat /proc/version

uname -a
Linux node1 6.6.36.3-microsoft-standard-WSL2 #1 SMP PREEMPT_DYNAMIC Mon Jul 22 17:47:43 CST 2024 x86_64 x86_64 x86_64 GNU/Linux

vim

1
2
3
4
5
6
7
8
9
10
11
apt install vim
if has('mouse')
set mouse-=a
endif

#复制 vim 配置模版
cp /usr/share/vim/vimrc ~/.vimrc
#开启语法高亮
echo 'syntax on' >> ~/.vimrc
#开启行号显示
echo 'set nu!' >> ~/.vimrc

wsl2备份和还原

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
wsl -l -v
Ubuntu (默认)
# 查看运行中的子系统
wsl -l --running
# 运行子系统
wsl --distribution Ubuntu
# 停止子系统
wsl --terminate Ubuntu 或 wsl --shutdown (停止当前子系统)
# 备份子系统(需要先停止才能备份)
wsl --export Ubuntu D:\Ubuntu-22.04.tar

# 卸载子系统
wsl --unregister Ubuntu

# 还原子系统
# wsl --import <Distro> <InstallLocation> <FileName> [Options]
wsl --import Ubuntu 'D:\Program Files\WSL' D:\Ubuntu-22.04.tar

镜像网络

版本要求:

1
2
3
4
5
6
7
8
9
10
11
# 设置 WSL2 为默认分发版本
wsl --set-default-version 2

# 更新子系统内核到较新的版本
wsl --update

# 安装一个你喜欢的 Linux 子系统发行版
# 安装完先启动下子系统,完成第一次初始化,防止某些冲突错误,比如:
# WslRegisterDistribution failed with error: 0x80070057
# wsl --list --online
wsl --install Ubuntu-22.04

配置~\.wslconfig文件,加入以下配置

1
2
3
4
# 此 Powershell/Posh/Pwsh 代码块用于创建或打开配置文件 ~\.wslconfig
if (-not (Test-Path -Path "~/.wslconfig")) { New-Item -ItemType File ~/.wslconfig }
pushd ~ ; notepad .wslconfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 推荐的一些配置项如下
[wsl2] # 核心配置
autoProxy=false # 是否强制 WSL2/WSLg 子系统使用 Windows 代理设置(请根据实际需要启用)
dnsTunneling=false # WSL2/WSLg DNS 代理隧道,以便由 Windows 代理转发 DNS 请求(请根据实际需要启用)
firewall=true # WSL2/WSLg 子系统的 Windows 防火墙集成,以便 Hyper-V 或者 WPF 能过滤子系统流量(请根据实际需要启用)
guiApplications=true # 启用 WSLg GUI 图形化程序支持
ipv6=true # 启用 IPv6 网络支持
localhostForwarding=true # 启用 localhost 网络转发支持
memory=4GB # 限制 WSL2/WSLg 子系统的最大内存占用
nestedVirtualization=true # 启用 WSL2/WSLg 子系统嵌套虚拟化功能支持
networkingMode=mirrored # 启用镜像网络特性支持
#pageReporting=true # 启用 WSL2/WSLg 子系统页面文件通报,以便 Windows 回收已分配但未使用的内存
processors=8 # 设置 WSL2/WSLg 子系统的逻辑 CPU 核心数为 8(最大肯定没法超过硬件的物理逻辑核心数)
vmIdleTimeout=-1 # WSL2 VM 实例空闲超时关闭时间,-1 为永不关闭,根据参数说明,目前似乎仅适用于 Win11+

[experimental] # 实验性功能(按照过往经验,若后续转正,则是配置在上面的 [wsl2] 选节)
autoMemoryReclaim=gradual # 启用空闲内存自动缓慢回收,其它选项:dropcache / disabled(立即/禁用)
hostAddressLoopback=true # 启用 WSL2/WSLg 子系统和 Windows 宿主之间的本地回环互通支持
sparseVhd=true # 启用 WSL2/WSLg 子系统虚拟硬盘空间自动回收
bestEffortDnsParsing=true # 和 dnsTunneling 配合使用,Windows 将从 DNS 请求中提取问题并尝试解决该问题,从而忽略未知记录(请根据实际需要启用)
useWindowsDnsCache=false # 和 dnsTunneling 配合使用,决定是否使用 Windows DNS 缓存池

如果遇到以下任意一个错误,请确保已 Windows 版本符合要求,并且已更新到 Windows Subsystem for Linux 2.0.0+

  • wsl: 此计算机上不支持嵌套虚拟化。
  • wsl: 不支持镜像网络模式,正在回退到 NAT 网络
  • wsl: Hyper-V 防火墙不受支持
  • wsl: DNS 隧道不受支持

另外子系统虚拟硬盘空间自动回收(sparseVhd=true),需要将 Linux 发行版所使用的虚拟硬盘(VHDX)转换为稀疏格式

1
2
# wsl --manage '<Distro>' --set-sparse true
wsl --manage 'Ubuntu-24.04' --set-sparse true

WSLg/WSL2 子系统内配置/etc/wsl.conf文件,加入以下配置,推荐一并配置 systemd 支持

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
# 此配置文件不能通过符号链接配置(软链接和硬链接都不行),只能通过真实副本拷贝
# https://learn.microsoft.com/zh-cn/windows/wsl/wsl-config

[automount]
enabled=true
mountFsTab=true
options="metadata,dmask=0022,fmask=0077,umask=0022"
root=/mnt/

[filesystem]
# umask确定了文件创建时的初始权限,(文件或目录权限为文件目录默认权限减去umask得到初始文件权限,文件初始默认权限为0666,目录为0777,若用户umask为0002,则新创建的文件或目录在没有指定的情况下默认权限分别为0664,0775)
umask=0022

[interop]
enabled=true
appendWindowsPath=false # 不添加 Windows 环境变量 Path,防止路径变量污染带来的干扰

# 其它网络配置
[network]
generateHosts=true
generateResolvConf=true


# boot command 暂不支持 nohup 后台启动
# command=nohup service cron start >/dev/null 2>&1 &
[boot]
# command=/root/.start.sh
systemd=true