首页 > 其他分享 >WSL2使用桥接网络,并指定IP

WSL2使用桥接网络,并指定IP

时间:2022-12-24 20:44:15浏览次数:79  
标签:systemd false 桥接 IP WSL wsl true WSL2

前言

微软终于解决了宇宙级难题了,一直以来的WSL2每次启动IP都是动态分配的,并且是NAT的网络。当然网上对此也有一些解决方案,编写脚本在启动时修改,但是太麻烦了,这次很完美的解决了这个难题。

检查

wsl --version
# 版本>1.0即可

如果出错,请安装最新版本

配置文件

  • .wslconfig
    用于在 WSL 2 上运行的所有已安装分发版 全局 配置设置。
  • wsl.conf
    为 WSL 1 或 WSL 2 上运行的 Linux 发行版配置 每个分发 版的设置。

这里详细配置介绍请移步 高级设置配置

配置WSL2使用外部网络

  • 使用Hyper-V创建外部网络(其他方式创建也行)
# 查看网络交换机名称
Get-VMSwitch -SwitchType External

image

  • 配置 .wslconfig
    存储在目录中 %UserProfile%,或者 PowerShell 中使用 cd ~ 访问主目录
[wsl2]
networkingMode=bridged
vmSwitch=bridge # 上图中的Name值
ipv6=true

重启 WSL2

wsl --shutdown
wsl
ip a

可以看到eth0的网络是桥接网络了

设置特定静态IP

修改 .wslconfig

[wsl2]
networkingMode=bridged
vmSwitch=WSL_external
dhcp=false # 禁止动态分配
ipv6=true

dhcp=false时会读取 cd /lib/systemd/network/wsl_external.network 的网络配置

vim wsl_external.network 加入以下内容

[Match]
Name=eth0
[Network]
Description=bridge
DHCP=false
Address=192.168.1.10/24 # 自行修改
Gateway=192.168.0.1 # 自行修改

重启WSL2 (wsl --shutdown)
你会发现eth0已经有自定义的网络了

修改DNS禁止自动生成

添加 /etc/wsl.conf 加入以下内容

[boot]
systemd=true
[network]
generateResolvConf = false

先删除再创建,因为resolv.conf是软链接创建的

rm -f /etc/resolv.conf
echo "nameserver 223.5.5.5" > /etc/resolv.conf

重启WSL2 (wsl --shutdown)

而且还启用了systemd (systemctl 命令回来了)

systemctl list-unit-files --type=service

systemctl restart systemd-networkd
systemctl enable systemd-networkd
systemctl restart systemd-resolved.service
systemctl enable systemd-resolved.service

常用配置

.wslconfig

[wsl2]

# Bridged networking
networkingMode=bridged
vmSwitch=bridge
dhcp=false
ipv6=true

# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB

# Sets the VM to use two virtual processors
processors=2

# Turn on/off default connection to bind WSL 2 localhost to Windows localhost ; default is true
localhostforwarding=true

# Turns on/off output console showing contents of dmesg when opening a WSL 2 distro for debugging ; default is false
debugConsole=false

# Turns on/off running GUI apps ; default is true
guiApplications=true

wsl.conf

# 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 = DemoHost
generateHosts = false
generateResolvConf = false

# Set whether WSL supports interop process 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 = false
appendWindowsPath = false

# Set the user when launching a distribution with WSL.
[user]
default = DemoUser

# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
[boot]
command = service docker start

https://learn.microsoft.com/en-us/windows/wsl/wsl-config
https://github.com/luxzg/WSL2-fixes
https://github.com/luxzg/WSL2-fixes/blob/master/networkingMode%3Dbridged.md

标签:systemd,false,桥接,IP,WSL,wsl,true,WSL2
From: https://www.cnblogs.com/lic0914/p/17003251.html

相关文章

  • 图文并茂解释TCPIP 3次握手4次挥手
    1.TCP与UDP的区别TCP(TransmissionControlProtocol,传输控制协议)是面向连接的,可靠的通信协议,而UDP(UserDatagramProtocol,用户数据报协议)是无连接的,不可靠的通信协议......
  • Centos7上使用jenkins执行pipeline在容器内部拉取环境运行代码的各种坑
    环境Centos7上部署了三个服务bitbucket192.168.1.156:7900jenkins192.168.1.156:8080docker服务坑一:忘了bitbucket的登录密码解决办法这个直接去bitbucket官网:lo......
  • [Typescript] Branded type
    Forexample,wehaveatyperepersentcurrency:USD,valueshouldbesomethinglike '$123',astringtypewith`$`andnumber. Solet'ssaywemakeaUSDt......
  • SIP协议
    目录一、SIP协议概念二、SIPURL三、SIP协议消息体1、请求消息分类2、请求消息结构3、响应消息分类四、SIP呼叫流程一、SIP协议概念会话启动协议SIP(SessionInitiation......
  • 前端知识学习案例-Typescipt实现
     ......
  • [NOIP2009 普及组] 多项式输出
    [NOIP2009普及组]多项式输出题目描述一元$n$次多项式可用如下的表达式表示:$$f(x)=a_nxn+a_{n-1}x{n-1}+\cdots+a_1x+a_0,a_n\ne0$$其中,$a_ix^i$称为$i$次项,$a......
  • 前端知识学习案例-Clipboard API一键复制和一键粘贴功能
      ......
  • 彻底弄懂Javascript模块导入导出
    笔者开始学习Javascript的时候,对模块不太懂,不知道怎么导入模块,导出模块,就胡乱一通试比如importxxfrom'test.js'不起作用,就加个括号import{xx}from'test.js'反......
  • P1003 [NOIP2011 提高组] 铺地毯
    P1003[NOIP2011提高组]铺地毯题目描述为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯。一共有$n$张地毯......
  • IPTV原理(一)
    视频解码标准MPEG2:与DVD编码制式一致,应用广泛,大量解码芯片支持;但占用带宽较大,存储空间大。MPEG4:带宽要求低,2.5M能达到DVD效果,处理器资源要求低;存在多个分支,要考虑相互兼容......