LoginSignup
65
55

More than 3 years have passed since last update.

WSL2のGUI設定でつまずいたところ

Last updated at Posted at 2019-11-26

簡単に言うと

たくさんのサイトで紹介されている方法を試してもエラーが発生する。

$ export DISPLAY=:0
$ GUI apps command
Error: Can't open display: :0

以下の方法でIPアドレスを指定することで解決した。

$ export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0

WSL2の設定

多くの記事ではDISPLAY環境変数を以下のように紹介しています。

$ export DISPLAY=localhost:0.0
$ export DISPLAY=127.0.0.1:0.0
$ export DISPLAY=:0.0
$ export DISPLAY=:0

上記4つは全て同じ意味合いで、Xサーバの場所を指定しています。[1]
これらを、.bashrc等に記述することで、設定が反映されます。

しかし、これらの設定を行ったのにも関わらず、以下のエラーに遭遇しました。
GUIの表示ができない...

$ export DISPLAY=:0
$ GUI apps command
Error: Can't open display: :0

解決策

WSL2のIssue[2]を参考に、以下のコマンドを実行したところGUIを表示をすることができました。

$ export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0

詳しくコマンドを見ていきます。

DISPLAY=$(色々):0

色々の処理から得られた結果を設定しています。例えばaaa.b.c.dという結果を得たのあれば、DISPLAY=aaa.b.c.d:0.0と言った具合にセットされます。

cat /etc/resolv.conf

resolv.confの内容を取得します。後述しますが、ここで得られたnameserverのIPアドレスを設定することにより、問題を解決することができました。

$ cat /etc/resolv.conf 
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver xxx.xx.xxx.x

grep nameserver

resolv.confから、nameserverという文字列が含まれる行を抽出します。

$ cat /etc/resolv.conf | grep nameserver
nameserver xxx.xx.xxx.x

awk '{print $2}'

空白区切りで2番めの文字(IPアドレス)を取得します。

$ cat /etc/resolv.conf | grep nameserver | awk '{print $2}'
xxx.xx.xxx.x
$ cat /etc/resolv.conf | grep nameserver | awk '{print $1}'
nameserver

最終的に得られるもの

resolv.confで記述されたnameserverのアドレスが設定され、export DISPLAY=xxx.xx.xxx.x:0.0となります。

解決した理由

問題が発生した理由として、$ export DISPLAY=:0.0をした際に、IPアドレスが意図したものに設定されていなかったことが考えられます。知識が乏しいのでこの程度の考察しかできませんが、IPアドレスが変わっても対応できるので、とりあえずの対策でした。

おまけ

VcXsrvの設定

  1. Select display settings: どれでもいい
  2. Select how to start clients: Start no client
  3. Extra settings: 全てにチェック(デフォルトだと、Disable access controlはチェックされていない)

一応ファイアウォールの設定もしました。

Ref.

  1. WSL上にXサーバをインストールしてGUIを実現する(VcXsrv編) https://www.atmarkit.co.jp/ait/articles/1812/06/news040.html
  2. Can't use X-Server in WSL 2 https://github.com/microsoft/WSL/issues/4106
  3. WSL2におけるVcXsrvの設 https://qiita.com/ryoi084/items/0dff11134592d0bb895c
65
55
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
65
55