wsl 安装 archlinux 子系统,以及一些配置解决一些常见问题

几年前写过一些相关教程,但是wsl这几年更新的太多了,很多设置都不太一样了。Windows上跑各种 终端ai,可能还是wsl方便,Windows使用powershell跑起来要么乱码,要么读取写入文件费事,所以干脆还是在wsl 安装 archlinux子系统方便了。相比于其他发行版的子系统,自己更喜欢archlinux一点,安装一些软件简单很多,其他发行版有时候安装一些常用的软件很费事。

1、Windows安装wsl,启动虚拟平台功能:

#wsl
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

#启用虚拟平台
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

建议安装完,之后重启一下电脑,不重启也可以。

2、安装archlinux 子系统:

#安装
wsl --install -d archlinux

#安装到d盘
wsl --install -d archlinux --location D:\WSLOS\Arch

#wsl --install -d archlinux --name 自定义名称 --location 自定义路径

主要还是两个配置文件:

.wslconfig 用于跨 WSL 2 上运行的所有已安装分发版全局配置设置。

放在windows系统%UserProfile% 目录中,比如:C:\Users\你的用户名.wslconfig

wsl.conf 用于为 WSL 1 或 WSL 2 上运行的 Linux 发行版配置每个分发版的设置。放在/etc/wsl.conf。

3、更新源,安装常用软件:

#更新系统
pacman -Syu

#如果更新不动,初始化密钥环
pacman-key --init
pacman-key --populate archlinux
pacman-key --refresh-keys

#安装常用软件
pacman -Sy base base-devel vim git wget curl net-tools

4、解决 nat 网络设置代理问题:

#Windows终端执行。在Windows系统中修改wsl配置文件
notepad "$env:USERPROFILE\.wslconfig"

#添加或者修改
[wsl2]
networkingMode=mirrored
autoProxy=true

#关闭wsl,重新启动生效
wsl --shutdown

5、安装中文字体,设置中文,解决中文乱码问题:

#取消注释zh_CN.UTF-8 
sed -i 's/#zh_CN.UTF-8 UTF-8/zh_CN.UTF-8 UTF-8/' /etc/locale.gen
#同时,最好也启用英文UTF-8区域,以避免某些程序出现兼容性问题:
sed -i 's/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen

#生成 Locale:
locale-gen

#设置默认区域:
echo 'LANG=zh_CN.UTF-8' | tee /etc/locale.conf

#安装中文字体,文泉驿正黑
pacman -S wqy-zenhei

6、配置 systemd:

#修改配置文件
vim /etc/wsl.conf

#添加或者修改
[boot]
systemd=true

#关闭wsl,重新启动生效
wsl --shutdown

7、安装配置docker:

#安装docker 
pacman -S docker

#根目录(/)挂载为可共享
vim /etc/systemd/system/mount-root-rshared.service

#添加
[Unit]
Description=Remount / with shared propagation
Requires=-.mount
After=-.mount

[Service]
Type=oneshot
ExecStart=/bin/mount --make-rshared /

[Install]
WantedBy=local-fs.target

#启动挂载
systemctl enable --now mount-root-rshared.service

#启动docker
systemctl eanle --now docker

留言

* - 必填