2025年5月1日 星期四

使用 Systemd 啟動 VNC server 的設定過程

之前在啟動 VNC 伺服器時,多使用『CLI, 命令列介面』的方式來啟動(參考:開啟個人化的 VNC 服務,供「遠端桌面」連線),在新版的作業系統開始使用 Systemd 後,TigerVNC 也逐漸轉移至 Systemd。不過,在過渡期間是兩者皆可使用,各有其優劣點:
啟動方式優點缺點
命令列介面, CLI1. 未啟動時,不佔用系統資源。
2. 使用者可自行啟動。
每次使用前需要確認已啟動,
若常用它的話有些不方便。
Systemd開機時,即自動啟動。1. 啟動後,即佔用系統資源。
2. 多個使用者時,會佔用可觀資源。
在過渡時期還可以『命令列介面』啟動時的訊息如下:
[root@EeePC ~]# vncserver :2266 -geometry 1920x1004 -screen scrn -name [user @EeePC]

WARNING: vncserver has been replaced by a systemd unit and is about to be removed in the next Fedora release.
Please read /usr/share/doc/tigervnc/HOWTO.md for more information.

New '[user @EeePC]' desktop is EeePC:2266

Starting applications specified in /home/user/.vnc/xstartup
Creating default config /home/user/.vnc/config
Starting applications specified in /home/user/.vnc/xstartup
Log file is /home/user/.vnc/EeePC:2266.log
但是會跳出一個訊息,提醒您使用 Systemd 開啟 VNC。被提醒久了想說就研究一下使用 Systemd 啟動 VNC 的設定,系統的發展一定有其必要原因,早晚要轉換就趁早,免得某次更新後就出問題。

1.VNC 伺服器的基本設定

以下操作都需先切換成 root 身份,請參考 "在 Linux 中,切換為 root 身分以操作、設定系統" 一文。假設 VNC 伺服器的設定參數為:使用者名稱為 SiB、連接埠為 8166,
[root@EeePC ~]# vncserver :2266 -geometry 1920x1004 -screen scrn -name [SiB @EeePC]

WARNING: vncserver has been replaced by a systemd unit and is about to be removed in the next Fedora release.
Please read /usr/share/doc/tigervnc/HOWTO.md for more information.

New '[SiB @EeePC]' desktop is SiB:2266

Starting applications specified in /home/cmchang/.vnc/xstartup
Log file is /home/cmchang/.vnc/EeePC:2266.log
其中 WARNING 起頭那一列文字就是提醒『改用 Systemd 取代 VNCserver 來啟動 VNC 伺服器。

2. 設定 VNC 伺服器使用 SystemD 的設定

同前例,假設 VNC 伺服器的設定參數為:使用者名稱為 SiB、連接埠為 8166,開啟 /etc/tigervnc/vncserver.users,編輯使用者設定檔,
[root@EeePC ~]# vim /etc/tigervnc/vncserver.users

[root@EeePC ~]# cat /etc/tigervnc/vncserver.users
# TigerVNC User assignment
#
# This file assigns users to specific VNC display numbers.
# The syntax is <display>=<username>. E.g.:
#
# :2=andrew
# :3=lisa
:2266=SiB
第二個指令是顯示編輯後的設定檔內容。其中,
  • :2266 是連接埠 8166 減去神秘數字 5900 後的結果。
  • SiB 是使用者名稱。
接著,開啟 /etc/tigervnc/vncserver-config-defaults,編輯編輯 VNC 伺服器的預設屬性,所有使用者適用。
[root@EeePC ~]# vim /etc/tigervnc/vncserver-config-defaults

[root@EeePC ~]# cat /etc/tigervnc/vncserver-config-defaults
## Default settings for VNC servers started by the vncserver service
#
# Any settings given here will override the builtin defaults, but can
# also be overriden by ~/.vnc/config and vncserver-config-mandatory.
#
# See HOWTO.md and the following manpages for more details:
# vncsession(8) Xvnc(1)
#
# Several common settings are shown below. Uncomment and modify to your
# liking.

# session=gnome
# securitytypes=vncauth,tlsvnc
# geometry=2000x1200
# localhost
# alwaysshared
session=mate
第二個指令是顯示編輯後的設定檔內,其中
# session=gnome預設為 GNOME「桌面環境」,
欲設為 MATE 桌面則改為 session=mate。
# securitytypes=vncauth,tlsvncVNC 預設加密演算法
# geometry=2000x1200VNC 桌面的預設大小
# localhost設定為使用內部連接埠,不可由外部連入。
# alwaysshared可同時接受多個 VNC 客戶端的連接

設定完成,用這個指令開啟 VNC 伺服器,
[root@EeePC ~]# systemctl start vncserver@\:2266.service
若要在每次開機時自動啟動 VNC 伺服器,則使用這個指令
[root@EeePC ~]# systemctl enable vncserver@\:2266.service
萬一需要重啟 VNC 伺服器,則使用這個指令
[root@EeePC ~]# systemctl restart vncserver@\:2266.service

已測試版本:

  • Fedora: 37 ~ 40, TigerVNC

參考資料:

沒有留言:

張貼留言

感謝你耐心看完本文,歡迎留下任何指正、建議,筆者會儘快回應。(English is also welcome.)