遠端桌面連線時,若遠端 Ubuntu 重開機,原本的 VNC 會話會失效,需重新以 SSH 登入後手動啟動 vncserver,相當麻煩。
利用 /etc/profile.d/ 目錄下的腳本會在使用者登入 Shell 時自動執行的特性,建立自動啟動腳本。
cd /etc/profile.d/sudo vim vnc_autostart.sh#!/bin/bash # 自動啟動 VNC 第 5 號顯示器 vncserver :5
sudo chmod +x vnc_autostart.shvncserver -list 應可見 :5 會話正在運行IP:5 即可直接連上固定會話$USER,不同使用者啟動不同顯示器編號(如 :5、:6)避免衝突。/etc/systemd/system/vncserver@.service 以服務方式管理,支援開機自啟、崩潰自動重啟、日誌集中。vncpasswd),並配合 SSH 隧道或防火牆限制存取來源。/etc/profile.d/*.sh 會被 /etc/profile 在登入 Shell 時 source,故適合放置使用者層級的環境初始化指令。但僅對互動式登入生效,非登入 Shell(如 cron、systemd 服務)不會執行。