沉冰浮水

沉冰浮水

做最终到的事,成为最终成为的人!
github
bilibili
mastodon
zhihu
douban

「WSL2_筆記」WSL2 的網路配置什麼的的

前情提要#

wsl2 + Docker 寫 PHP 什麼的,直接 127.0.0.1 [] 就能訪問;

為了寫 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文件裡內容如下:

# 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)怎麼命令行配置也不懂;

# 配置 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 也固定:

# 給 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 內部網絡測試#

# 測試網絡連通性
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

留存#

過程中找到和嘗試的各種命令,就不太懂,姑且留存記錄下:

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
}
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。