WSL2 安装 ArchLinux
wsl2:
wsl --install --no-distribution; wsl --set-default-version 2
推荐浏览:
安装 ArchLinux
基本安装
以 import 的方式安装:
wsl --import Arch "D:\wsl\arch" "D:\downloads\archlinux.wsl" --version 2
wsl -l -v
wsl --manage Arch --set-sparse true
wsl --setdefault Archwsl2 config
windows 下的 ~/.wslconfig 和 子系统下的 /etc/wsl.conf 可配置项部分不是完全相同的!!!
systemctl initializing 的解决方案:
- wslconfig 的
[wsl2]下配置kernelCommandLine = cgroup_no_v1=all
windows ~/.wslconfig:
# boot interop ... 只能位于 /etc/wsl.conf
[experimental]
autoMemoryReclaim=gradual
sparseVhd=true
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true
[wsl2]
kernelCommandLine=cgroup_no_v1=allwsl2 网卡不支持 mtu 大于 1500, 上面配置了
networkingMode = mirrored则需要保证主机网卡 mtu 不大于 1500. 所以如果使用 clash 的 tun 模式时, 需要修改 tun 的 mtu 为 1500
子系统下的 /etc/wsl.conf:
[boot]
systemd=true
[interop]
enabled=true
appendWindowsPath=false
[automount]
enabled=true
options="metadata"
mountFsTab=true
[time]
useWindowsTimezone=true启动
以下三种启动方式:
- Arch 正常安装的方式会自动配置 Windows Terminal, 如果是上面 import 的方式则需要手动添加
wsl来启动默认的发行版,wsl -u root指定启动用户
Windows Terminal 配置示例:
// .actions
// 快速打开 arch
{ "icon": "D:\\wsl\\arch\\shortcut.ico", "name": "New Tab - Arch", "command": { "action": "newTab", "profile": "Arch" }, "keys": "ctrl+shift+t" }
// 快速打开 powershell
{ "name": "New Tab - PowerShell", "command": {"action": "newTab", "profile": "Windows PowerShell"} }
// 取消默认的 ctrl+tab 行为
{"command": {"action": "sendInput","input": "\u001b[27;5;9~"},"keys": "ctrl+tab"}
// .profiles.list
{
"guid": "{743afa2f-9bb6-564e-ba0c-adddf7d73fd3}",
"hidden": false,
"icon": "D:\\wsl\\arch\\shortcut.ico",
"name": "Arch",
"source": "Microsoft.WSL"
}网络驱动器
官方文档: 在 Windows 中映射网络驱动器
再我的电脑上右击, 选择映射网络驱动器, 文件夹填: \\wsl.localhost\Arch.
完成后如果看不到则需要重启 explorer.exe
基本设置
时区: 使用
timedatectl查看时区和时间是否正确, 系统默认使用systemd-timesyncd来同步时间. 官网文档设置时区区域:
grep -v '^#' /etc/locale.gen # 确认只保留 en_US.UTF-8 UTF-8* locale-gen cat /etc/locale.conf # 确认包含 LANG=en_US.UTF-8禁用 systemd-networkd-wait-online:
systemctl disable --now systemd-networkd-wait-online.service
完成后重启: wsl -t Arch wsl
包管理器
设置镜像: pacman-mirrorlist 生成镜像, 如:
echo 'Server = https://mirrors.aliyun.com/archlinux/$repo/os/$arch' > /etc/pacman.d/mirrorlistpacman-key --init
pacman-key --populate
pacman-key --refresh-keys更新:
pacman -Syu基本包:
pacman -S man-db which wget git vim sudo
# vi
ln -s /usr/bin/vim /usr/bin/vi安装时忽略 debug: /etc/makepkg.conf 的 OPTIONS 数组增加 !debug
创建用户
groupadd sudo
visudo # 取消注释 `%wheel ALL=(ALL:ALL) NOPASSWD: ALL` 和 `%sudo ALL=(ALL:ALL) ALL`
useradd -m -G wheel,sudo wsl
passwd wsl修改默认用户: 在 /etc/wsl.conf 内添加:
[user]
default=wsl其他工具
yay
官方文档 Jguer/yay, 二进制安装:
sudo pacman -S --needed git base-devel
git clone https://aur.archlinux.org/yay-bin.git && cd yay-bin && makepkg -sizinit
yay zsh
chsh -s /bin/zsh
# 使用 chezmoi 拉取 zinit 配置
yay chezmoi
yay source-highlight
# 此处安装到系统目录, 以便用户共用
ZINIT_ROOT="/usr/local/share/zinit"
sudo mkdir -p "$ZINIT_ROOT"
sudo chown wsl: -R "$ZINIT_ROOT"
git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_ROOT/zinit.git"
chezmoi init potoo0常用工具
- btop:
yay btop - duf:
yay duf, ‘df’ alternative - dust:
yay dust, ‘du’ alternative - tmux:
yay tmux
podman
sudo pacman -S podman
podman info
podman run hello
podman image rm -f hellowslu
仓库已归档, 慎用
wsl 的工具, 比如在 wsl 下通过 windows 打开图片/视频等. 安装见教程见其文档: wslu 百科 - 安装.
使用示例:
# 使用 wslview 来替代 xdg-open
sudo ln -s $(which wslview) /usr/local/bin/xdg-open
xdg-open CreateCircle.mp4疑难杂症
WSL2 大量端口被保留
Huge amount of ports are being reserved 中给出的方案是修改端口范围, 查看和修改命令如下:
netsh int ipv4 show dynamicport tcp
netsh int ipv4 show excludedportrange protocol=tcp
netsh int ipv4 set dynamic tcp start=49152 num=16384
netsh int ipv6 set dynamic tcp start=49152 num=16384备份
清除日志和缓存
# 清除日志
# 使总大小小于 100M
journalctl --vacuum-size=100M
# 清理两周前的日志
journalctl --vacuum-time=2weeks
# 清除 go build cache, 默认目录为 ~/.cache/go-build
go clean -cache
# 清除 yay
yay -Ps # 查看使用情况
yay -Sc # 清除缓存备份:
wsl --export Arch "D:\desktop\arch.tar.gz"还原:
wsl --import Arch3 "D:\wsl\arch3" "D:\downloads\arch-20251026.tar.gz" --version 2与 ubuntu 的不同
- 没有
/var/log/auth.log, ssh 认证信息通过journalctl查看 - 没有
/var/log/syslog, 通过journalctl查看