WSL 安装及其相关配置

482 阅读4分钟

install

wsl -l -o

查看现可下载分发版列表.(最好开代理)

~ wsl -l -o
以下是可安装的有效分发的列表。
使用 'wsl.exe --install <Distr>' 安装。

NAME                            FRIENDLY NAME
Ubuntu                          Ubuntu
Debian                          Debian GNU/Linux
kali-linux                      Kali Linux Rolling
Ubuntu-18.04                    Ubuntu 18.04 LTS
Ubuntu-20.04                    Ubuntu 20.04 LTS
Ubuntu-22.04                    Ubuntu 22.04 LTS
Ubuntu-24.04                    Ubuntu 24.04 LTS
OracleLinux_7_9                 Oracle Linux 7.9
OracleLinux_8_7                 Oracle Linux 8.7
OracleLinux_9_1                 Oracle Linux 9.1
openSUSE-Leap-15.6              openSUSE Leap 15.6
SUSE-Linux-Enterprise-15-SP5    SUSE Linux Enterprise 15 SP5
SUSE-Linux-Enterprise-15-SP6    SUSE Linux Enterprise 15 SP6
openSUSE-Tumbleweed             openSUSE Tumbleweed

移动存储文件

wsl --install <distr-name> 将会 -> dir: C:\Users\<username>\AppData\Local\Packages\<ditr-name>,wsl 的虚拟存储文件位于 dir/LocalState.

移动该ext4.vhdx :

wsl --manage <distr-name> --move </path/dir>

更改 distro-name

实际也更改了ext4.vhdx 的位置.

~ wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-18.04    Running         2
  docker          Running         2

目前好像只能通过导出再导入 (真麻烦 )-: :sweat:

wsl --export <distr-name> </path/backup.tar>
wsl --import <new-distr-name> </path/to-vhdx-dir> </path/backup.tar>

使用新式 Windows 终端 访问wsl

  • 启动命令行
# 启动时 cd 到 home
C:\Windows\system32\wsl.exe -d <distr-name> --cd ~
  • 图标:https://assets.ubuntu.com/v1/49a1a858-favicon-32x32.png
  • Ubuntu终端经典外观配色

![screen-capture](langyu.world/2025/01/02/… 安装及其相关配置/8d8c6cd6db88c543cce7b682c3d4c027.png)

网络

wsl 默认网络模式为 NAT,不能直接使用宿主机的 localhost 代理,多个distrobution共享同一个网络栈(意味着有相同的ip :sweat: )-:

什么是 NAT: 通过对 IP 数据包中源 IP / 端口,目的 IP / 端口修改,使得在同一个私有子网中的主机通过一个共有 IP 对外访问的网络技术。

几种 NAT 类型

前三种:映射 IP / port目的 IP / port 无关,前者固定。

对称NAT:对于每一个不同的 目的 IP / port映射 IP / port 都不同,动态生成。

Name示例图
完全圆锥型NAT(Full cone NAT,也叫NAT1)
1. 一旦内部地址(iAddr:iPort)映射到外部地址(eAddr:ePort),所有发自iAddr:iPort的数据包都经由eAddr:ePort向外发送。
2. 任意外部主机都能经由发送数据包给eAddr:ePort到达iAddr:iPort。
![截图](langyu.world/2025/01/02/… 安装及其相关配置/f959905884b8f13a8b2db18f420d2c9e.png)
受限圆锥型NAT(Restricted cone NAT,也叫NAT2)
1. 一旦内部地址(iAddr:iPort)映射到外部地址(eAddr:ePort),所有发自iAddr:iPort的数据包都经由eAddr:ePort向外发送。
2. 唯iAddr:iPort曾经发送数据包到外部主机(nAddr:any),外部主机才能经由发送数据包给eAddr:ePort到达iAddr:iPort。(注:any指外部主机源端口不受限制。)
![截图](langyu.world/2025/01/02/… 安装及其相关配置/653ac91a75604219cd939402dc3c64dd.png)
端口受限圆锥型NAT(Port-Restricted cone NAT,也叫NAT3) 类似受限制锥形NAT,但是还有端口限制。
1. 一旦内部地址(iAddr:iPort)映射到外部地址(eAddr:ePort),所有发自iAddr:iPort的数据包都经由eAddr:ePort向外发送。
2. 在受限圆锥型NAT基础上增加了外部主机源端口必须是固定的
![截图](langyu.world/2025/01/02/… 安装及其相关配置/ed4537e078f93a31382893730f72077a.png)
对称NAT(Symmetric NAT,也叫NAT4)
1. 每一个来自相同内部IP与端口,到一个特定目的地IP和端口的请求,都映射到一个独特的外部IP和端口。 同一内部IP与端口发到不同的目的地和端口的信息包,都使用不同的映射。
2. 只有曾经收到过内部主机数据的外部主机,才能够把数据包发回。
![截图](langyu.world/2025/01/02/… 安装及其相关配置/68ccbc65d8d30b41cd6904a1446b7206.png)

解决多个distr共享同一个ip

额。。没得解决,至少不能通过现有的配置项解决,所有的 wsl distr 都是共享一个 wsl vm ,本质上处于同一 network namespace,也有一些 “奇淫技巧”可以实现不同ip,但都有一些这样那样的限制,不够通用 --_--

解决代理问题

最好关闭wsl的防火墙

~/.wslconfig

[wsl2]
firewall=false

两种方式:

  1. 通过局域网形式手动设置到宿主机的代理.
  2. 使用 mirror网络模式. (该种方式下宿主机通过 localhost:port 访问 docker 容器 会有些问题,通过 LAN IP 访问是没问题的。)

mirror

局域网代理

  1. 开启代理软件的局域网访问。
  2. 固定宿主机 wsl 虚拟网卡的 ip (电脑重启后会失效 :sweat:)
# 先看看现在的 ip 与 mask
ipconfig
# PS 下管理员
netsh interface ip set address name="<wsl-vEthernet-name>" static <host-ip> <mask>
  1. 固定 wsl 实例 ip (方便后面访问 wsl 内服务)
sudo vim /etc/netplan/01-netcfg.yaml
network:
 version: 2
 ethernets:
   eth0:
     dhcp4: no
     addresses:
       - <ip>/N  # 静态 IP 地址和子网掩码,可以参照 ip addr shouw eth0
     gateway4: <host-ip> # 宿主机固定的 ip
     nameservers:
       addresses:
         - 8.8.8.8           # 首选 DNS
         - 8.8.4.4           # 备用 DNS

应用

sudo netplan apply
  1. 为 apt,curl,docker,git 设置代理

使用 zsh 的配置文件中自定义的 环境变量导出脚本代理 apt curl

proxy () {
  host_proxy=<host-ip>:<port>
  export ALL_PROXY="http://${host_proxy}"
  export HTTP_PROXY="http://${host_proxy}"
  export HTTPS_PROXY="http://${host_proxy}"
  export NO_PROXY=localhost,127.0.0.1
  git config --global http.proxy "http://${host_proxy}"
  git config --global https.proxy "http://${host_proxy}"
  
  echo -e "Acquire::http::Proxy \"http://${host_proxy}\";\nAcquire::https::Proxy \"http://${host_proxy}\";" | sudo tee  /etc/apt/apt.conf.d/apt-proxy.conf > /dev/null
}

# 取消代理
unproxy () {
  unset ALL_PROXY
  unset HTTP_PROXY
  unset HTTPS_PROXY
  git config --global --unset http.proxy
  git config --global --unset https.proxy
  echo -e "" | sudo tee  /etc/apt/apt.conf.d/apt-proxy.conf > /dev/null
}

为 docker 设置代理

For docker daemon (to access images stored on Docker Hub and other registries, and to reach other nodes in a Docker swarm):

sudo vim /etc/docker/daemon.json
{
  "proxies": {
    "http-proxy": "<host-proxy>",
    "https-proxy": "<host-proxy>",
    "no-proxy": "*.test.example.com,.example.org,127.0.0.0/8"
  }
}
sudo systemctl restart docker

For builds and containers

vim ~/.docker/config.json
{
 "proxies": {
   "default": {
     "httpProxy": "<host-proxy>",
     "httpsProxy": "<host-proxy>",
     "noProxy": "*.test.example.com,.example.org,127.0.0.0/8"
   }
 }
}

For docker building image,only avaliable during building the image

docker build --build-arg HTTP_PROXY="<host-proxy>" .

使用oh-my-zsh

安装 zsh

sudo apt update && apt install zsh

安装 oh-my-zsh

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

安装插件

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
vim ~/.zshrc
plugins=(git zsh-autosuggestions zsh-syntax-highlighting history-substring-search)
source ~/.zshrc

参考: