最近我得在 Linux Desktop 環境中使用 GUI 應用程式。由於平常主要使用 MacOS,因此需要一套遠端桌面工具,讓我可以從 Mac 連線到 Linux。
有一段時間沒有使用 Linux Desktop,我發現現在主流的 KDE Plasma 與 GNOME 桌面環境已經逐漸從 X11 轉向 Wayland,內建的遠端桌面功能與以前的使用方式也有所不同。
以我實際嘗試的環境來說,Desktop Sharing 需要使用既有的桌面工作階段;螢幕鎖定或進入螢幕保護狀態時,也遇到遠端連線無法繼續使用的問題。至於 Desktop Login,我曾嘗試設定,但花了一些時間仍未成功。除此之外,我對 RDP 遠端桌面的畫面呈現與操作體驗也不太滿意。
後來我想到以前曾經使用過 Sunshine + Moonlight 遊玩遊戲,當時的畫面與延遲表現都很好。既然這套軟體原本就能用來串流高效能的遊戲畫面,或許也適合用來操作一般的 Linux 桌面應用程式。
實際測試後,這套組合確實提供了很好的操作體驗。因此,我把目前成功的設定與自動化流程整理下來,方便日後重新安裝或部署時參考。
[!NOTE]
Sunshine 是免費、開源的遠端串流伺服器,搭配 Moonlight 客戶端,可以將遠端電腦的畫面與操作串流到本地端。它原本常用於遊戲串流,但也能用來操作一般桌面應用程式。
580.178.04)在 Nobara 安裝 KRFB:
sudo dnf install krfb
確認工具可用:
# 使用 krfb-virtualmonitor 工具時必須要有存在的 Wayland display 並且設定正確的環境變數
# 在 Desktop 環境中使用通常會有正確的環境變數
# ssh 環境可能就需要手動設定 WAYLAND_DISPLAY 變數
# export WAYLAND_DISPLAY=wayland-0
krfb-virtualmonitor --help
Usage: krfb-virtualmonitor [options]
Offer a Virtual Monitor that can be accessed remotely
Options:
-h, --help Displays help on commandline options.
--help-all Displays help, including generic Qt options.
-v, --version Displays version information.
--author Show author information.
--license Show license information.
--desktopfile <file name> The base file name of the desktop entry for this
application.
--resolution <resolution> Logical resolution of the new monitor
--name <name> Name of the monitor
--password <password> Password for the client to connect to it
--scale <dpr> The device-pixel-ratio of the device, the scaling
factor
--port <number> The port we will be listening to
我原本懷疑 Proxmox VE 提供的 KVM 虛擬顯示裝置可能會影響遠端桌面的使用體驗,因此嘗試建立 KDE Virtual Monitor,讓 Sunshine 擷取這個虛擬螢幕,而不是使用原本的 KVM 虛擬顯示裝置。
經過實際測試,使用 Virtual Monitor 時,Moonlight 的操作體感比較流暢,glmark2-wayland 的測試分數也比較高。雖然目前還不能確定差異的根本原因,但對我的使用情境來說,採用 Virtual Monitor 是比較好的選擇。
使用以下指令建立 Virtual Monitor
krfb-virtualmonitor --resolution 1920x1080 --name Sunshine --scale 1
建立後,會出現名為 Virtual-Sunshine 的 output。
查看螢幕輸出:
kscreen-doctor -o
Output: 1 Virtual-Sunshine 3b564df2-606c-4d55-aa9d-4e5ce3d6f539
enabled
connected
priority 1
Unknown
replication source:0
Modes: 1:1920x1080@60.00*!
Custom modes: None
Geometry: 1360,0 1920x1080
Scale: 1
Rotation: 1
Overscan: 0
Vrr: incapable
RgbRange: unknown
HDR: incapable
Wide Color Gamut: incapable
ICC profile: incapable
Color profile source: incapable
Brightness control: unsupported
Color resolution: unknown
Allow EDR: unsupported
Sharpness control: unsupported
Automatic brightness: unsupported
Auto Rotate Policy: incapable
Adaptive backlight modulation: unsupported
Output: 2 Virtual-1 22607ad6-02c2-476c-832a-728056a5f4a1
disabled
connected
priority 2
Unknown
replication source:0
Modes: 2:1280x800@74.99! 3:5120x2160@50.00 4:4096x2160@50.00 5:3840x2160@60.00 6:3840x2160@59.94 7:3840x2160@50.00 8:1920x1440@60.00 9:2560x1080@50.00 10:1856x1392@59.99 11:1792x1344@60.00 12:2048x1152@60.00 13:1920x1200@59.88 14:1920x1080@60.00* 15:1920x1080@50.00 16:1600x1200@60.00 17:1680x1050@59.95 18:1400x1050@59.98 19:1280x1024@60.02 20:1440x900@59.89 21:1280x960@60.00 22:1360x768@60.01 23:1280x768@59.87 24:1024x768@60.00 25:800x600@60.32 26:640x480@60.00 27:640x480@59.94
Custom modes: None
Geometry: 0,0 1920x1080
Scale: 1
Rotation: 1
Overscan: 0
Vrr: incapable
RgbRange: unknown
HDR: incapable
Wide Color Gamut: incapable
ICC profile: none
Color profile source: sRGB
Color power preference: prefer efficiency and performance
Brightness control: supported, set to 100% and dimming to 100%
Color resolution: unknown
Allow EDR: unsupported
Sharpness control: unsupported
Automatic brightness: unsupported
Auto Rotate Policy: incapable
Adaptive backlight modulation: unsupported
可以看到建立了一個虛擬螢幕為 Virtual-Sunshine,解析度 1920×1080;原本的 Proxmox KVM 顯示器則是另一個 Output Virtual-1。
安裝:
sudo dnf install sunshine
啟動(使用當前 User 不使用 root):
systemctl --user enable --now app-dev.lizardbyte.app.Sunshine.service
Sunshine 使用 systemd user service,因此要在正確的 Desktop 環境的使用者 session 中操作。
在 Sunshine 本機可開啟:
https://localhost:47990
從另一台電腦管理時,使用 Nobara 主機的 IP 位址與連接埠 47990。
曾遇到 CSRF Protection Error。若遇到類似問題,可以檢查 ~/.config/sunshine/sunshine.conf 的 csrf_allowed_origins 設定,將實際使用的 Web UI 來源加入允許清單。例如:
csrf_allowed_origins = https://192.168.1.67:47990
請將範例 IP 換成自己實際使用的位址,修改後重新啟動 Sunshine。
[!NOTE]
⚠️第一次登錄要設定帳號密碼
目前我修改的核心設定是:
kwin or portal
Virtual-Sunshine
在我的 Wayland 環境中,Sunshine 可以選擇 KWin Screencast 或 XDG Portal 等擷取方式。目前使用的設定是 capture = kwin,並指定 output_name = Virtual-Sunshine。這是目前實際測試成功的組合,尚未完整比較不同擷取方式的效能。
可以透過 Web UI 設定或直接設定到檔案內:
$ cat ~/.config/sunshine/sunshine.conf
capture = kwin
csrf_allowed_origins = https://192.168.1.67:47990
output_name = Virtual-Sunshine
從 Moonlight Qt 的 GitHub Releases 下載最新版本的安裝檔。
成功連線後,之後就不需要輸入 Pin code 了。
實際使用結果:
這個時候 Nobara Linux 上有兩個螢幕,一個是 KVM 虛擬的 Display Virtual-1 另一個是 Virtual-Sunshine,這個環境有些不好用,所以要把 Virtual-1 關掉。
可以使用 KDE 上的 Display 設定工具關閉這個螢幕,或使用 Command Line:
kscreen-doctor output.2.disable
由於不想要每次登入 KDE 都做這麼多繁瑣的操作,所以需要做自動化設定。
手動操作時,先建立 Virtual-Sunshine,再用 kscreen-doctor 停用原本的 Proxmox/QEMU 顯示器,很麻煩。為了不想每次登入後手動處理的步驟,目前使用 KDE 自動啟動腳本:
建立一個 Script 放在任意位置:
~/.local/bin/sunshine-monitor.sh
設定為可執行檔:
chmod +x ~/.local/bin/sunshine-monitor.sh
腳本內容:
#!/usr/bin/env bash
# 此腳本是針對目前只有一個原始 KVM Display 的環境設計
krfb-virtualmonitor --resolution 1920x1080 --name Sunshine --scale 1 &
sleep 3
k_display_no=$(kscreen-doctor -o | grep Output | grep Sunshine | awk '{print $2}' | tr -cd 0-9)
kscreen-doctor output.$k_display_no.enable
k_display_no=$(kscreen-doctor -o | grep Output | grep -v Sunshine | awk '{print $2}' | tr -cd 0-9)
kscreen-doctor output.$k_display_no.disable
systemctl --user restart app-dev.lizardbyte.app.Sunshine.service
流程是:
不想做太複雜,目前測試這個 Script 可以讓 Sunshine 正常運作。
有了腳本操作還需要 KDE 登錄後自動執行這個腳本
建立一個 KDE 登錄後自動執行的檔案(檔案權限為 600)
~/.config/autostart/sunshine-monitor.sh.desktop
內容是
[Desktop Entry]
Exec=/home/jack/.local/bin/sunshine-monitor.sh
Icon=application-x-executable-script
Name=sunshine-monitor.sh
Type=Application
X-KDE-AutostartScript=true
這樣設定好之後在登入 KDE 的時候就會執行 sunshine-monitor.sh
Sunshine + Moonlight 延遲低、畫面漂亮,實際使用 Moonlight 顯示幾乎像是在 MacOS 上跑的應用程式一樣,有這樣的遠端工具,我可以很舒服的在遠端桌面上使用圖形化的應用程式。