WSL에서 X로 그래픽 애플리케이션 실행
X Window 시스템 아키텍처는 X 서버와 X 클라이언트를 별도의 시스템에서 실행할 수 있도록 하며 WSL의 경우 X 서버는 OpenGL 가속을 제공하는 Windows 서버가 되며 클라이언트는 WSL 환경에서 실행되는 그래픽 Linux 응용 프로그램이 됩니다.
윈도우용 X서버 설치
아래 Windows용으로 사용 가능한 여러 X 서버 중 하나를 설치하고 실행합니다. 본문서에서는 MobaXTerm을 설치했습니다.
MobaXTerm을 설치 후 실행하면 아래 그림과 같이 좌측에 WSL-Ubuntu session이 보이는데,
더블 클릭하여 화면과 같이 WSL(X 클라이언트)로 접속합니다.
이제 Ubuntu(WSL)에서 실행할 그래픽 응용프로그램을 설치합니다. 본문서에서는 xclock을 설치합니다.
xclock은 DISPLAY 변수가 제대로 설정되었는지 테스트하는 편리한 도구이며, "xclock" 명령을 실행하면 GUI 기반 시계 화면이 나타납니다.
xclock 설치
WSL에는 xclock가 설치되어 있지 않으니,
WSL에 접속한 후 apt-get install x11-apps 명령어를 실행하여 Ubuntu용 X Window 응용프로그램인 xclock을 설치합니다.
sudo apt-get install x11-apps
xclock 실행
WSL에는 xclock가 설치되었으니, xclock 명령어를 실행합니다. 화면에 그래픽 시계가 나타납니다.
xclock
X서버에 연결이 안될 때
그래픽 응용 프로그램이 실행 중인 X 서버에 연결할 수 없는 경우 응용 프로그램(xclock)을 실행하기 전에 터미널에 다음 명령을 붙여넣거나 ~/.bashrc에 추가하여 X 클라이언트 환경을 설정합니다.
export DISPLAY=:0 # in WSL 1
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 # in WSL 2
export LIBGL_ALWAYS_INDIRECT=1
WSL에서 GUI 프로그램 실행
이제 WSL에서 그래픽 애플리케이션을 수행할 수 있는 환경을 확인했으니, Linux GUI 프로그램을 수행하여 GUI 프로그램을 사용할 수 있습니다. 본문서에서는 nautilus을 설치합니다.
sudo apt install nautilus
nautilus 명령어로 실행한 화면입니다. 실행하면서 몇 개의 오류 메시지가 뜨는데 아래와 같이 실행은 됩니다.
nautilus
참고 :
XServer(예: 저장된 config.xlaunch를 사용하는 vcxsrv.exe)를 시작하고 Windows DOS 배치 파일에서 XWindows 세션을 시작할 수 있습니다. 이 방법을 사용하면 DOS ipconfig를 사용하여 가상 이더넷 WSL2 IP를 결정하고 세션을 시작하기 전에 DISPLAY 환경 변수를 설정할 수 있습니다. 이것은 가상 이더넷 IP가 Windows를 재부팅할 때마다 변경되기 때문에 유용합니다(따라서 위의 DISPLAY에서 awk를 사용해야 함). 배치 파일의 이름을 지정하고(이 배치 파일 코드 주석 참고) config.xlaunch 파일이 있는 동일한 디렉토리(이 스크립트는 c:\bin\)에서 실행하세요.
@echo off
rem Works ONLY if Ethernet adapter vEthernet (WSL): is the LAST "adapter" in
rem the ipconfig output; that is, this for loop assigns the IPs from first to
rem last from the ipconfig printed output, so the WSL virtual adapter must be the
rem last one in the ipconfig output for this to work correctly.
rem If you have excellent DOS batch file skills, you may find a better
rem way of doing this.
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip==%%b
set ipAddress=%ip:~1%
rem Trim whitespace
call :TRIM %ipAddress% ipAddress
rem Use this for testing
rem echo IP Address: %ipAddress%
rem Start up the xServer using saved xlaunch configuration
rem This configuration creates a single large window
start /B config_one_window.xlaunch
rem Start up PulseAudio server, will accept audio from external
rem source, i.e., from the X server
start "" /B "C:\bin\pulseaudio-5.0-rev18\pulse\pulseaudio.exe"
rem Call ubuntu.exe, have it run what is essentially a startup
rem script that sets two ENV variables using the ipAddress variable
rem created above, which is the virtual IP to which services
rem INSIDE the running X server can connect; these are the
rem link between WSL2 and Windows 10
rem Initiate an xfce4 session with dbus-launch
rem when the X server is killed, do cleanup by
rem killing pulseadio and vcxsrv
<PATH_TO_UBUNTU_EXE>\ubuntu.exe run "export DISPLAY="%ipAddress%:0.0"; export PULSE_SERVER="%ipAddress%"; if [ -z \"$(pidof xfce4-session)\" ]; then dbus-launch --exit-with-session ~/.xsession; pkill '(gpg|ssh)-agent'; taskkill.exe /IM pulseaudio.exe /F; taskkill.exe /IM vcxsrv.exe; fi;"
rem Function to trim white space or the export VARIABLE will fail
:TRIM
SET %2=%1
GOTO :EOF