Windows Subsystem for Linux 配置记录

写在前面

Windows Subsystem for Linux(简称 WSL)顾名思义是在 Windows 上运行的 Linux 子系统。搭配 Windows Terminal,开发者可以获得直接在 Windows 上运行 GNU/Linux 环境的极佳体验。

需要注意的是,WSL 并不是一个 Linux 发行版,我们需要挑选一款我们中意的 Linux 发行版进行安装。本文将记录 0xfaner 配置 WSL 的 Debian 镜像的部分操作。

安装 WSL

首先在 Microsoft Store 中安装 WSL,然后列出可用的发行版。

1
2
# List available distributions
wsl --list --online

选用 Debian 并安装。

1
2
# Install debian
wsl --install -d Debian

如果使用 PowerShell 7 安装,遇到报错,可以尝试使用 PowerShell 5 重试命令。

包管理换源

进入 WSL 后使用清华大学开源软件镜像站加速 APT,更新软件源配置文件 /etc/apt/sources.list

参考:Debian 镜像使用帮助

1
2
3
4
5
# Back up source list
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

# Edit source list
sudo vi /etc/apt/sources.list
HTTPS Source
1
2
3
4
5
6
7
8
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free

使更新生效

1
2
3
# update system
sudo apt update
sudo apt upgrade -y

如果遇到无法拉取 HTTPS 源的情况,请先使用 HTTP 源并安装需要的包。

安装 Zsh

1
2
3
4
5
# Install zsh
sudo apt install -y zsh

# Set zsh as default shell
chsh -s /bin/zsh

新开一个 Shell Session,此时会得到提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~). This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q) Quit and do nothing. The function will be run again next time.

(0) Exit, creating the file ~/.zshrc containing just a comment.
That will prevent this function being run again.

(1) Continue to the main menu.

(2) Populate your ~/.zshrc with the configuration recommended
by the system administrator and exit (you will need to edit
the file by hand, if so desired).

--- Type one of the keys in parentheses ---

选择 0 或 2 均可,安装 Oh My Zsh 后配置文件 .zshrc 会被覆盖。

安装 Oh My Zsh

1
2
3
4
5
6
7
8
sudo apt install -y curl git vim

# Install oh my zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# Install plugin
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

配置主题与插件

编辑配置文件 .zshrc

1
2
3
4
5
6
7
8
9
# Set up themes
ZSH_THEME="ys"

# Set up plugins
plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
)

配置代理和高亮

编辑配置文件 .zshrc

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
# Set proxy shell function
proxy() {
if [[ $@ == 'enable' ]]; then
# Get host ip
export HOST_IP=$(ip route | grep default | awk '{print $3}')
export ALL_PROXY="socks5://$HOST_IP:1080";
export all_proxy="socks5://$HOST_IP:1080";
export http_proxy="http://$HOST_IP:1080";
export https_proxy="http://$HOST_IP:1080";
elif [[ $@ == 'disable' ]]; then
unset ALL_PROXY;
unset all_proxy;
unset http_proxy;
unset https_proxy;
else
echo 'ALL_PROXY =' ${ALL_PROXY:-'none'};
echo 'all_proxy =' ${all_proxy:-'none'};
echo 'http_proxy =' ${http_proxy:-'none'};
echo 'https_proxy =' ${https_proxy:-'none'};
fi
}

# Enable proxy
proxy enable

# Change ls colors
export LS_COLORS='ow=34;4'

使配置生效

1
source ~/.zshrc