Windows10的Ubuntu20.04子系统安装GUI界面gnome并用远程连接

  • 1. 配置Windows系统的设置
  • 1.1 开发人员选项
  • 1.2 子系统选项
  • 2. WSL版本
  • 3. 配置Ubuntu20.04


1. 配置Windows系统的设置

1.1 开发人员选项

需要在设置中开启开发人员选项

ubuntu 按配置启动redis ubuntu 启动gui_ubuntu 按配置启动redis

1.2 子系统选项

ubuntu 按配置启动redis ubuntu 启动gui_windows_02

需要在控制面板去勾选使用于Linux的Windows子系统

2. WSL版本

如果对于Ubuntu子系统的GUI没有要求,那么可以跳过这一个步骤,但是如果,想要在Ubuntu子系统上搭建一套GUI界面,那么就非常有必要注意wsl的版本。

WSL目前有两个版本,如果使用Ubuntu20.04子系统,并且要实现GUI界面,那么必须将其升为WSL2。

以管理员身份打开PowerShell,执行如下的命令:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

必须保证Windows的版本在1903以上(包括1903),并且Build必须在18362以上(包括18362)。

在打开的PowerShell中执行如下命令:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

下载WSL2的内核

注意:这个下载的只适用x64机器,如果是ARM机器,就需要换一个下载源

下载的是msi文件,直接双击安装既可以。弹窗选择

设置WSL的版本为WSL2,在PowerShell中执行如下的命令:

wsl --set-default-version 2

然后就可以在微软商店安装Ubuntu20.04.


3. 配置Ubuntu20.04

在开始界面打开Ubuntu20.04,配置用户名和密码之后就开始如下的步骤:

打开Ubuntu20.04,如图所示:

ubuntu 按配置启动redis ubuntu 启动gui_linux_03

这里就可以对Ubuntu20.04执行命令行操作。首先可以换源,可以换成清华的源,其他的源可以上网搜索,自行更换即可。(换源操作很基础,这里不说明了)

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

首先执行如下的命令:

sudo apt update && sudo apt upgrade
sudo apt purge xrdp


安装gnome:

sudo apt install ubuntu-desktop gnome

安装Xrdp

sudo apt install xrdp

然后打开文件并进行更改,使用vim打开配置文件:

sudo vim /etc/xrdp/startwm.sh

在文件的最后添加如下所示的两行:

# gnome
gnome-session

添加之后的结果如下(这里因为对Windows终端进行了美化,和原生的看起来不一样):

ubuntu 按配置启动redis ubuntu 启动gui_Windows_04

然后继续执行如下命令:

sudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.bak
sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/max_bpp=32/#max_bpp=32\nmax_bpp=128/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/xserverbpp=24/#xserverbpp=24\nxserverbpp=128/g' /etc/xrdp/xrdp.ini
echo "gnome-session --session=gnome-classic" > ~/.xsession#enable dbus
sudo systemctl enable dbus
sudo /etc/init.d/dbus start
sudo /etc/init.d/xrdp start

执行这些命令之后,就可以打开Windows自带的远程控制软件,可以直接搜索找到,打开之后输入如下的代码连接:

localhost:3390

示意如下:

ubuntu 按配置启动redis ubuntu 启动gui_linux_05

注意:

如果远程连接发现直接闪退,可以打开Ubuntu20.04的终端,然后按照如下的方式进行操作:

echo "gnome-session --session=gnome-classic" > ~/.xsession sudo chown username:username .xsession # 这里的 username 更改为你的Ubuntu20.04配置的名字即可

如上的办法还是不能解决之后,在Ubuntu20.04子系统终端中执行:

gnome-session

会看到一些报错大致如下(我自己的没来及截图):

Unable to init server: Could not connect: Connection refused

(gnome-session-check-accelerated:6054): Gtk-WARNING **: 11:04:51.973: cannot open display: :0
Unable to init server: Could not connect: Connection refused

(gnome-session-check-accelerated:6055): Gtk-WARNING **: 11:04:52.234: cannot open display: :0
gnome-session-binary[6044]: WARNING: software acceleration check failed: Child process exited with code 1
gnome-session-binary[6044]: CRITICAL: We failed, but the fail whale is dead. Sorry....

那么需要通过如下方式解决:

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

然后:

wsl --shutdown

在这之后重新打开Ubuntu子系统,然后执行:

gnome-session
sudo /etc/init.d/xrdp start

即可解决问题。

之后远程就可以看到如下的画面:

ubuntu 按配置启动redis ubuntu 启动gui_ubuntu_06