发布于 

Windows Subsystem for Linux (WSL) 🪟

“适用于 Linux 的 Windows 子系统 (WSL) 可让开发人员直接在 Windows 上按原样运行 GNU/Linux 环境,且不会产生传统虚拟机或双启动设置的开销。”

参考资料

配置终端

  1. 安装 nerd-fonts

  2. 安装 Terminal + PowerShell

  3. 添加 Terminal-Icons

  4. 安装 Oh My Posh

  5. 安装 Scoop A command-line installer for Windows.

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装 OhMyPosh
winget install JanDeDobbeleer.OhMyPosh
# 写入配置
notepad $PROFILE
# oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\di4am0nd.omp.json" | Invoke-Expression
# Import-Module -Name Terminal-Icons

# 安装 scoop - Windows 包管理器
iwr -useb get.scoop.sh | iex
# 安装多线程下载
scoop install aria2
# 安装 neofetch
scoop install neofetch

powerShell 支持多个配置文件并按照优先级加载。下面按照优先级顺序列出 Windows 下 PowerShell 配置文件的路径

  • 所有用户、所有主机 $PSHOME\Profile.ps1
  • 所有用户,当前主机 $PSHOME\Microsoft.PowerShell_profile.ps1
  • 当前用户、所有主机 $Home\Documents\PowerShell\Profile.ps1
  • 当前用户,当前主机 $Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

安装 WSL

开启功能

安装 Linux

1
2
3
4
5
6
7
8
9
10
11
12
# 直接 Microsoft Store 中安装 或
wsl --install -d Ubuntu
# 运行报错,重启一下电脑
Installing, this may take a few minutes...
WslRegisterDistribution failed with error: 0x8004032d
Error: 0x8004032d (null)
Press any key to continue...
# 查看安装镜像列表
wsl --list --verbose
# 资源访问
# \\wsl.localhost\Ubuntu
# 即 C:\Users\hcjjj\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState

配置

1
2
3
4
5
6
7
8
9
10
11
# wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理
vim ~\.wslconfig
# ...
[experimental]
autoMemoryReclaim=gradual
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true
# ...
wsl --shutdown
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
# 修改镜像源 
sudo vim /etc/apt/sources.list
# https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
sudo apt update && sudo apt upgrade

# zsh
sudo apt install zsh curl -y
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
vim ~/.zshrc
# ...
plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
)
# ...
source ~/.zshrc

# 设置默认shell
chsh -s `which zsh`

# Vim 配置
# 插件管理:https://github.com/junegunn/vim-plug
nvim ~/.config/nvim/init.vim

# ssh 服务
sudo apt install openssh-server
sudo service ssh start
sudo apt install net-tools
ifconfig

迁移 WSL

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
# 导出当前发行版
hcjjj@laptop ~ wsl --shutdown
hcjjj@laptop ~ wsl -l -v
NAME STATE VERSION
* Ubuntu Stopped 2
docker-desktop Stopped 2
hcjjj@laptop ~ wsl --export Ubuntu D:\WSL\ubuntu24
正在导出,这可能需要几分钟时间。
操作成功完成。
# 注销并卸载当前发行版
hcjjj@laptop ~ wsl --unregister Ubuntu
正在注销。
操作成功完成。
# 导入到 D 盘的新位置
hcjjj@laptop ~ cd D:\WSL
hcjjj@laptop ~ ls
Directory: D:\WSL
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2024/11/12 21:28  Ubuntu
-a--- 2024/11/12 21:20 2056519680  ubuntu24
hcjjj@laptop ~ wsl --import ubuntu D:\WSL\Ubuntu .\ubuntu24
正在导入,这可能需要几分钟时间。
操作成功完成。
# 启动新的 WSL 实例
hcjjj@laptop ~ wsl -d ubuntu