当前位置:网站首页>wsl2设置静态ip(固定ip)static ip
wsl2设置静态ip(固定ip)static ip
2022-07-30 05:47:00 【littlebird4】
本方法是为了尽可能让配置在wsl中执行,有一键脚本的方法,参见 给 WSL2 设置静态 IP 地址
有两个地方需要设置静态ip:
- wsl内部的eth0网卡;假定ip为172.22.0.2;
- Windows的vEthernet (WSL)网卡;假定ip为172.22.0.1;
Windows设置
cmd命令行以管理员权限执行以下语句:
powershell -c "Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress 172.22.0.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 172.22.0.0/24;"
或者直接在power shell里执行上面双引号里的内容即可。
wsl设置
设置ip
在启动脚本 ~/.bashrc 里添加:
# static ip
if [ "$(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1)" != "172.22.0.2/24" ];
then
sudo ip addr del $(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1) dev eth0
sudo ip addr add 172.22.0.2/24 broadcast 172.22.0.255 dev eth0
sudo ip route add 0.0.0.0/0 via 172.22.0.1 dev eth0
fi
免密sudo权限
为了执行上一步,需要这个权限。
在 /etc/sudoers 里添加:
UserName ALL=(ALL) NOPASSWD:ALL
配置DNS
在 /etc/wsl.conf 文件写入
[network]
generateResolvConf = false # 不自动生成dns配置,必须
generateHosts = false # 是否生成hosts文件,可选
[interop]
enabled = false # 不能调用Windows进程,可选
appendWindowsPath = false # 不添加Windows路径,可选
在 /etc/wsl.resolv文件写入(如果原来是软连接文件,需要先删除)
nameserver 8.8.4.4 # 写入你本地最好用的dns就行
边栏推荐
- SQL并列排序问题
- QT serial port dynamically displays a large number of data waveform curves in real time (5) ======== "Final perfect solution version"
- Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
- 图扑数字孪生青岛城轨,赋能智慧交通低碳发展
- 单片机第一步
- 你不知道的JS思考题
- Three working modes of CPU: real mode, protected mode, long mode
- Explore the efficiency of make_shared
- This beta version of Typora is expired, please download and install a newer;解决方法
- IO进程线程->文件IO->day2
猜你喜欢

QT Weekly Skills (1) ~~~~~~~~~ Running Icon

服务器基础知识:包含基本概念,作用,服务器选择,服务器管理等(学习来自米拓建站)

Kunlun state screen production (serial 3) - based article (button serial port to send)

lcd1602调试

xxx is not in the sudoers file.This incident will be reported error

Diwen serial screen production (serialization 1) ===== preparation work
![[Quick MSP430f149] Notes on learning MSP430f149 during the game](/img/06/741c609b24be007718091b8348666c.png)
[Quick MSP430f149] Notes on learning MSP430f149 during the game

Acwing Brush Questions Section 1

Unity Shader 空间坐标系

阿里京东“喜提”国电投,顶流互联网和能源大厂为何合作?
随机推荐
[Jiangsu University Automation Association stm32F103c8t6] Notes [Initial 32 MCU and EXTI External Interrupt Initialization Parameter Configuration]
sizeof和strlen最全区别,以及指针和数组运算解析
SSH-RSA密钥
Knowledge of the day: handwritten deep copy and shallow copy (solves the problem of circular references)
Word使用中常用的快捷键
动态规划进阶 JS
服务器基础知识:包含基本概念,作用,服务器选择,服务器管理等(学习来自米拓建站)
ssh script space character conversion
牛顿迭代法求方程的根
Explore the efficiency of make_shared
租用服务器训练yolov3模型
xxx is not in the sudoers file.This incident will be reported错误
QT serial 2: LORA test platform based on QT and STM32H750 (1)
[Punctuality Atom] Learning and use of IIC (unfinished...)
测试第二题
Antd 树拖拽一些细节,官网没有,摸坑篇
Kunlun State Screen Production (Serialization 2)---Basic Chapter (setting and display, serial transmission)
图扑数字孪生煤矿开采系统,打造采煤“硬实力”
VsCode打开终端的方法
Insert map data efficiently