前情提要

wsl2 + Docker 写 PHP 啥的,直接 127.0.0.1[:port] 就能访问;

为了写 ASP 又久违地装了 IIS ,然后发现 wsl2 内的服务打不开了;

最后找到的真正原因是挂进 Docker 内的一个文件额外用了符号连接产生的权限问题,那个文件实际用决定不需要了所以删掉就好了;

但是找原因的过程中照着一篇设置 wsl2 的网络的文章操作了下:「给 WSL2 设置静态 IP 地址 - 知乎;」

一个问题是,WSL2 在重启后内部的配置会恢复,但是宿主机对应的vEthernet (WSL)则不会,就导致每次都要执行一次命令改内部的配置,否则网络连上不……

win11 下的坑

可以使用Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress命令进行相应的操作,但是 win11 下这个vEthernet (WSL)被隐藏了,实际要加个参数,写成Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPAddress

所以有什么办法让它直接显示出来呢?

Windows 11 下 WSL2 vEthernet (WSL) 如何取消隐藏? - 知乎

https://www.zhihu.com/question/571415099

探索

前边教程中配置 WSL2 内部时会修改/etc/resolv.conf文件,然后重启会被恢复,然后虽然可以设置脚本自动执行,我嫌麻烦就没弄;

然后发现默认生成的/etc/resolv.conf文件里内容如下:

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.30.80.1

原来可以通过/etc/wsl.conf文件来控制这个文件的生成……

其实现在的需求就是让 WSL2 内部恢复连网并保持,不需要频繁折腾的话固不固定 IP 都无所谓,尤其vEthernet (WSL)怎么命令行配置也不懂;

1
2
3
4
5
6
7
8
9
10
11
12
13
# 配置 WSL2 内部的网络,并写入配置文件
# 给 WSL2 设置静态 IP 地址 - 知乎
# https://zhuanlan.zhihu.com/p/380779630
sudo ip addr del $(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1) dev eth0
sudo ip addr add 192.168.50.2/24 broadcast 192.168.50.255 dev eth0
sudo ip route add 0.0.0.0/0 via 192.168.50.1 dev eth0
sudo echo nameserver 192.168.50.1 > /etc/resolv.conf

# 写入配置到 /etc/wsl.conf
# sudo echo -e "[user]" > /etc/wsl.conf
# sudo echo -e "default = wdssmq" >> /etc/wsl.conf
sudo echo -e "[network]" >> /etc/wsl.conf
sudo echo -e "generateResolvConf = false" >> /etc/wsl.conf

这样上边的命令理论上执行一次就可以了……

然后在powershell中执行下边的命令,让vEthernet (WSL)的 IP 也固定:

1
2
3
4
5
6
# 给 WSL2 设置静态 IP 地址 - 知乎
# https://zhuanlan.zhihu.com/p/380779630
Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False
New-NetIPAddress -IPAddress 192.168.50.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'
Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False
New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 192.168.50.0/24;

wsl2 内部网络测试

1
2
3
4
5
6
7
8
9
10
11
12
# 测试网络连通性
ping -c 1 baidu.com

# 查看 IP
ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1
# 192.168.50.2/24

# 查看路由
ip route show
# default via 192.168.50.1 dev eth0
# 192.168.50.0/24 dev eth0 proto kernel scope link src 192.168.50.2

留存

过程中找到和尝试的各种命令,就不太懂,姑且留存记录下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPAddress

Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPConfiguration

Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPInterface

Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPInterface -Dhcp -- Enabled

Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPInterface -AddressFamily "IPv4"


# ----------------------

# Using PowerShell to Set Static and DHCP IP Addresses – Part 1 | PDQ
# https://www.pdq.com/blog/using-powershell-to-set-static-and-dhcp-ip-addresses-part-1/

$IPType = "IPv4"
$adapter = Get-NetAdapter -IncludeHidden 'vEthernet (WSL)'
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
If ($interface.Dhcp -eq "Disabled") {
# Remove existing gateway
If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
$interface | Remove-NetRoute -Confirm:$false
}
# Enable DHCP
$interface | Set-NetIPInterface -DHCP Enabled
# Configure the DNS Servers automatically
$interface | Set-DnsClientServerAddress -ResetServerAddresses
}