当前位置: 首页 > article >正文

WSL 2 自动更新 虚拟 IP 到 window hosts

window下的wsl2 开发中使用到 域名映射,但是WSL2 每次启动都会被分配一个虚拟的 ip 地址,每次启动虚拟ip 都不一样,导致要频繁 更改 window 的 hosts 文件,太麻烦了,所以写一个自动执行的 .sh 脚本,每次启动之后 自动获取虚拟ip,并写入到 window 的 hosts 文件中。

1、在 当前用户目录下 新建 script/get_wsl_ip.sh 文件脚本,我的路径 \\wsl$\Ubuntu\home\user1\scripts

#!/usr/bin/bash

# 为 win 设置 wsl host

# win hosts 文件路径
win_hosts_path="/mnt/c/Windows/System32/drivers/etc/hosts"

# 为 wsl2 设置的域名
wsl_domain="dev.domain.com"

# 获取 wsl2 的 ip
wsl_ip=$(ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')

if [ -z "$wsl_ip" ]; then
    echo "无法获取 WSL2 的 IP 地址,请检查网络配置。"
    exit 1
fi

# 判断是否已存在 wsl2 的域名,如果存在则修改,否则追加
if grep -wq "$wsl_domain" "$win_hosts_path"; then
    # 备份原始 hosts 文件
    cp "$win_hosts_path" "$win_hosts_path.bak"
    
    # 使用临时文件避免权限问题
    temp_file=$(mktemp)
    sed "s/.* $wsl_domain/$wsl_ip $wsl_domain/" "$win_hosts_path" > "$temp_file"
    sudo mv "$temp_file" "$win_hosts_path"
else
    # 追加记录到 hosts 文件
    echo "$wsl_ip $wsl_domain" | sudo tee -a "$win_hosts_path" > /dev/null
fi

# 为 wsl 设置 win host
wsl_hosts_path="/etc/hosts"
win_domain="win"
win_ip=$(grep "nameserver" /etc/resolv.conf | awk '{print $2}')

if [ -z "$win_ip" ]; then
    echo "无法获取 Windows 主机的 IP 地址,请检查网络配置。"
    exit 1
fi

if grep -wq "$win_domain" "$wsl_hosts_path"; then
    # 使用临时文件避免权限问题
    temp_file=$(mktemp)
    sed "s/.* $win_domain/$win_ip $win_domain/" "$wsl_hosts_path" > "$temp_file"
    sudo mv "$temp_file" "$wsl_hosts_path"
else
    # 追加记录到 hosts 文件
    echo "$win_ip $win_domain" | sudo tee -a "$wsl_hosts_path" > /dev/null
fi

echo "设置完成。"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

2、修改当前用户目录下  \\wsl$\Ubuntu\home\user1\.bashrc,在末尾加

# Call custom script to get WSL IP and save it for host update
/home/user1/scripts/get_wsl_ip.sh

3、重启 wsl 即可

wsl --shutdown

4、window 的 hosts 会自动添加下面的 ip 地址映射

# wsl 2 虚拟的IP
172.23.38.130 dev.domain.com


 


http://www.kler.cn/a/511946.html

相关文章:

  • 说说HashMap 的位操作以及HashSet的contains方法复杂度是多少?
  • std::forward实现原理与应用场景
  • Linux之socket编程(上)
  • Excel 技巧14 - 如何批量删除表格中的空行(★)
  • 工业现场数据实时采集:解锁工业智能化转型的关键
  • 深入理解Linux系统内存中文件结构以及缓冲区,模拟实现c语言库文件接口
  • 《重生到现代之从零开始的C++生活》—— 类和对象2
  • 【STM32-学习笔记-14-】FLASH闪存
  • 开源模型应用落地-工具使用篇-Spring AI-高阶用法(九)
  • 力扣203题—— 移除链表元素
  • ovs实现lb负载均衡
  • 外部flash烧写算法学习笔记(一)
  • Linux:EXT2文件系统
  • 分布式 IO 模块:开启药品罐装产线高效生产新纪元
  • 技术面试中的软素质技巧性答复集锦
  • 【HarmonyOS NEXT】鸿蒙三方应用跳转到系统浏览器
  • 将 AzureBlob 的日志通过 Azure Event Hubs 发给 Elasticsearch(3.纯python的实惠版)
  • 第01章 分别使用DCMTK和gdcm库,解析DICOM文件系列的dicom标准数据信息
  • win32汇编环境,窗口程序中复杂列表框的应用举例
  • 家政预约小程序08服务分类