WSL2 中的网络子系统与 WSL1 中使用的不同。您必须consider the differences才能访问在 Windows 和 Linux 上运行的网络应用程序:
- 在 WSL1 中,Linux 使用与 Windows 主机相同的 IP 地址,然后,您可以使用
localhost
或 127.0.0.1
访问应用程序
- 在 WSL2 中,Linux 在轻量级虚拟机上运行,并具有不同的 IP 地址。要访问在 Windows 主机上运行的网络应用,您必须使用 Windows IP 地址。
检查Windows主机的IP地址
有多种方法可以确定 Windows 主机中的 IP 地址。您可以在 WSL Linux 中运行以下命令:
-
cat /etc/resolv.conf
显示了 Windows 中 eth0
接口的 IP 地址
-
ipconfig.exe
显示 Windows 主机中的所有 IP 配置
-
route.exe print
显示了 Windows 主机中的网络路由配置
为 WSL2 设置 DISPLAY 变量
根据 Microsoft 文档,您可以设置 DISPLAY 变量,检查 nameserver
文件中的 /etc/resolv.conf
。 (@fqquiner 和 @VPraharsha 已经提到了这一点)
export DISPLAY=$(grep nameserver /etc/resolv.conf | awk '{print $2}'):0.0
但是,我在使用此解决方案时遇到了问题,可能是因为我使用带有 WiFi 连接和多个虚拟网络的笔记本电脑。我没有使用之前的解决方案,而是使用 route.exe
并检查默认网关中使用的接口来确定 Windows IP 地址。
export DISPLAY=$(route.exe print | grep 0.0.0.0 | head -1 | awk '{print $4}'):0.0
在 .profile
中设置 DISPLAY 变量
您可以在 ~/.profile
文件中设置 DISPLAY 变量。我使用了以下代码:
# set DISPLAY to use X terminal in WSL
# in WSL2 the localhost and network interfaces are not the same than windows
if grep -q WSL2 /proc/version; then
# execute route.exe in the windows to determine its IP address
DISPLAY=$(route.exe print | grep 0.0.0.0 | head -1 | awk '{print $4}'):0.0
else
# In WSL1 the DISPLAY can be the localhost address
if grep -q icrosoft /proc/version; then
DISPLAY=127.0.0.1:0.0
fi
fi
,
遇到了同样的问题,因此我尝试了其他建议,但最终起作用的是允许vcxsrv通过公共防火墙。我知道您没有使用vcxsrv,但也许对您来说也是同样的问题。
安装VcXsrv,然后启用如下图所示的公共防火墙。在命令提示符下使用wf.msc打开具有高级安全性的Windows Defender防火墙。然后允许这些图片中的连接。
[
然后针对Windows 10 WSL2运行VcXsrv from this guide
通过添加-ac添加参数来运行VcXsrv或在命令提示符下键入此参数
“ C:\ Program Files \ VcXsrv \ vcxsrv.exe”::0 -multiwindow-剪贴板-wgl -ac
然后将其输入到您的WSL2终端
export DISPLAY_NUMBER="0.0"
export DISPLAY=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'):$DISPLAY_NUMBER
export LIBGL_ALWAYS_INDIRECT=1
# OPTIONAL Set the keyboard layout to US
setxkbmap -layout us
setsid emacs
exit
,
在fquinner的回答中,
您的 DISPLAY 环境变量应设置为export DISPLAY=X.X.X.X:0
以使用Windows主机的IP地址作为WSL2,并且Windows主机不在同一网络设备中,其中XXXX是IP地址
,您的IP地址在名称服务器($ cat /etc/resolv.conf
)的 resolv.conf 中列出
或简单地export DISPLAY="`grep nameserver /etc/resolv.conf | sed 's/nameserver //'`:0"
自动加载正确的IP地址。此外,您可以将其添加到.bashrc
或.zshrc
(如果使用Zsh)
,
这里有一个故障排除部分,用于在wsl2上调试X11:
https://github.com/cascadium/wsl-windows-toolbar-launcher/blob/master/README.md#troubleshooting
端口转发与WSL1不同-您的Linux服务可以通过Windows的本地主机访问,但反之不再成立。
因此,您需要使用Windows主机的内部IP并调整防火墙以允许WSL网络通过。
,
2021 TL;DR 答案
此处的所有(当前)其他答案都是旧的且已不复存在。截至 2021 年 4 月 27 日,它正常工作 TM。
来自https://docs.microsoft.com/en-us/windows/wsl/tutorials/gui-apps
先决条件:
- 确保使用 Windows 10 Insider build 21362+
- 为 vGPU 安装驱动程序:Intel AMD NVIDIA
新的 wsl
实例将“正常工作”。现有系统需要“更新”:
- 在管理 PowerShell 中:
wsl --update
-
wsl --shutdown
强制重启 WSL
不要忘记删除您可能对 DISPLAY
所做的任何其他修改。
证明有效:
,
发现现在为了使GUI正常工作(至少在联机时),您需要使用公共IP ...
例如运行以下命令应该可以工作:
export DISPLAY=$(dig +short myip.opendns.com @resolver1.opendns.com):0
,
这是我写的一篇文章,介绍了如何设置Ubuntu桌面:
How to Install Ubuntu Desktop with a Graphical User Interface in WSL2
本文介绍了如何安装vcxsrv,dotNet,genie和Ubuntu桌面。它涵盖了创建用于导出环境变量的脚本,启动vcxsrv,启动gnome桌面环境以及创建将它们联系在一起的快捷方式的脚本。它还涵盖了运行Ubuntu桌面,防止屏幕锁定错误以及安装Snap Store的问题。
@karel和@Zanna要求的说明:
链接的文章包括原始格式和屏幕截图
下载VcXsrv:
访问官方网站
点击“下载”
安装VcXsrv:
打开“ vcxsrv-64.1.20.8.1.installer.exe”
点击下一步”
点击“安装”
点击“关闭”
允许访问VcXsrv:
选中“专用网络”
点击“允许访问”
打开PowerShell:
按“⊞Windows”
在搜索栏中输入“ PowerShell”
右键单击“ Windows PowerShell”
点击“以管理员身份运行”
打开WSL2:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
wsl
安装Ubuntu桌面:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
sudo apt --yes install ubuntu-desktop
设置用户名变量:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
username=$(wslvar USERNAME)
创建Ubuntu目录:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
mkdir --parents /mnt/c/users/$username/.ubuntu/
打开Ubuntu目录:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
cd /mnt/c/users/$username/.ubuntu
下载适用于Microsoft产品的Linux软件存储库:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
Ubuntu 20.04:
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb --output-document packages-microsoft-prod.deb
Ubuntu 18.04:
wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb --output-document packages-microsoft-prod.deb
为Microsoft产品安装Linux软件存储库:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
sudo dpkg --install packages-microsoft-prod.deb
更新存储库:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
sudo apt update
为HTTPS安装APT传输:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
sudo apt install --yes apt-transport-https
更新存储库:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
sudo apt update
安装.Net:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
sudo apt install --yes dotnet-sdk-5.0
将Arkane Systems添加到源列表目录:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
sudo sh -c 'echo "deb [trusted=yes] https://wsl-translinux.arkane-systems.net/apt/ /" > /etc/apt/sources.list.d/wsl-translinux.list'
更新存储库:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
sudo apt update
安装精灵:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
sudo apt install --yes systemd-genie
创建Sudoers文件:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
echo "$USER ALL=(ALL) NOPASSWD:/usr/bin/genie" | sudo EDITOR="tee" visudo --file /etc/sudoers.d/$USER
创建桌面脚本:
从以下说明中复制代码
将代码粘贴到PowerShell中
按“ Enter”
# CREATE BASH SCRIPT
# Store block of text with here document
create_bash_script=$(cat << end_of_string
# Define necessary environment variables
export DISPLAY="\$(cat /etc/resolv.conf | grep nameserver | awk '{ print \$2 }'):1.0"
export DESKTOP_SESSION="ubuntu"
export GDMSESSION="ubuntu"
export XDG_SESSION_DESKTOP="ubuntu"
export XDG_CURRENT_DESKTOP="ubuntu:GNOME"
export XDG_SESSION_TYPE="x11"
export XDG_BACKEND="x11"
export XDG_SESSION_CLASS="user"
export XDG_DATA_DIRS="/usr/local/share/:/usr/share/:/var/lib/snapd/desktop"
export XDG_CONFIG_DIRS="/etc/xdg"
export XDG_RUNTIME_DIR="\$HOME/xdg"
export XDG_CONFIG_HOME="\$HOME/.config"
export XDG_DATA_HOME="\$HOME/.local/share"
export XDG_CACHE_HOME="\$HOME/.cache"
export XDG_DESKTOP_DIR="\$HOME/Desktop"
export XDG_DOCUMENTS_DIR="\$HOME/Documents"
export XDG_DOWNLOAD_DIR="\$HOME/Downloads"
export XDG_MUSIC_DIR="\$HOME/Music"
export XDG_PICTURES_DIR="\$HOME/Pictures"
export XDG_PUBLICSHARE_DIR="\$HOME/Public"
export XDG_TEMPLATES_DIR="\$HOME/Templates"
export XDG_VIDEOS_DIR="\$HOME/Videos"
# Start desktop environment
gnome-session
end_of_string
)
# Store username environment variable in lowercase
username=$(wslvar USERNAME | awk '{ print tolower($0) }') &&
# Save block of text in bash file
echo "${create_bash_script}" > "/mnt/c/users/$username/.ubuntu/02_start_desktop.sh"
下载快捷方式图像:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
wget https://assets.ubuntu.com/v1/9fbc8a44-circle-of-friends-web.zip
解压缩快捷方式图像:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
unzip -o 9fbc8a44-circle-of-friends-web.zip
创建快捷方式图标:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
convert -resize 64x64 ./circle-of-friends-web/png/cof_orange_hex.png ubuntu.ico
退出WSL2:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
exit
创建VcXsrv脚本:
从以下说明中复制代码
将代码粘贴到PowerShell中
按“ Enter”
# RELOAD VCXSRV SCRIPT
# Store username environment variable in lowercase
$username = $env:username.tolower()
# Store block of text with here-string
$reload_vcxsrv_script = @"
# Stop vcxsrv proccess that contains "1.0" in the program window title
get-process vcxsrv | where { `$_.mainwindowtitle -like "*1.0*" } | stop-process
# Start vcxsrv process in a large program window on display number one
start-process "c:\program files\vcxsrv\vcxsrv.exe" -argument ":1 -ac -nowgl -multimonitors -dpms"
"@
# Save block of text in powershell file
echo "${reload_vcxsrv_script}" > $env:userprofile/.ubuntu/reload_vcxsrv.ps1
创建Ubuntu脚本:
从以下说明中复制代码
将代码粘贴到PowerShell中
按“ Enter”
# CREATE VISUAL BASIC SCRIPT
# Store username environment variable in lowercase
$username = $env:username.tolower()
# Store block of text with here-string
$create_vbs_script = @"
' Run PowerShell script in background
set application = createobject("shell.application")
application.shellexecute "powershell","-file c:\users\admin\.ubuntu\01_reload_vcxsrv.ps1","",0
' Allow PowerShell script to complete
wscript.sleep 3000
' Run Bash script in background
set shell= createobject("wscript.shell")
shell.run "wsl sudo genie -c bash /mnt/c/users/admin/.ubuntu/02_start_desktop.sh",0
"@
# Save block of text in bash file
echo "${create_vbs_script}" > $env:userprofile/.ubuntu/03_start_ubuntu.vbs
创建快捷方式脚本:
从以下说明中复制代码
将代码粘贴到PowerShell中
按“ Enter”
# Store block of text with here-string
$create_shortcut_script = @"
# Define location variables
`$shortcut_location = "`$env:userprofile\.ubuntu\Ubuntu.lnk"
`$program_location = "`$env:userprofile\.ubuntu\03_start_ubuntu.vbs"
# Create shortcut
`$object = new-object -comobject wscript.shell
`$shortcut = `$object.createshortcut(`$shortcut_location)
`$shortcut.targetpath = `$program_location
`$shortcut.iconlocation = "`$env:userprofile\.ubuntu\ubuntu.ico"
`$shortcut.save()
"@
# Save block of text in powershell file
echo $create_shortcut_script > $env:userprofile/.ubuntu/04_create_shortcut.ps1
打开Ubuntu目录:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
cd c:\users\admin\.ubuntu
创建快捷方式:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
powershell.exe -file .\04_create_shortcut.ps1
在资源管理器中打开目录:
从这些说明下面复制命令
将命令粘贴到PowerShell中
按“ Enter”
explorer .
启动Ubuntu桌面:
双击“ Ubuntu”快捷方式
打开终端:
点击左上角的“活动”
在搜索栏中输入“终端”
点击“终端”
禁用屏幕锁定:
从这些说明下面复制命令
将命令粘贴到终端
按“ Enter”
gsettings set org.gnome.desktop.screensaver lock-enabled false
安装快照存储:
从这些说明下面复制命令
将命令粘贴到终端
按“ Enter”
sudo snap install snap-store
,
简答:
- 安装 xming 服务器
- 在 WSL 上运行 export DISPLAY=localhost:0.0