DEV Community

Li
Li

Posted on

Get the host address for WSL2

For WSL2, the ip address of a running distro is not static, neither the subset WSL2 uses is fixed. If you need to do something like accessing the services on the WSL2 host, you need to get the address by yourself.

Get the ip address the current running distro:

ip -4 -br route get 8.8.8.8 | head -n 1|awk '{print $7}'
Enter fullscreen mode Exit fullscreen mode

Get the ip address the host(the gateway):

ip -4 -br route get 8.8.8.8 | head -n 1|awk '{print $3}'
Enter fullscreen mode Exit fullscreen mode

And get the netmask:

ifconfig eth0 |grep netmask|awk '{print $4}'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)