WSL 使用 Proxy 连接 Github

1
2
3
4
5
export hostip=127.0.0.1 
export hostport=10808 // 取决于win的proxy工具设置的端口号
export HTTPS_PROXY="socks5://${hostip}:${hostport}";
export HTTP_PROXY="socks5://${hostip}:${hostport}";
export ALL_PROXY="socks5://${hostip}:${hostport}";

下面的命令只针对 github.com,在国内还可能会用到 gitee.com,所以不能将所有的 git 站点都添加上代理。

1
2
3
4
## 设置代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:1086
## 取消代理
git config --global --unset http.https://github.com.proxy

如果没有代理也不要急,有专门的 cdn 加速:

1
2
3
git config --global url."https://github.com.cnpmjs.org".insteadOf "https://github.com"
git config --global url."https://ghproxy.com".insteadOf "https://github.com"
git config --global url."https://hub.fastgit.xyz/".insteadOf "https://github.com/"

因为没有官方 CDN,上面都是用爱发电,没准哪天就挂了。

WSL2 优化

搬迁

[WSL2]WSL2迁移虚拟磁盘文件ext4.vhdx_wsl ext4.vhdx-CSDN博客

压缩

随着使用时间的延长,WSL2 占用的硬盘空间会越来越多,这个时候就需要对其文件进行压缩。方法如下

1
2
3
4
5
6
7
8
wsl --shutdown
diskpart
# open window Diskpart
select vdisk file="C:\Users\Ci\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx"
# select vdisk file="C:\Users\Ci\AppData\Local\Packages\KaliLinux.54290C8133FEE_ey8k8hqnwqnmg\LocalState\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk

内存

当然,WSL2 也会带来内存占用的问题,可以打开 $home/.wslconfig,进行如下设置

1
2
3
[wsl2]
memory=4GB
swap=0

Wsl 2 设置静态 DNS 服务地址及 Linux 和 Windows 主机网络互相访问设置

DNS 解析
wsl 2 使用的 DNS 解析服务器设置在文件:/etc/resolv.conf 内设置,这是一个 link 文件,真正的文件路径是:/run/resolvconf/resolv.conf,这个文件是每次 wsl 启动时自动生成的,内容如下:

1
2
3
4
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.24.144.1

nameserver 后面的就是指定的 DNS 服务器地址。我发现有时候分配的服务器地址是无效的,如:127.0.0.53,使用 ping 来测试发现提示无法解析地址。需要设置一个固定的 DNS 地址,方法如下:

新建文件:/etc/wsl.conf,内容如下:

1
2
[network]
generateResolvConf = false

以上配置好就不会自动创建 /run/resolvconf/resolv.conf 文件了。

退出 wsl,关机然后重新启动:

1
2
3
exit
wsl --shutdown
wsl

删除 link 文件:/etc/resolv.conf:

1
rm /etc/resolv.conf

然后新建 /etc/resolv.conf 文件,内容如下,可自定义 nameserver 地址:

1
nameserver 114.114.114.114

再次退出 wsl,关机然后重新启动
查看 /etc/resolv.conf 文件是否是之前自定义的地址:

1
cat /etc/resolv.conf

测试网络服务:

1
ping niekun.net

如果你想对将要安装的 deb 软件包使用 dpkg 命令,在这里是如何完成它:

1
sudo dpkg -i path_to_deb_file

记录 Windows11 安装 WSL2 配置环境时遇到的坑,以及一些常用命令(持续更新)

https://blog.csdn.net/luyuyingyingying/article/details/123110417

WSL_systemd

安装好新版 wsl 后,通过以下命令来配置 wsl 启用 systemd:

1
echo -e "[boot]\nsystemd=true" | sudo tee -a /etc/wsl.conf

配置后需要通过 wsl --shutdown 命令关闭 wsl,来进行 wsl 的完整重启。

如何判断 Systemd 是否启用成功

判断 wsl 是否已启用 systemd,可通过以下命令查看:

1
ps --no-headers -o comm 1

如果命令返回的是 init 说明 systemd 未启用,如果是 systemd 那么你的 systemd 已启用成功了。

查看已启用的 Systemd Service

通过 systemctl 的 list-units、list-unit-files 命令,都可以很好的查看目前的 systemd 状态,命令如下:

1
2
systemctl list-units --type=service
systemctl list-unit-files --type=service --state=enabled

解决 Exec: Exec Format Error

1
sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

After wsl --shutdown and logging back in, now next to systemd, explorer.exe and the like are finally working again!
[WSL2][systemd][interop] Unable to Execute Windows Binary when systemd enabled · Issue #8843 · microsoft/WSL (github.com)

启用 Systemd 支持后无法从 Wsl2 运行 Exe

1
sudo update-binfmts --disable cli

解决方案

解决:wsl 系统无法访问此文件-CSDN博客