WSL2使用桥接网络,并指定IP
前言
微软终于解决了宇宙级难题了,一直以来的WSL2每次启动IP都是动态分配的,并且是NAT的网络。当然网上对此也有一些解决方案,编写脚本在启动时修改,但是太麻烦了,这次很完美的解决了这个难题。
检查
wsl --version
# 版本>1.0即可
如果出错,请安装最新版本
配置文件
- .wslconfig
用于在 WSL 2 上运行的所有已安装分发版 全局 配置设置。 - wsl.conf
为 WSL 1 或 WSL 2 上运行的 Linux 发行版配置 每个分发 版的设置。
这里详细配置介绍请移步 高级设置配置
配置WSL2使用外部网络
- 使用Hyper-V创建外部网络(其他方式创建也行)
# 查看网络交换机名称
Get-VMSwitch -SwitchType External

- 配置 .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 /usr/lib/systemd/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
WSL2使用桥接网络,并指定IP的更多相关文章
- 网络设置-指定ip
1 克隆centos6.设置网卡 vim /etc/udev/rules.d/70-persistent-net.rules 确定光标所在的位置 d3d删除以下三行,更改NAME为0 vim /etc ...
- VM 虚拟机 CentOS 7 设置 桥接网络
桥接网络的设置: 安装时设置网络为桥接网络! 找到 ip 地址,设置如下 注意ip地址并不是一样的,用以区分 然后在关闭 windows 防火墙,打开 cmd 使用 ping 命令 O ...
- CentOS 网络设置修改 指定IP地址 DNS 网关(转)
CentOS 网络设置修改 指定IP地址 DNS 网关(实测 笔记) 环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G) 系统版本:Centos-6.5-x86_64 ...
- 桥接物理网卡,pipwork指定ip,外网连接,研究salt+docker
1.桥接物理网卡: 首先下载工具: yum -y install --enablerepo=epel bridge-utils 停止服务: 983 systemctl stop docker 删除do ...
- java网络访问指定出口ip
java网络访问指定出口ip Table of Contents 1. socket 2. apache httpclient 1 socket 可以在Socket构造函数中指定使用的本地ip,如: ...
- Linux:Vmware安装linux虚拟机,桥接方式配置静态IP后重启网卡,提示:Error,some other host already uses address 10.252.252.21...
问题: Vmware安装linux虚拟机,桥接方式配置静态IP后重启网卡,提示:Error,some other host already uses address 10.252.252.21... ...
- KVM/QEMU桥接网络设置及kvm资料
KVM/QEMU桥接网络设置 配置kvm的网络有2种方法.其一,默认方式为用户模式网络(Usermode Networking),数据包由NAT方式通过主机的接口进行传送.其二,使用桥接方式(Brid ...
- 使用Docker的macvlan为容器提供桥接网络及跨主机通讯
对于了解Docker容器网络的朋友,我想对虚拟机的网络也不会陌生,毕竟我们是跟随这个时代一起学习和进步的人.相比VM,Docker的网络也在逐步走向成熟,本文主要针对其中的macvlan做下简单的介绍 ...
- docker从零开始网络(二)桥接网络
使用桥接网络 在网络方面,桥接网络是链路层设备,它在网络段之间转发流量.桥接网络可以是硬件设备或在主机内核中运行的软件设备. 就Docker而言,桥接网络使用软件桥接器,该软件桥接器允许连接到同一桥接 ...
- Linux初识之VMWare14中配置Centos7桥接网络环境
1.查看当前初始环境如下:
随机推荐
- KingbaseES V8R6 集群运维案例 -- 归档失败导致 Switchover 失败
案例说明: KingbaseES V8R6集群,备库在执行'repmgr standby switchover'时,切换失败,出现以下故障: 经检查发现是主库归档配置错误,主库出现归档失败导致. 适用 ...
- #二叉堆#JZOJ 4320 旅行
分析 有一个很重要的性质就是如果经过道路数为奇数,把两个点到根节点的路径长加起来就是两个点间的路径长(正负消掉了) 而且众所周知的是奇数+偶数=奇数 可以预处理每个点到根节点的路径长度(按照题目要求) ...
- #NTT,原根#洛谷 3321 JZOJ 4051 [SDOI2015]序列统计
题目 分析 首先朴素dp方程 设\(dp[i][j]\)表示\(i\)个数的数列乘积为\(j\)的方案 那么\(dp[i][j*a[k]\bmod m]=itself+dp[i-1][j]\) 这可以 ...
- #线段树,倒序#CF356A Knight Tournament
题目 分析 要求覆盖必须是第一个覆盖的, 考虑最后一个覆盖的很简单做线段树区间赋值, 那么倒序区间赋值就可以了 代码 #include <cstdio> #include <ccty ...
- 网络协议之:haproxy的Proxy Protocol代理协议
目录 简介 Proxy Protocol的实现细节 版本1 版本2 Proxy Protocol的使用情况 总结 简介 代理大家应该都很熟悉了,比较出名的像是nginx,apache HTTPD,st ...
- 今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
「OpenHarmony 开源贡献者计划 2022」战"码"先锋 PR 征集,"润和赛道"已于6月15日正式开启.套件在手.先机在握,更有润和软件的超多赋能和专 ...
- 掌握 C++ 编译过程:面试中常见问题解析
C++是一种高级编程语言,但是计算机并不能直接理解它.因此,需要将C++代码翻译成计算机可以理解的机器语言.这个过程就是编译过程,是C++程序从源代码到可执行文件的转换过程,包括预处理.编译.汇编和链 ...
- 国产开源数据库OpenGauss的安装运行
步骤一:OpenGauss 的安装 环境 OS:openEuler 20.03 64bit with ARM 架构:arm64 部署:单机 安装过程 1.环境配置 安装依赖包: yum install ...
- HDC2021技术分论坛:ArkUI 3.0让多设备开发更简单!
作者:wanglei,华为UI编程框架技术专家 HarmonyOS自诞生以来,就是为满足分布式多设备应用场景而设计的,大到智慧屏.车机.平板,小到手机.手表.在多设备场景下进行应用UI界面开发,面临新 ...
- k8s 深入篇———— 守护容器[九]
前言 守护容器,也叫做deamonset, 只做整理 正文 顾名思义,DaemonSet 的主要作用,是让你在 Kubernetes 集群里,运行一个 Daemon Pod. 所以,这个 Pod 有如 ...