https://qiita.com/samunohito/items/019c1432161a950892be

Windows→WSL2はlocalhostでも参照できるようによしなにしてくれてますが、 その逆は情報が見つからなかったので残しておきます。

コマンド

ip route | grep 'default via' | grep -Eo '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}'

解説

WSL2のデフォルトルートとして設定されているアドレスは、 Windows側に出現する仮想アダプタの"vEthernet (WSL)"のアドレスと同一であるのを利用しています。

Windowsにてipconfigをしてみるとこんな感じ。

イーサネット アダプター vEthernet (WSL):

   接続固有の DNS サフィックス . . . . .:
   IPv4 アドレス . . . . . . . . . . . .: 172.17.64.1
   サブネット マスク . . . . . . . . . .: 255.255.240.0
   デフォルト ゲートウェイ . . . . . . .:

WSL2からip routeするとこんな感じ。

$ ip route
default via 172.17.64.1 dev eth0
172.17.64.0/20 dev eth0 proto kernel scope link src 172.17.73.12

VcXsrvとかと組み合わせてGUIアプリを立ち上げる時に便利なテクニックになると思います。

# .bashrcとかに書いておくともっとらくちん
export DISPLAY=`ip route | grep 'default via' | grep -Eo '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}'`:0

余談

WSL2からでもlocalhostで見えるといいなあ…

Why not register and get more from Qiita?

  1. We will deliver articles that match youBy following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficientlyBy "stocking" the articles you like, you can search right away

What you can do with signing up