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

bash 复制代码
#!/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,在末尾加

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

3、重启 wsl 即可

bash 复制代码
wsl --shutdown

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

bash 复制代码
# wsl 2 虚拟的IP
172.23.38.130 dev.domain.com
相关推荐
CertiK23 分钟前
CertiK《Hack3d:2025年第二季度及上半年Web3.0安全报告》(附报告全文链接)
网络
一只小鱼儿吖2 小时前
进程代理单窗口单IP技术:原理、应用与实现
网络·网络协议·tcp/ip
稳联技术2 小时前
Ethernet IP与Profinet共舞:网关驱动绿色工业的智慧脉动
网络·网络协议·tcp/ip
学习3人组2 小时前
CentOS配置网络
linux·网络·centos
计算机毕设定制辅导-无忧学长2 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
高兴达3 小时前
RPC框架--实现一个非常简单的RPC调用
网络协议·rpc·firefox
~山有木兮3 小时前
LiteHub中间件之限流实现
网络·http·中间件
cui_win3 小时前
【网络】Linux 内核优化实战 - net.core.flow_limit_table_len
linux·运维·网络
BD_Marathon4 小时前
虚拟机网络检查
网络
游戏开发爱好者84 小时前
iOS App首次启动请求异常调试:一次冷启动链路抓包与初始化流程修复
websocket·网络协议·tcp/ip·http·网络安全·https·udp