VNC (Virtual Network Computing) 為 Linux 服務器提供圖形化遠程桌面能力。生產級部署需解決 **systemd 多用戶會話管理、桌面環境 (GNOME/KDE/Xfce) 正確啟動、SSH 隧道加密傳輸、防火牆/SELinux 策略** 等工程化問題。本文提供 CentOS 7/8、Ubuntu 20.04+ 通用的標準化配置流程。
用戶端 (VNC Viewer)
│
▼ (TCP 5900+Display)
SSH 隧道 (本地端口轉發) ← 強烈建議,加密 + 繞過防火牆
│
▼
服務器 :1 (TCP 5901) → Xvnc (TigerVNC) → 桌面環境 (gnome-session/startkde/startxfce4)
│
▼
systemd 管理 vncserver@:1.service (用戶級實例)
# CentOS/RHEL 7/8
yum install -y tigervnc-server
# Ubuntu/Debian
apt update && apt install -y tigervnc-standalone-server
# 驗證
vncserver -list
su - user1
vncpasswd
# 輸入 6-8 位密碼 (僅前 8 位有效),確認
# 生成 ~/.vnc/passwd (權限 600)
exit
~/.vnc/xstartup
su - user1
cat > ~/.vnc/xstartup <<'EOF'
#!/bin/sh
# 標準桌面啟動序列
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec /etc/X11/xinit/xinitrc
# 顯式指定桌面環境 (二選一,註釋另一個)
# GNOME
exec gnome-session
# KDE Plasma
# exec startkde
# Xfce (輕量推薦)
# exec startxfce4
EOF
chmod +x ~/.vnc/xstartup
exit
💡 關鍵點:unset SESSION_MANAGER 避免衝突;exec 替換 Shell 進程,使 VNC 直接監控桌面進程存活。
# 複製模板並重命名 (顯示器 :1 對應端口 5901)
cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
# 編輯服務文件
vi /etc/systemd/system/vncserver@:1.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
User=user1 # 具體用戶
Group=user1
WorkingDirectory=/home/user1
# 啟動前清理舊鎖文件
ExecStartPre=-/usr/bin/vncserver -kill %i
# 啟動參數:幾何分辨率、色深、僅本地監聽 (配合 SSH 隧道)
ExecStart=/usr/bin/vncserver %i -geometry 1920x1080 -depth 24 -localhost
# 停止
ExecStop=/usr/bin/vncserver -kill %i
# 資源限制
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
⚠️ 關鍵參數:-localhost 僅綁定 127.0.0.1,**強制走 SSH 隧道**,拒絕公網直連;%i 自動展開為 :1。
systemctl daemon-reload
systemctl enable --now vncserver@:1.service
systemctl status vncserver@:1.service
# 查看端口
ss -tlnp | grep 5901
# 127.0.0.1:5901
# 建立隧道 (本地 5901 → 服務器 127.0.0.1:5901)
ssh -L 5901:localhost:5901 user1@server_ip -N -f
# 連接 VNC
vncviewer localhost:5901
# 或 remmina、vinagre 等 GUI 客戶端填 localhost:5901
# 用戶 user2 顯示器 :2 (端口 5902)
cp /etc/systemd/system/vncserver@:1.service /etc/systemd/system/vncserver@:2.service
sed -i 's/User=user1/User=user2/g; s/Group=user1/Group=user2/g' /etc/systemd/system/vncserver@:2.service
# user2 執行 vncpasswd、配置 xstartup
systemctl enable --now vncserver@:2.service
# 統一管理腳本
cat > /usr/local/bin/vnc-user-add <<'EOF'
#!/bin/bash
USER=$1
DISPLAY_NUM=$2
[ -z "$USER" -o -z "$DISPLAY_NUM" ] && { echo "Usage: $0 "; exit 1; }
cp /etc/systemd/system/vncserver@:1.service /etc/systemd/system/vncserver@:${DISPLAY_NUM}.service
sed -i "s/User=user1/User=${USER}/g; s/Group=user1/Group=${USER}/g" /etc/systemd/system/vncserver@:${DISPLAY_NUM}.service
systemctl daemon-reload
systemctl enable --now vncserver@:${DISPLAY_NUM}.service
echo "VNC for $USER on :${DISPLAY_NUM} (port $((5900+DISPLAY_NUM))) ready. User must run vncpasswd and setup xstartup."
EOF
chmod +x /usr/local/bin/vnc-user-add
| 桌面 | 啟動命令 | 套件名 | 記憶體佔用 | 推薦場景 |
|---|---|---|---|---|
| GNOME 3/40+ | gnome-session | gnome-shell / @gnome-desktop | 高 (1.5GB+) | 功能全、Wayland 原生、觸控 |
| KDE Plasma 5 | startkde | plasma-workspace / @kde-desktop | 中 (800MB+) | 高度可定制、傳統範式 |
| Xfce 4 | startxfce4 | xfce4-session / @xfce-desktop | 低 (300MB+) | 輕量、穩定、遠程流暢 |
| MATE | mate-session | mate-session-manager / @mate-desktop | 低 | GNOME 2 風格 |
| LXQt | startlxqt | lxqt-session / @lxqt-desktop | 極低 (200MB+) | 極限資源受限 |
💡 遠程桌面首選 Xfce:低延遲、低帶寬、穩定性最佳。
| 現象 | 可能原因 | 排查命令/修復 |
|---|---|---|
| 灰屏/只有終端 | xstartup 未啟動桌面會話 | 檢查 ~/.vnc/xstartup 含 exec gnome-session 且可執行 |
| 連接後立即斷開 | 桌面進程啟動失敗/SELinux 阻擋 | journalctl -u vncserver@:1 -f、ausearch -m avc -ts recent |
| 無法輸入中文 | 缺輸入法模組/環境變量 | 安裝 ibus-libpinyin,xstartup 加 export GTK_IM_MODULE=ibus QT_IM_MODULE=ibus XMODIFIERS=@im=ibus |
| 剪貼板不共享 | vncconfig 未運行 | xstartup 加 vncconfig -nowin & (TigerVNC 自帶) |
| 分辨率固定/無法調整 | 啟動參數 -geometry 鎖定 | 移除 -geometry 或使用 xrandr (需桌面支援) |
| 防火牆阻擋 | 未放行 5901 或 SSH 端口 | firewall-cmd --add-port=5901/tcp --permanent && firewall-cmd --reload (直連模式) |
-localhost 參數 + 防火牆**不開放** 5900-5999vncserver -securitytypes TLSVnc + 憑證配置vncpasswd -f systemd 實例化服務 + xstartup 顯式桌面啟動 + SSH 隧道強制加密 + Xfce 輕量桌面 = 生產級、可審計、低延遲的 Linux 遠程桌面基石。
按此標準部署,讓無頭服務器擁有「如本地般流暢」的圖形運維體驗。