WSL2设置自动更改hosts映射
March 22, 2022 默认分类
WSL2设置自动更改hosts映射
每次关机开机后,WSL默认会自动更换IP地址,对于远程访问编程很不友好。
下面几步是自动更改wsl中Ubuntu20.04系统的hosts映射:
- 将一下内容复制到/etc/profile或者~/.bashrc中
ipaddr=$(ifconfig eth0 | grep 'inet ' | awk '{print $2}') oldIpaddr=`grep wslhost /mnt/c/Windows/System32/drivers/etc/hosts|awk '{print $1}'` if [ "${ipaddr}" != "${oldIpaddr}" ];then vim "+:%s/^.*wslhost/$ipaddr\t\twslhost/g" '+:wq' -E /mnt/c/Windows/System32/drivers/etc/hosts fi
如果重新打开ubuntu,发现提示permission deny。在windows下找到hosts,右键,属性,安全,给user用户权限。