Skip to content
EN

为WSL2搭建图形界面

作者:Maki 日期:2024年10月09日
CSDN链接:为WSL2配置图形界面GUI-CSDN博客

前言

这学期多了两门需要用到Linux的课程,使我有机会开始接触Linux。
手上的电脑要被我当成生产力工具,自然是不能重装为Linux了。会弄虚拟机,用的Vmware装Ubuntu,很好用。不过我是上课会带电脑去(八九斤重!),性能本续航没得说,还开个虚拟机实在是顶不住。一节课还是可以,主要是两节课我都想用电脑,接近4个小时还是不行了。
从而转去了 WSL2 ,好用,我还装了 oh-my-posh 别提上课多爽了哈哈哈。但我毕竟Linux小白,有时还是会需要图形界面,也想玩玩新的东西,于是便有了为WSL2配置图形界面的需求。网上找了教程也有些不太适用了,但还是谢谢大佬,我最后是靠AI成功配置的。

教程开始

1. 安装 VcXsrv

  • 下载并安装 VcXsrv
  • 安装完成后启动 VcXsrv,选择以下选项:
    • Multiple windows:多窗口模式
    • Fullscreen:全屏模式
    • One large window:大窗口模式
    • One window without titlebar:无标题栏窗口模式
  • 显示窗口编号设置为 -1(自动从0分配)。

2. 连接方式设置

  • 选择默认的连接方式(第一项)。

  • 勾选第三项以使用 ssh 连接 WSL2。
    最后点击“完成”即可。

3. WSL 端配置

需要获取 WSL 的 IP 地址:

  1. 打开 命令提示符(cmd),输入以下命令:

    shell
    ipconfig

    记下与 WSL 相关的网络适配器的 IPv4 地址。Ethernet adapter vEthernet (WSL) ,注意有个叫WSLCore的别看错了。

  2. 在 WSL2 中设置显示环境变量。打开 WSL(Ubuntu),执行以下命令:

    bash
    export DISPLAY=[HOST]:[DISPLAY NUMBER]

    这里的 [HOST] 是你刚才记录的 IP 地址,[DISPLAY NUMBER] 通常设置为 0。

    比如,如果你的 IP 地址是 192.168.1.2,则命令为:

    bash
    export DISPLAY=192.168.1.2:0
  3. 刷新环境变量:

    bash
    source ~/.bashrc

4. WSL2 安装桌面环境(Ubuntu)

  1. 安装 GNOME 桌面环境
    在 WSL 的 Ubuntu 中,运行以下命令:

    bash
    sudo apt-get update
    sudo apt-get install ubuntu-desktop
  2. 安装相关工具

    bash
    sudo apt-get install gnome-tweak-tool
  3. 安装 systemctl
    由于 WSL 默认不支持 systemctl,需要手动安装。执行以下命令:

    bash
    git clone https://github.com/DamionGans/ubuntu-wsl2-systemd-script.git
    cd ubuntu-wsl2-systemd-script/
    bash ubuntu-wsl2-systemd-script.sh --force

    注意:如果出现杀毒软件警告,请允许程序所有操作或暂时关闭杀毒软件。

  4. 重启 WSL 服务
    打开管理员模式的 PowerShell,执行:

    powershell
    net restart LxssManager

  1. 安装远程控制软件 xrd
    在 WSL 的 Ubuntu 中,执行以下命令安装 xrdp:
bash
sudo apt-get install xrdp
  1. 修改 xrdp 端口

将 xrdp 的默认端口从 3389 改为 3390,因为 3389 已被保留。执行:

bash
sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini
  1. 配置启动 session

为了确保远程桌面登录后能够正确启动 GNOME 桌面环境,执行:

bash
echo "gnome-session" > ~/.xsession
  1. 启动 xrdp 服务

执行以下命令以重新启动 xrdp 服务:

bash
sudo service xrdp start
  1. 连接远程桌面

  2. 打开 Windows 开始菜单,搜索 "远程桌面连接"(mstsc)。

  3. 在计算机字段中输入 localhost:3390,然后点击“连接”。

  4. 输入你的 Ubuntu 用户名和密码,点击“确定”。

到这就是前大佬分享的教程,我在黑屏后几乎半秒内闪退,无法进入图形界面。、
查询日志文件:

xrdp-sesman.log

[20241019-19:35:53] [INFO ] sesman_main_loop: sesman asked to terminate
[20241019-19:35:57] [INFO ] starting xrdp-sesman with pid 25235
[20241019-19:36:04] [INFO ] Socket 11: AF_INET6 connection received from ::1 port 38354
[20241019-19:36:04] [INFO ] Terminal Server Users group is disabled, allowing authentication
[20241019-19:36:04] [INFO ] ++ created session (access granted): username maki, ip ::1:58496 - socket: 11
[20241019-19:36:04] [INFO ] starting Xorg session...
[20241019-19:36:04] [INFO ] Starting session: session_pid 25263, display :10.0, width 1280, height 720, bpp 24, client ip ::1:58496 - socket: 11, user name maki
[20241019-19:36:04] [INFO ] [session start] (display 10): calling auth_start_session from pid 25263
[20241019-19:36:04] [ERROR] sesman_data_in: scp_process_msg failed
[20241019-19:36:04] [ERROR] sesman_main_loop: trans_check_wait_objs failed, removing trans
[20241019-19:36:04] [INFO ] Starting X server on display 10: /usr/lib/xorg/Xorg :10 -auth .Xauthority -config xrdp/xorg.conf -noreset -nolisten tcp -logfile .xorgxrdp.%s.log
[20241019-19:36:04] [INFO ] Found X server running at /tmp/.X11-unix/X10
[20241019-19:36:04] [INFO ] Found X server running at /tmp/.X11-unix/X10
[20241019-19:36:04] [INFO ] Found X server running at /tmp/.X11-unix/X10
[20241019-19:36:04] [INFO ] Session started successfully for user maki on display 10
[20241019-19:36:04] [INFO ] Starting the xrdp channel server for display 10
[20241019-19:36:04] [INFO ] Starting the default window manager on display 10: /etc/xrdp/startwm.sh
[20241019-19:36:04] [INFO ] Session in progress on display 10, waiting until the window manager (pid 25264) exits to end the session
[20241019-19:36:05] [WARN ] Window manager (pid 25264, display 10) exited with non-zero exit code 255 and signal 5. This could indicate a window manager config problem
[20241019-19:36:05] [WARN ] Window manager (pid 25264, display 10) exited quickly (1 secs). This could indicate a window manager config problem
[20241019-19:36:05] [INFO ] Calling auth_stop_session and auth_end from pid 25263
[20241019-19:36:05] [INFO ] Terminating X server (pid 25265) on display 10
[20241019-19:36:05] [INFO ] Terminating the xrdp channel server (pid 25268) on display 10
[20241019-19:36:06] [INFO ] X server on display 10 (pid 25265) returned exit code 0 and signal number 0
[20241019-19:36:06] [INFO ] xrdp channel server for display 10 (pid 25268) exit code 0 and signal number 0
[20241019-19:36:06] [INFO ] cleanup_sockets:
[20241019-19:36:06] [INFO ] Process 25263 has exited
[20241019-19:36:06] [INFO ] ++ terminated session:  username maki, display :10.0, session_pid 25263, ip ::1:58496 - socket: 11

xrdp.log

plaintext
[20241019-19:35:53] [INFO ] Received termination signal, stopping the server accept new connections thread
[20241019-19:35:57] [INFO ] address [0.0.0.0] port [3390] mode 1
[20241019-19:35:57] [INFO ] listening to port 3390 on 0.0.0.0
[20241019-19:35:57] [INFO ] xrdp_listen_pp done
[20241019-19:35:59] [INFO ] starting xrdp with pid 25238
[20241019-19:35:59] [INFO ] address [0.0.0.0] port [3390] mode 1
[20241019-19:35:59] [INFO ] listening to port 3390 on 0.0.0.0
[20241019-19:35:59] [INFO ] xrdp_listen_pp done
[20241019-19:36:01] [INFO ] Socket 11: AF_INET6 connection received from ::1 port 58494
[20241019-19:36:01] [INFO ] Using default X.509 certificate: /etc/xrdp/cert.pem
[20241019-19:36:01] [INFO ] Using default X.509 key file: /etc/xrdp/key.pem
[20241019-19:36:01] [ERROR] Cannot read private key file /etc/xrdp/key.pem: Permission denied
[20241019-19:36:01] [WARN ] Cannot accept TLS connections because certificate or private key file is not readable. certificate file: [/etc/xrdp/cert.pem], private key file: [/etc/xrdp/key.pem]
[20241019-19:36:01] [INFO ] Security protocol: configured [RDP], requested [SSL|HYBRID|HYBRID_EX|RDP], selected [RDP]
[20241019-19:36:01] [ERROR] libxrdp_force_read: header read error
[20241019-19:36:01] [ERROR] Processing [ITU-T T.125] Connect-Initial failed
[20241019-19:36:01] [ERROR] [MCS Connection Sequence] receive connection request failed
[20241019-19:36:01] [ERROR] xrdp_sec_incoming: xrdp_mcs_incoming failed
[20241019-19:36:01] [ERROR] xrdp_rdp_incoming: xrdp_sec_incoming failed
[20241019-19:36:01] [ERROR] xrdp_process_main_loop: libxrdp_process_incoming failed
[20241019-19:36:01] [ERROR] xrdp_iso_send: trans_write_copy_s failed
[20241019-19:36:01] [ERROR] Sending [ITU T.125] DisconnectProviderUltimatum failed
[20241019-19:36:01] [INFO ] Socket 11: AF_INET6 connection received from ::1 port 58496
[20241019-19:36:01] [INFO ] Using default X.509 certificate: /etc/xrdp/cert.pem
[20241019-19:36:01] [INFO ] Using default X.509 key file: /etc/xrdp/key.pem
[20241019-19:36:01] [ERROR] Cannot read private key file /etc/xrdp/key.pem: Permission denied
[20241019-19:36:01] [WARN ] Cannot accept TLS connections because certificate or private key file is not readable. certificate file: [/etc/xrdp/cert.pem], private key file: [/etc/xrdp/key.pem]
[20241019-19:36:01] [INFO ] Security protocol: configured [RDP], requested [RDP], selected [RDP]
[20241019-19:36:01] [INFO ] Connected client computer name: DESKTOP-6DAKL5P
[20241019-19:36:01] [WARN ] Received [MS-RDPBCGR] TS_UD_HEADER type 0xc006 is unknown (ignored)
[20241019-19:36:01] [WARN ] Received [MS-RDPBCGR] TS_UD_HEADER type 0xc00a is unknown (ignored)
[20241019-19:36:01] [INFO ] xrdp_load_keyboard_layout: Keyboard information sent by the RDP client, keyboard_type:[0x07], keyboard_subtype:[0x00], keylayout:[0x00000804]
[20241019-19:36:01] [INFO ] xrdp_load_keyboard_layout: model [] variant [] layout [us] options []
[20241019-19:36:01] [INFO ] Non-TLS connection established from ::1 port 58496: with security level : high
[20241019-19:36:01] [INFO ] xrdp_caps_process_pointer: client supports new(color) cursor
[20241019-19:36:01] [INFO ] xrdp_process_offscreen_bmpcache: support level 1 cache size 10485760 MB cache entries 100
[20241019-19:36:01] [INFO ] xrdp_caps_process_codecs: nscodec, codec id 1, properties len 3
[20241019-19:36:01] [WARN ] xrdp_caps_process_codecs: unknown codec id 5
[20241019-19:36:01] [WARN ] Cannot find keymap file /etc/xrdp/km-00000804.ini
[20241019-19:36:01] [WARN ] Cannot find keymap file /etc/xrdp/km-00000804.ini
[20241019-19:36:01] [INFO ] Loading keymap file /etc/xrdp/km-00000409.ini
[20241019-19:36:01] [WARN ] local keymap file for 0x00000804 found and doesn't match built in keymap, using local keymap file
[20241019-19:36:04] [INFO ] connecting to sesman on 127.0.0.1:3350
[20241019-19:36:04] [INFO ] xrdp_wm_log_msg: sesman connect ok
[20241019-19:36:04] [INFO ] sesman connect ok
[20241019-19:36:04] [INFO ] sending login info to session manager. Please wait...
[20241019-19:36:04] [INFO ] xrdp_wm_log_msg: login successful for user maki on display 10
[20241019-19:36:04] [INFO ] login successful for user maki on display 10
[20241019-19:36:04] [INFO ] loaded module 'libxup.so' ok, interface size 10296, version 4
[20241019-19:36:04] [INFO ] started connecting
[20241019-19:36:04] [INFO ] lib_mod_connect: connecting via UNIX socket
[20241019-19:36:04] [INFO ] lib_mod_log_peer: xrdp_pid=25262 connected to X11rdp_pid=25265 X11rdp_uid=1000 X11rdp_gid=1000 client_ip=::1 client_port=58496
[20241019-19:36:04] [INFO ] connected ok

补充步骤

随后询求AI帮助,通过以下步骤成功进入图形界面。

  1. 确认已经安装桌面环境
sudo apt-get install xfce4
  1. 更新startwm.sh,路径/etc/xrdp/startwm.sh
#!/bin/sh
# startxfce4
startxfce4


执行权限

sudo chmod +x /etc/xrdp/startwm.sh
  1. 修改.xsession文件
echo "startxfce4" > ~/.xsession
  1. 更改 key.pemcert.pem 文件的权限
sudo chmod 600 /etc/xrdp/key.pem
sudo chmod 644 /etc/xrdp/cert.pem
  1. 重启xrdp
sudo service xrdp restart

最后再返回mstsc远程,成功进入桌面~

如果不会AI我还真的无法配置成功,感谢科技发展。