WSL2+ArchLinux环境配置
开启wsl
以下指令均需以管理员权限运行
-
启用wsl功能
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart -
启用虚拟平台(完成后重启)
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart -
将wsl2设置为默认版本
wsl --set-default-version 2 -
开启/关闭hyperv
bcdedit /set hypervisorlaunchtype auto bcdedit /set hypervisorlaunchtype off
Arch
安装
-
下载制作好的Arch安装包(选择zip版本,可以自定义安装后文件位置)
-
将zip包放至希望的安装路径,解压,打开文件夹,直接运行Arch.exe即可安装文件系统
-
安装完成后Windows Terminal中就添加了Arch的入口,或者再次运行Arch.exe也可以以命令行的形式进入Arch的shell
安装后打开shell时提示“参考的对象类型不支持尝试的操作”
参照关于使用WSL2出现“参考的对象类型不支持尝试的操作”的解决方法
- 下载NoLsp.exe(需魔法)
- 管理员powershell执行
.\NoLsp.exe C:\Windows\System32\wsl.exe
配置
pacman
使用国内源
# 备份原文件 cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.backup cp /etc/pacman.conf /etc/pacman.conf.backup # 编辑配置文件 vim /etc/pacman.d/mirrorlist # 注释原有源并添加下列源 ## TsingHua Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch ## 163 Server = http://mirrors.163.com/archlinux/$repo/os/$arch ## aliyun Server = http://mirrors.aliyun.com/archlinux/$repo/os/$arch # 编辑配置文件 vim /etc/pacman.conf # 添加以下内容 [archlinuxcn] # The Chinese Arch Linux communities packages. SigLevel = Optional TrustAll # TsingHua Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch # 更新源 pacman -Syy
初始化与更新
# 初始化 ## 设置root密码 passwd pacman-key --init pacman-key --populate pacman -S archlinuxcn-keyring # 更新 pacman -Syyu # 安装基本软件 pacman -S base base-devel git curl wget zsh yay neofetch net-tools dnsutils inetutils
添加用户
# 添加用户并设置密码 useradd -m -G wheel -s /usr/bin/zsh <用户名> passwd <用户名> # 给予用户sudo权限 vim /etc/sudoers ## 添加下面的内容 <用户名> ALL=(ALL:ALL) NOPASSWD: ALL ## 强制保存并退出 :w !sudo tee % :q! # 退出shell exit
-
使用管理员权限powershell运行命令
.\Arch.exe config --default-user <用户名>
配置代理
其实开全局就可以
proxychains(建议)
sudo pacman -S proxychains-ng vim /etc/proxychains.conf # 跳转至最后一行,添加代理 socks5 [ip] [port] # 使用方法:在正常的命令前添加proxychains,就会走代理 proxychains curl https://www.baidu.com
环境变量
-
原理:通过环境变量
ALL_PROXY
配置代理 -
查看DNS服务器(也就是主机)ip:
cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " " 或者直接写个脚本设置代理
#!/bin/bash host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ") export ALL_PROXY="http://$host_ip:7890"
打通ping
-
新建防火墙入站规则
-
打开控制面板\系统和安全\Windows Defender 防火墙
-
点击入站规则->新建规则
-
规则类型:自定义
-
程序:所有程序
-
协议和端口:默认即可
-
作用域:
- 本地ip处选择
任何IP地址
- 远程ip处选择
下列IP地址
,并将wsl2的IP添加进去。(通过ip addr获取wsl2的ip,带上掩码)
- 本地ip处选择
-
操作:允许连接
-
配置文件:三个全选
-
名称描述:请自定义(这一步完成后,从wsl2 ping主机的ip应该可以ping通了)
-
-
防火墙配置
- 打开
控制面板\系统和安全\Windows Defender 防火墙\允许的应用
- 将与代理相关的应用程序均设置为:
允许其进行专用、公用网络通信
- 打开
oh-my-zsh
-
获取安装脚本
sh ./install.sh -
安装插件
# 自动语法高亮 git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting # 在输入命令时会给出建议的命令 git clone https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions # 更新配置文件并应用 vim .zshrc ## 修改为 plugins=(git zsh-syntax-highlighting zsh-autosuggestions) ## 应用 source .zshrc
python
sudo pacman -S python python2# python2有需要的话也可以安装,下面的命令变为python2即可 # 安装pip python -m ensurepip --upgrade # 换清华源 python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple # pip更新 python -m pip install --upgrade pip
docker
-
由于wsl2原生不支持
systemctl
命令,即便成功安装docker,也无法将其服务启动解决方法:
subsystemctl
# 安装subsystemctl sudo pacman -S cargo cargo install subsystemctl # 将cargo的bin文件加入$PATH中 sudo subsystemctl start sudo subsystemctl shell# 会进入subsystemctl的命令行 # 在subsystemctl命令行就可以正常执行systemctl的命令了,比如 sudo systemctl start docker
sudo pacman -S docker docker-compose # docker免sudo sudo gpasswd -a ${USER} docker # 配置国内镜像 sudo vim /etc/docker/daemon.json ## 写入以下内容 { "registry-mirrors": ["http://hub-mirror.c.163.com"] } # 下面的命令需要在powershell中的subsystemctl命令行中运行 sudo systemctl daemon-reload sudo systemctl start docker # 设置为随系统启动(感觉没必要,因为不是每次开wsl都是要用docker) sudo systemctl enable docker
图形界面显示
主要用于matplotlib绘图显示,感觉没必要安装xfce4等桌面环境
-
在windows主机中安装显示服务器(如Vcxsrv)
# 安装tk环境与xorg字体 sudo pacman -S tk sudo pacman -S xorg-fonts-100dpi # 设置DISPLAY变量,可以加入.zshrc中 export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0
-
在py文件中添加
import matplotlib matplotlib.use('TkAgg')
powershell指令
关机、备份/快照快捷指令
-
创建目录及文件
C:\Users\username\Documents\WindowsPowerShell\Microsoft.Powershell_profile.ps1
-
写入如下内容(灵活改动)
function wslrestart { Get-Service LxssManager | Restart-Service } function archdown { wsl --shutdown Arch } function archbackup { wsl --export Arch F:\WSL\Backup\Arch_$(get-date -format "yyMMddHHmm").tar; Get-ChildItem -Path "F:\WSL\Backup" | Sort-Object LastWriteTime -Descending |Select-Object -Skip 1 | Remove-Item -Recurse } function archrestore { wsl --unregister Arch; wsl --import Arch F:\WSL\Arch $(Get-ChildItem "F:\WSL\Backup" -Attributes !Directory * | Sort-Object -Descending -Property CreationTime | select -First 1).FullName; F:\WSL\Arch\Arch.exe config --default-user zero } -
执行下列命令使改动生效
Set-ExecutionPolicy RemoteSigned
快照
# 关闭arch archdown # 制作快照/备份 archbackup # 恢复到最新快照 archrestore
清除历史记录
Remove-Item (Get-PSReadlineOption).HistorySavePath
本文作者:lentikr
本文链接:https://www.cnblogs.com/lentikr/p/16433148.html
版权声明:本作品采用CC-BY-NC-SA 4.0许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步