在 WSL 2 下使用 Arch Linux 系统
WARNING以下内容可能过时,请注意文章最近更新时间.
前期准备
我正在使用 Windows 11, version 25H2,SKU 版本为 Windows 家庭中文版。
首先检查自己的 Windows 是否启用了虚拟化:“右击任务栏 —— 任务栏管理 —— 性能 —— CPU”,观察到 “虚拟化:已启用”。
大部分电脑默认已开启了虚拟化。如果没有启用,请在 UEFI/BIOS 设置中手动开启虚拟化。
在开始菜单中,依次点击 “设置 —— 系统 —— 可选功能 —— 更多 Windows 功能”,进入 “启用或关闭 Windows 功能”,开启 “适用于 Linux 的 Windows 子系统” 和 “虚拟机平台” 两个功能,确认后需要重启你的计算机。
打开 PowerShell,下载 WSL (适用于 Linux 的 Windows 子系统,即 Windows Subsystem for Linux):
> wsl --update请求的操作需要提升。正在下载: 适用于 Linux 的 Windows 子系统 2.6.3正在安装: 适用于 Linux 的 Windows 子系统 2.6.3已安装 适用于 Linux 的 Windows 子系统 2.6.3。正在检查更新。已安装最新版本的适用于 Linux 的 Windows 子系统。Terminal window 正在检查更新。已禁止(403)。错误代码: Wsl/UpdatePackage/0x80190193如果在检查更新时出现 “已禁止(403)”,请关闭网络代理功能。
现在我成功安装了适用于 Linux 的 Windows 子系统 2.6.3,接着检查 WSL、WSLg、Linux 内核的版本信息:
> wsl --versionWSL 版本: 2.6.3.0内核版本: 6.6.87.2-1WSLg 版本: 1.0.71MSRDC 版本: 1.2.6353Direct3D 版本: 1.611.1-81528511DXCore 版本: 10.0.26100.1-240331-1435.ge-releaseWindows: 10.0.26200.8037TIP你也可以从 Microsoft Store 下载 WSL,这允许您比以前更快地获得 WSL 更新。
查看支持在线安装的 Linux 发行版:
> wsl --list --online以下是可安装的有效分发的列表。使用“wsl.exe --install <Distro>”安装。
NAME FRIENDLY NAMEUbuntu UbuntuUbuntu-24.04 Ubuntu 24.04 LTSopenSUSE-Tumbleweed openSUSE TumbleweedopenSUSE-Leap-16.0 openSUSE Leap 16.0SUSE-Linux-Enterprise-15-SP7 SUSE Linux Enterprise 15 SP7SUSE-Linux-Enterprise-16.0 SUSE Linux Enterprise 16.0kali-linux Kali Linux RollingDebian Debian GNU/LinuxAlmaLinux-8 AlmaLinux OS 8AlmaLinux-9 AlmaLinux OS 9AlmaLinux-Kitten-10 AlmaLinux OS Kitten 10AlmaLinux-10 AlmaLinux OS 10archlinux Arch LinuxFedoraLinux-43 Fedora Linux 43FedoraLinux-42 Fedora Linux 42eLxr eLxr 12.12.0.0 GNU/LinuxUbuntu-20.04 Ubuntu 20.04 LTSUbuntu-22.04 Ubuntu 22.04 LTSOracleLinux_7_9 Oracle Linux 7.9OracleLinux_8_10 Oracle Linux 8.10OracleLinux_9_5 Oracle Linux 9.5openSUSE-Leap-15.6 openSUSE Leap 15.6SUSE-Linux-Enterprise-15-SP6 SUSE Linux Enterprise 15 SP6我即将安装 Arch Linux:
> wsl --install archlinux正在下载: Arch Linux正在安装: Arch Linux已成功安装分发。可以通过 “wsl.exe -d archlinux” 启动它正在启动 archlinux...Welcome to the Arch Linux WSL image!
This image is maintained at <https://gitlab.archlinux.org/archlinux/archlinux-wsl>.
Please, report bugs at <https://gitlab.archlinux.org/archlinux/archlinux-wsl/-/issues>.Note that WSL 1 is not supported.
For more information about this WSL image and its usage (including "tips and tricks" and troubleshooting steps), see the related Arch Wiki page at <https://wiki.archlinux.org/title/Install_Arch_Linux_on_WSL>.
While images are built regularly, it is strongly recommended running "pacman -Syu" right after the first launch due to the rolling release nature of Arch Linux.
Generating pacman keys...Done
Populating keyring...Done[root@shy-pc-windows /]#此时我便以 root 身份进入了 Arch Linux 环境。
TIPWSL 可安装并运行多个 Linux 系统。在 PowerShell 中查看已安装的 WSL:
Terminal window > wsl --listTerminal window 适用于 Linux 的 Windows 子系统分发:archlinux (默认值)若想再次启动 WSL,可以在 PowerShell 中打开 “新建标签页” 的下拉菜单,选择想启动的 Linux 发行版后点击启动,进入 Linux 环境的 home 目录
~。WSL 也可以在开始菜单中搜索找到并启动。也可以在 PowerShell 中键入Terminal window > wsl -d archlinux在 Windows 的当前目录下启动 Linux 环境。使用
exit关闭终端,即可退出 WSL。
Linux
仓库
先同步软件仓库
pacman -Syu安装必要软件:
pacman -S vim sudo which fastfetch btop默认情况下,pacman 只有 [core] 和 [extra] 两种仓库。我可以定义 32 位仓库 [multilib] 以及 Arch Linux 中文社区仓库 [archlinuxcn],修改 /etc/pacman.conf:
# The testing repositories are disabled by default. To enable, uncomment the# repo name header and Include lines. You can add preferred servers immediately# after the header, and they will be used before the default mirrors.
#[core-testing]#Include = /etc/pacman.d/mirrorlist
[core]Include = /etc/pacman.d/mirrorlist
#[extra-testing]#Include = /etc/pacman.d/mirrorlist
[extra]Include = /etc/pacman.d/mirrorlist
# 32 bit applications
#[multilib-testing]#Include = /etc/pacman.d/mirrorlist
[multilib]Include = /etc/pacman.d/mirrorlist
# An example of a custom package repository. See the pacman manpage for# tips on creating your own repositories.#[custom]#SigLevel = Optional TrustAll#Server = file:///home/custompkgs
[archlinuxcn]Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$archServer = https://mirrors.ustc.edu.cn/archlinuxcn/$arch配置好 /etc/pacman.conf 后,pacman 会从仓库指定的镜像源 (比如 [core]、[extra] 对应的 /etc/pacman.d/mirrorlist) 下载软件。
因为一些原因,我需要为仓库准备中国相应的镜像源,这可以通过 Arch Linux 官网提供的 Pacman Mirrorlist Generator 获取。
修改 /etc/pacman.d/mirrorlist,更换镜像源
vim /etc/pacman.d/mirrorlist# Server = https://fastly.mirror.pkgbuild.com/$repo/os/$arch# Server = https://geo.mirror.pkgbuild.com/$repo/os/$arch
## ChinaServer = http://mirrors.aliyun.com/archlinux/$repo/os/$archServer = https://mirrors.aliyun.com/archlinux/$repo/os/$archServer = http://mirrors.nju.edu.cn/archlinux/$repo/os/$archServer = https://mirrors.nju.edu.cn/archlinux/$repo/os/$archServer = http://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$archServer = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$archServer = http://mirrors.ustc.edu.cn/archlinux/$repo/os/$archServer = https://mirrors.ustc.edu.cn/archlinux/$repo/os/$archroot 用户
我给 root 用户配置默认编辑器为 vim:
vim ~/.bash_profileexport EDITOR='vim'非 root 用户
sudo
我为本机添加一位名为 nisemono、可通过 sudo 提权的用户:
useradd -m -G wheel -s /bin/bash nisemonopasswd nisemono谁能 sudo?sudo 之后他的权限如何?我需要管理他们。
我使用 vim 编辑器 (环境变量 EDITOR 尚未生效),通过 visudo 命令编辑 sudoers 文件,规定处于 wheel 用户组里的所有用户 (包括 nisemono) sudo 后的权限:
EDITOR=vim visudo#%wheel ALL=(ALL:ALL) ALL%wheel ALL=(ALL:ALL) ALL%用户组名 主机名=(目标用户名) 命令1, 命令2, !命令3在
用户组名组中,允许目标用户名在主机名上,使用sudo后可以执行命令1、命令2,禁止执行命令3。
WSL 默认启动用户
现在我希望每次打开 WSL,登录的是 nisemono 用户,而不是 root 用户,因此修改 /etc/wsl.conf:
[boot]systemd=true
[user]default=nisemono重启终端,登录 nisemono。
yay
为 nisemono 获取 [archlinuxcn] 仓库的签名后,安装 archlinuxcn/yay,它可以让我安装 AUR 中的软件:
sudo pacman -S archlinuxcn-keyringsudo pacman -S yay默认文本编辑器
让 vim 作为 nisemono 的默认文本编辑器:
## ~/.bashrc#
# If not running interactively, don't do anything[[ $- != *i* ]] && return
alias ls='ls --color=auto'alias grep='grep --color=auto'PS1='[\u@\h \W]\$ '
export EDITOR='vim'fish
为 nisemono 安装 fish:
sudo pacman -S fishwhich fishTIP
fish全称 Friendly Interactive SHell,有许多开箱即用的功能,如自动补全、语法高亮、命令简写等。相比bash的兼容性、zsh的高度可定制,fish更易上手。
启动 fish,执行 fish_config 命令后,可以非常方便地在本机 8000 端口自由设置 fish 的配色、prompt、函数、变量:
$ fish
Welcome to fish, the friendly interactive shellType help for instructions on how to use fishnisemono@shy-pc-windows ~> fish_config...~ $| 快捷键 | 功能 |
|---|---|
Alt + Left | 在目录历史中后退一步 ⭐ |
Alt + Right | 在目录历史中前进一步 |
命令 cdh | 列出目录历史,字母/数字选择 ⭐⭐ |
Tab | 路径补全 |
Right | 接受命令补全 |
Ctrl + R | 列出相关命令,方向键选择 ⭐ |
Ctrl + C | 清空当前输入的命令 |
Ctrl + A | 将光标移至命令开头 ⭐⭐ |
Ctrl + E | 将光标移至命令末尾 |
Alt + F/B | 按单词移动光标 ⭐⭐ |
Ctrl + W | 删除光标前的一个单词 ⭐⭐ |
Alt + E | 使用 EDITOR 编辑命令 |
Ctrl + Z | 撤销误删操作 |
Alt + / | 重做撤销的操作 |
Niri
CAUTION经实践,在 WSL 下使用 Niri 体验十分不佳:
- WSLg 主要支持运行 Linux GUI 应用,而不提供完整桌面环境支持;
- 当你的计算机屏幕分辨率达到或超过 2k 时,
niri-session无法弹出窗口。我尝试过手动降低屏幕分辨率,然后打开终端niri msg outputs获取信息,然后在~/.config/niri/config.kdl里修改配置,但最终窗口十分不稳定,非常容易闪退;- 弹出的窗口无法真正全屏。
因此下面有关
Niri的内容都是废弃的。
接下来我将安装 Wayland 窗口管理器 (Compositor) Niri,支持可滚动平铺 (scrollable-tiling) 窗口。
NOTE与 Sway 或 Hyprland 不同的地方在于,Niri 将窗口排列在一个无限延伸的水平桌面上,你可以向左或向右滚动(当然也可以实现更复杂的布局)。
安装桌面常用工具,比如 Niri 默认使用的应用启动器 fuzzel 和终端 alacritty。为了兼容不支持 Wayland 显示协议的图形程序,我还要安装 xdg-desktop-portal-gtk 和 xdg-desktop-portal-gnome。Arch Wiki 推荐了这些软件包:
pacman -S fuzzel mako waybar xdg-desktop-portal-gtk xdg-desktop-portal-gnome alacritty swaybg swayidle swaylock xwayland-satellite udiskie安装完软件后,我可以直接启动 Niri:
niri-session