官方教程
https://learn.microsoft.com/zh-cn/windows/wsl/networking
还是建议读官方的,很全很新,不坑人
-
WSL 中的高级设置配置
- wsl.conf 和 .wslconfig 之间有什么差别?
- systemd 支持
-
网络注意事项
-
...
WSL网络
要使 Windows 局域网 IP(192.168.191.52)能够访问 WSL 的服务端口(例如 8000 端口),你可以尝试以下几种方法:
方法一:配置 WSL 网络
通过配置 WSL 使其绑定到 Windows 局域网 IP 上。
-
打开 WSL 终端并运行以下命令以编辑 WSL 的配置文件
/etc/wsl.conf:sudo nano /etc/wsl.conf -
添加以下内容:
[network] generateResolvConf = false -
保存并关闭文件,然后重启 WSL:
wsl --shutdown wsl -
编辑
/etc/resolv.conf文件,将 DNS 服务器地址修改为 Windows 的 DNS 服务器(例如 192.168.191.1):sudo nano /etc/resolv.conf添加以下内容:
nameserver 192.168.191.1 -
再次重启 WSL,确保更改生效。
方法二:在 Windows 上创建端口转发
通过创建 Windows 防火墙规则来转发端口。
-
以管理员身份打开 PowerShell。
-
运行以下命令来启用端口转发:
netsh interface portproxy add v4tov4 listenport=8000 listenaddress=192.168.191.52 connectport=8000 connectaddress=192.168.252.26 -
运行以下命令来允许端口通过防火墙:
netsh advfirewall firewall add rule name="WSL 8000" dir=in action=allow protocol=TCP localport=8000
方法三:配置 WSL2 子系统
如果使用的是 WSL2,可以通过以下方式让其在局域网内可访问:
-
在 WSL 中运行以下命令以获取 WSL IP 地址:
ip addr show eth0找到类似于
inet 192.168.252.26/24的行,记下 IP 地址。 -
在 Windows 上创建端口转发规则,允许外部设备访问:
netsh interface portproxy add v4tov4 listenport=8000 listenaddress=192.168.191.52 connectport=8000 connectaddress=192.168.252.26 -
允许防火墙规则:
netsh advfirewall firewall add rule name="WSL 8000" dir=in action=allow protocol=TCP localport=8000
通过以上方法,你应该可以使 WSL 中的服务在 Windows 局域网 IP 上可访问。
端口转发
# 设置端口转发
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8080 connectaddress=<wsl ip> connectport=8080
# 删除端口转发
netsh interface portproxy delete v4tov4 listenaddress=192.168.0.101 listenport=7001
# 查看所有转发信息
netsh interface portproxy show all
# 清空所有转发信息
netsh interface portproxy reset
配置信息
\\wsl\etc\wsl\wsl.conf
[boot]
# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
systemd=true
# command = service docker start
# Automatically mount Windows drive when the distribution is launched
[automount]
# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true
# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c.
root = /
# DrvFs-specific options can be specified.
# options = "metadata,uid=1003,gid=1003,umask=077,fmask=11,case=off"
# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
mountFsTab = true
# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).
[network]
hostname = LemonUbuntu
generateHosts = true
generateResolvConf = false
# Set whether WSL supports interop processes like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
[interop]
enabled = true
appendWindowsPath = true
# Set the user when launching a distribution with WSL.
[user]
default = root
\c\User\<username>\.wslconfig
# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=8GB
; networkingMode=mirrored
# Sets the VM to use two virtual processors
processors=10
# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
# kernel=C:\\temp\\myCustomKernel
# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
kernelCommandLine = vsyscall=emulate
# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB
# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
# swapfile=D:\\WSL\\ubuntu20\\wsl-swap.vhdx
# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
# pageReporting=false
# Turn on default connection to bind WSL 2 localhost to Windows localhost. Setting is ignored when networkingMode=mirrored
localhostforwarding=true
dnsProxy=false
firewall=false
autoProxy=true
# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
# debugConsole=true
# Enable experimental features
; [experimental]
; hostAddressLoopback=true