雖然微軟在 BUILD 2020 上已經宣佈,未來會讓 WSL2 可以執行 GUI 應用程式

事前準備

首先,對於一個習慣 Ubuntu 的我來說,如果可以體驗到完整的 Ubuntu 桌面是再好不過了,換句話說,我們需要在上面可以跑一個完整的 Gnome Shell 環境,並加上 Ubuntu 的 Extension 們。

取得完整 systemd 環境

Gnome Shell 從 3.34 版開始,就已經跟 systemd 整合

也因此,在 WSL2 的系統裡面,其實是沒有啓用 systemd 的,我們可以簡單的透過下面的方式來檢查看看:

$ systemctl

System has not been booted with systemd as init system (PID 1). Can't operate.

Failed to connect to bus: Host is down

$ ps u -q 1

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1 0.0 0.0 908 592 ? Sl 10:31 0:00 /init

可以很明顯的看到,當我們執行 systemctl 的時候,會顯示出我們的 init system (PID 1) 並非 systemd,而是微軟提供的 /init。

那我們如果想要擁有一個 systemd 環境的話,該怎麼辦呢?

由於 systemd 必須以 PID 1 的方式執行,所以直接執行 systemd 是沒有用的,但多虧了 Linux Namespace 我們可以在 WSL2 中建立新的 Namespace 並把 systemd 作為 PID 1 來執行,也就是在 WSL2 中再多加一層 PID Namespace,使得我們可以建築一個 systemd 的環境並跳進這個新的 Namespace 中。

所幸,我們不需要自己來研究這部分該如何操作,GitHub 上已經有幾個專案可以直接拿來參考並使用:

$ git clone https://github.com/DamionGans/ubuntu-wsl2-systemd-script.git

Cloning into 'ubuntu-wsl2-systemd-script'...

remote: Enumerating objects: 76, done.

remote: Counting objects: 100% (76/76), done.

remote: Compressing objects: 100% (55/55), done.

remote: Total 76 (delta 40), reused 41 (delta 21), pack-reused 0

Unpacking objects: 100% (76/76), 19.46 KiB | 996.00 KiB/s, done.

$ cd ubuntu-wsl2-systemd-script/

ubuntu-wsl2-systemd-script $ bash ubuntu-wsl2-systemd-script.sh

[sudo] password for davy:

Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease

Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease

Hit:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease

Hit:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease

Reading package lists... Done

(Reading database ... 31836 files and directories currently installed.)

Preparing to unpack .../0-dbus-user-session_1.12.16-2ubuntu2.1_amd64.deb ...

Unpacking dbus-user-session (1.12.16-2ubuntu2.1) over (1.12.16-2ubuntu2) ...

Preparing to unpack .../1-dbus-x11_1.12.16-2ubuntu2.1_amd64.deb ...

Unpacking dbus-x11 (1.12.16-2ubuntu2.1) over (1.12.16-2ubuntu2) ...

Preparing to unpack .../2-dbus_1.12.16-2ubuntu2.1_amd64.deb ...

Unpacking dbus (1.12.16-2ubuntu2.1) over (1.12.16-2ubuntu2) ...

Preparing to unpack .../3-libdbus-1-3_1.12.16-2ubuntu2.1_amd64.deb ...

Unpacking libdbus-1-3:amd64 (1.12.16-2ubuntu2.1) over (1.12.16-2ubuntu2) ...

Selecting previously unselected package daemonize.

Preparing to unpack .../4-daemonize_1.7.8-1_amd64.deb ...

Unpacking daemonize (1.7.8-1) ...

Selecting previously unselected package fontconfig.

Preparing to unpack .../5-fontconfig_2.13.1-2ubuntu3_amd64.deb ...

Unpacking fontconfig (2.13.1-2ubuntu3) ...

Setting up fontconfig (2.13.1-2ubuntu3) ...

Regenerating fonts cache... done.

Setting up libdbus-1-3:amd64 (1.12.16-2ubuntu2.1) ...

Setting up dbus (1.12.16-2ubuntu2.1) ...

Setting up daemonize (1.7.8-1) ...

Setting up dbus-x11 (1.12.16-2ubuntu2.1) ...

Setting up dbus-user-session (1.12.16-2ubuntu2.1) ...

Processing triggers for systemd (245.4-4ubuntu3) ...

Processing triggers for man-db (2.9.1-1) ...

Processing triggers for libc-bin (2.31-0ubuntu9) ...

rm: cannot remove '/lib/systemd/system/sysinit.target.wants/proc-sys-fs-binfmt_misc.mount': No such file or directory

'\\wsl$\Ubuntu-20.04\home\davy\ubuntu-wsl2-systemd-script'

是目前用來啟動 CMD.EXE 的目錄路徑。不支援 UNC 路徑。

預設目錄是 Windows 目錄。

成功: 已經儲存指定的值。

'\\wsl$\Ubuntu-20.04\home\davy\ubuntu-wsl2-systemd-script'

是目前用來啟動 CMD.EXE 的目錄路徑。不支援 UNC 路徑。

預設目錄是 Windows 目錄。

成功: 已經儲存指定的值。

ubuntu-wsl2-systemd-script $

安裝完畢後我們需要重啓整個 WSL2,假定我們的 WSL 名稱是 ubuntu-20.04,在命令提示字元(CMD)中執行下列指令以關閉 WSL2:

> wsl.exe -t ubuntu-20.04

接者使用一般使用者啓動 WSL2,就可以發現有不一樣的地方了(多了一個啓動 systemd 的提示),透過檢查 PID 1 也可以發現整個環境已經是由 systemd 掌握了:

Sleeping for 1 second to let systemd settle

Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.19.104-microsoft-standard x86_64)

$ ps u -q 1

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1 2.8 0.0 175224 12920 ? Ss 21:48 0:06 /lib/systemd/systemd --system-unit=basic.target

$

移除用不到的 snap(可選)

由於 Ubuntu 安裝 systemd 時,會連 snap 也一併啓用,如果大家用不到的話可以把 snap 從系統中移除,這麼一來也可以提升啓動速度,我們可以看到系統啓動時也連著一些 snap 的元件一起啓動了(而且還不少 Processes):

$ ps aux

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1 1.3 0.0 108788 11360 ? Ss 22:03 0:00 /lib/systemd/systemd --system-unit=basic.targ

root 43 2.1 0.1 45248 14600 ? S

root 61 0.4 0.0 21592 8496 ? Ss 22:03 0:00 /lib/systemd/systemd-udevd

systemd+ 63 1.2 0.0 18548 7896 ? Ss 22:03 0:00 /lib/systemd/systemd-networkd

root 152 0.0 0.0 10572 4588 pts/0 S 22:03 0:00 /bin/login -p -f 'HOSTTYPE=x86_64' 'PWD=

root 219 1.2 0.0 3608 1792 ? Ss 22:03 0:00 snapfuse /var/lib/snapd/snaps/core18_1705.sna

root 220 0.3 0.0 3660 1476 ? Ss 22:03 0:00 snapfuse /var/lib/snapd/snaps/lxd_14804.snap

root 221 0.1 0.0 3488 1536 ? Ss 22:03 0:00 snapfuse /var/lib/snapd/snaps/snapd_7264.snap

systemd+ 228 1.1 0.0 24116 12592 ? Ss 22:03 0:00 /lib/systemd/systemd-resolved

root 231 0.1 0.0 241020 9280 ? Ssl 22:03 0:00 /usr/lib/accountsservice/accounts-daemon

message+ 232 0.2 0.0 7428 4640 ? Ss 22:03 0:00 /usr/bin/dbus-daemon --system --address=syste

root 235 0.3 0.1 29216 17788 ? Ss 22:03 0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher

syslog 236 0.1 0.0 224328 4296 ? Ssl 22:03 0:00 /usr/sbin/rsyslogd -n -iNONE

root 238 2.1 0.2 1457156 35432 ? Ssl 22:03 0:00 /usr/lib/snapd/snapd

root 240 1.1 0.0 16852 7728 ? Ss 22:03 0:00 /lib/systemd/systemd-logind

root 261 0.0 0.0 236408 9084 ? Ssl 22:03 0:00 /usr/lib/policykit-1/polkitd --no-debug

root 302 0.0 0.0 8540 2764 ? Ss 22:03 0:00 /usr/sbin/cron -f

root 307 0.4 0.1 108036 20512 ? Ssl 22:03 0:00 /usr/bin/python3 /usr/share/unattended-upgrad

daemon 308 0.0 0.0 3796 2200 ? Ss 22:03 0:00 /usr/sbin/atd -f

root 318 0.0 0.0 7356 2172 tty1 Ss+ 22:03 0:00 /sbin/agetty -o -p -- \u --noclear --keep-bau

root 326 0.0 0.0 5832 1744 ? Ss 22:03 0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux

davy 376 0.2 0.0 18444 9612 ? Ss 22:03 0:00 /lib/systemd/systemd --user

davy 377 0.0 0.0 110132 3188 ? S 22:03 0:00 (sd-pam)

davy 387 0.2 0.0 10048 4992 pts/0 S 22:03 0:00 -bash

davy 431 0.0 0.0 10604 3224 pts/0 R+ 22:04 0:00 ps aux

$

首先我們先將所有 snap 都列出後,一個一個移除:

$ snap list

Name Version Rev Tracking Publisher Notes

core18 20200311 1705 latest/stable canonical✓ base

lxd 4.0.1 14804 latest/stable/… canonical✓ -

snapd 2.44.3 7264 latest/stable canonical✓ snapd

# snap remove lxd

# snap remove core18

# snap remove snapd

$ snap list

No snaps are installed yet. Try 'snap install hello-world'.

# apt purge snapd

$ ps aux

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1 0.6 0.0 109692 12692 ? Ss 22:03 0:03 /lib/systemd/systemd --system-unit=basic.targ

root 43 0.1 0.1 53444 15456 ? S

root 61 0.0 0.0 21592 8496 ? Ss 22:03 0:00 /lib/systemd/systemd-udevd

systemd+ 63 0.1 0.0 18548 7896 ? Ss 22:03 0:00 /lib/systemd/systemd-networkd

root 152 0.0 0.0 10572 4588 pts/0 S 22:03 0:00 /bin/login -p -f 'HOSTTYPE=x86_64' 'PWD=

systemd+ 228 0.0 0.0 24116 12592 ? Ss 22:03 0:00 /lib/systemd/systemd-resolved

root 231 0.0 0.0 241020 9280 ? Ssl 22:03 0:00 /usr/lib/accountsservice/accounts-daemon

message+ 232 0.0 0.0 7428 4640 ? Ss 22:03 0:00 /usr/bin/dbus-daemon --system --address=syste

root 235 0.0 0.1 29216 17788 ? Ss 22:03 0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher

syslog 236 0.0 0.0 224328 4296 ? Ssl 22:03 0:00 /usr/sbin/rsyslogd -n -iNONE

root 240 0.1 0.0 16852 7728 ? Ss 22:03 0:00 /lib/systemd/systemd-logind

root 261 0.0 0.0 236408 9084 ? Ssl 22:03 0:00 /usr/lib/policykit-1/polkitd --no-debug

root 302 0.0 0.0 8540 2764 ? Ss 22:03 0:00 /usr/sbin/cron -f

root 307 0.0 0.1 108036 20512 ? Ssl 22:03 0:00 /usr/bin/python3 /usr/share/unattended-upgrad

daemon 308 0.0 0.0 3796 2200 ? Ss 22:03 0:00 /usr/sbin/atd -f

root 318 0.0 0.0 7356 2172 tty1 Ss+ 22:03 0:00 /sbin/agetty -o -p -- \u --noclear --keep-bau

root 326 0.0 0.0 5832 1744 ? Ss 22:03 0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux

davy 376 0.0 0.0 18444 9708 ? Ss 22:03 0:00 /lib/systemd/systemd --user

davy 377 0.0 0.0 110132 3188 ? S 22:03 0:00 (sd-pam)

davy 387 0.0 0.0 10180 5252 pts/0 S 22:03 0:00 -bash

root 1168 0.0 0.1 283780 15888 ? Ssl 22:07 0:00 /usr/lib/packagekit/packagekitd

davy 1273 0.0 0.0 10604 3316 pts/0 R+ 22:11 0:00 ps aux

結束後我們就可以發現 Processes 數量減少了許多,讓我們的環境又稍微輕量了些。

在 Windows 準備 X Window Server

這邊有很多選擇,我選擇了 X410

安裝 Ubuntu 桌面

接著我們就可以來安裝 Ubuntu 的預設桌面了,這裡會需要比較多的硬碟空間:

# apt install ubuntu-desktop

...

10 upgraded, 1077 newly installed, 0 to remove and 64 not upgraded.

Need to get 605 MB of archives.

After this operation, 2260 MB of additional disk space will be used.

Do you want to continue? [Y/n] y

...

安裝結束後,我們需要先取得 Windows 的 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 172.17.160.1

$ nc -v 172.17.160.1 6000

Connection to 172.17.160.1 6000 port [tcp/x11] succeeded!

^C

$

成功連線後,我們可以撰寫一個啓動腳本,將 DISPLAY 指向 Windows 上的 X Window Server 中並且加上一些 Ubuntu 桌面的設定後執行 Gnome Shell:

$ cat - > gnome.sh <

#!/bin/bash

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

export XDG_SESSION_TYPE="x11"

export XDG_RUNTIME_DIR=~/.cache/xdg

export XDG_SESSION_CLASS="user"

export XDG_SESSION_DESKTOP=ubuntu

export XDG_CURRENT_DESKTOP=ubuntu:GNOME

export DESKTOP_SESSION=ubuntu

export GDMSESSION=ubuntu

export GNOME_SHELL_SESSION_MODE=ubuntu

gnome-session "[email protected]"

EOF

$ chmod +x gnome.sh

$ ./gnome.sh

當然,如果想要在這個桌面中執行 Windows CMD 也是沒有問題的,而系統也可以正確的偵測到 Virtualization 是 WSL 呢。

關閉 systemd-resolved

這裡是一個可選的項目,由於筆者在透過 /etc/resolv.conf 取得 Windows IP 時有遇到 resolv.conf 被 systemd-resolved 替換成 127.0.0.53 的問題,這邊提供一個方式將這個東西停用,操作完重啓 WSL2 即可:

# systemctl stop systemd-resolved

# systemctl disable systemd-resolved

Removed /etc/systemd/system/multi-user.target.wants/systemd-resolved.service.

Removed /etc/systemd/system/dbus-org.freedesktop.resolve1.service.

後記

由於目前的 WSL2 還沒有 GPGPU 加速,所以拿來執行 GUI 程式的話可能還是會有點 lag,微軟提出的最終方案是透過 RDP 來執行 GUI 應用程式也許會透過 RemoteApp 來將顯卡加速做在 Windows 端,但作為嚐鮮一下,目前的結果已經算是還可以的了XD

wsl2启动桌面_在 WSL2 中使用 Ubuntu 桌面環境相关推荐

  1. windows虚拟桌面_在Windows中使用虚拟桌面的最佳免费程序

    windows虚拟桌面 If you often open a lot of applications at once, a virtual desktop program can help you ...

  2. linux桌面_使用 KDE Plasma 定制 Linux 桌面 | Linux 中国

    本文是 24 天 Linux 桌面特别系列的一部分.如果你认为没有太多自定义桌面的需要,KDE Plasma 可能适合你.-- Seth Kenlon KDE 社区的 Plasma 桌面是开源桌面环境 ...

  3. linux ubuntu桌面进程,如何加快你的Ubuntu桌面性能

    Ubuntu是非常强大和精致的基于Debian的Linux发行版. 尽管Ubuntu桌面性能在现代笔记本电脑和台式机上非常好,速度也非常快. 但是在安装Ubuntu之后的几天,您可能已经注意到,您的系 ...

  4. linux桌面添加软件快捷方式,如何在 Ubuntu 桌面手动添加应用快捷方式

    原标题:如何在 Ubuntu 桌面手动添加应用快捷方式 在这篇快速指南中,你将学到如何在 Ubuntu 桌面和其他使用 GNOME 桌面的发行版中添加应用图标. -- Abhishek Prakash ...

  5. linux桌面环境丢失,一个pycharm引起ubuntu桌面丢失的惨案

    原标题:一个pycharm引起ubuntu桌面丢失的惨案 俗话说 no zuo ,no die, 我用python写一个数据转换程序,可以正常运行,但是有几个小问题,我要用debug看一下 一调试才发 ...

  6. 自定义linux桌面,教您6个自定义Ubuntu桌面的步骤

    对于那些想要易于使用的界面的人来说,Ubuntu是一个不错的Linux发行版,可以说是Linux新手的最佳Linux发行版. 但是,这会带来一些副作用,即对于界面和外观,Canonical为用户做了很 ...

  7. unpn 远程桌面_在路由器中开启UPNP有什么具体作用,有什么坏处

    如题,只说好处坏处,喜欢去到处复制的人少去做无用功,不用去解释UPNP是什么意思,对于一般的家庭笔记本局域网,开启路由器的此功能有何好坏处,对路由器的稳定性有何影响? 一.路由器中开启UPNP的作用: ...

  8. 开机后黑屏看不到桌面_电脑开机后不显示桌面图标怎么回事 电脑开机后不显示桌面图标解决办法大全!...

    现在电脑的使用率非常高,随着信息化时代的到来,电脑早已是生活不可或缺的一部分.但是电脑在使用过程中有些时候容易出现故障.那么,若碰上 电脑开机 后只显示桌面的背景而没有任何程序和应用的情况,该如何应对 ...

  9. outlook 日历 桌面_将Microsoft Outlook固定到桌面背景

    outlook 日历 桌面 Have you ever wanted to just "stick" your Outlook calendar to the desktop? F ...

最新文章

  1. 清华集训2014 day2 task1 简单回路
  2. 小马哥spring编程核心思想_Spring源码高级笔记之——Spring核心思想
  3. Elasticsearch之mapping映射入门
  4. SQL Server 查询处理中的各个阶段(SQL执行顺序)
  5. java中的类型擦除type erasure
  6. 【12图】你管这破玩意叫Pulsar
  7. IdentityServer4学习笔记汇总(实现传送门在底部)
  8. ubuntu系统写路由指令_在Ubuntu中如何查看网络路由表详解,
  9. WCF与ASMX Web服务差异比较[译]
  10. Julia: reduce 、mapreduce、filter
  11. 请同学检查地址是否有误:CSDN博客地址汇总(网络121、网络131、计科1111-1114)
  12. 华硕Z99jc安装win7出现安装程序无法定位现有系统分区,也无法创建新的系统分区解决办法...
  13. 为什么我玩游戏那么卡?
  14. 服装企业的ERP实施是有条件的
  15. 会计专业计算机工具,会计工作需要用哪些工具
  16. 自动阅读项目到底能不能做?看成本分析再决定
  17. 基金的估值原来这么简单,一文看懂
  18. Lizcst Software Lab 正式入驻新华军软件商城——华商团!
  19. Temporary failure in name resolution错误解决方法
  20. 第 1 份工作,我只干了 2 周就被辞退了 | 十年系列

热门文章

  1. 蓝桥杯真题31日冲刺国一 | 每日题解报告 第五天
  2. 音域测试软件 电脑,音域太窄怎么办?教你这两个小方法,瞬间就能提升好几个档次!...
  3. RuntimeError: Cannot re-initialize CUDA in forked subprocess
  4. CSS 字体拉伸 font-stretch属性
  5. Python 实现文本转语音
  6. 花朵数c语言算法,【菜鸟求助】21位数的花朵数问题,求解题思路
  7. 基于matlab/simulink环境下空调房间仿真模型的建立,基于Matlab_Simulink环境下空调房间仿真模型的建立.pdf...
  8. Substance Designer+Unity HDRP
  9. 东风本田crv2020新款混动说明书_2019东风本田crv锐混动 续航能力超强
  10. 8086CPU中指令RET与IRET区别