Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

VNC 遠程連線 Linux 桌面完整教學:Server 安裝、配置、密碼、防火牆、Viewer 連線

編輯:關於 Linux

VNC (Virtual Network Computing) 是跨平台遠程桌面標準協議。本文以 RHEL/CentOS/Fedora (dnf/yum) 為例,完整演示 安裝 tigervnc-server → 配置 /etc/sysconfig/vncservers → 設定 VNC 密碼 → 啟動服務 → 防火牆放行 → Windows VNC Viewer 連線 全流程,並補充 systemd 管理、多用戶、SSH 隧道加密、分辨率調整等進階主題。

yum 安裝 vnc-server

環境準備

  • Linux Server:CentOS 7 / RHEL 7 / Fedora (systemd)
  • 桌面環境:GNOME / KDE / XFCE / MATE (已安裝 @gnome-desktop 等)
  • Client:Windows / macOS / Linux 安裝 VNC Viewer (RealVNC、TigerVNC、TightVNC 均可)

步驟 1:安裝 VNC Server

# CentOS 7 / RHEL 7
sudo yum -y install tigervnc-server

# Fedora / CentOS 8+ / RHEL 8+
sudo dnf -y install tigervnc-server

# Ubuntu / Debian
sudo apt update && sudo apt install tigervnc-standalone-server

vncservers 配置檔

步驟 2:配置 VNC 服務 (SysVinit 風格配置檔,systemd 仍兼容)

sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

# 編輯 /etc/sysconfig/vncservers (舊版集中配置,仍被 service 腳本讀取)
sudo vim /etc/sysconfig/vncservers

配置內容 (針對用戶 root,顯示器 :1,解析度 1024x768)

VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1024x768 -depth 24"
  • 1:root:顯示器編號 :1 對應用戶 root
  • -geometry:桌面解析度
  • -depth 24:24 位色彩
  • 多用戶範例:VNCSERVERS="1:user1 2:user2",對應端口 5901、5902

步驟 3:設定 VNC 登入密碼 (對應用戶 root)

sudo su - root
vncpasswd
# 輸入兩次密碼 (僅前 8 字元有效)
# 會在 ~/.vnc/passwd 建立加密密碼檔

vncpasswd 設定密碼

步驟 4:啟動 VNC 服務並設定開機自啟

systemd 方式 (推薦)

# 重載 systemd
sudo systemctl daemon-reload

# 啟動顯示器 :1 (對應 root)
sudo systemctl start vncserver@:1.service

# 開機自啟
sudo systemctl enable vncserver@:1.service

# 查看狀態
sudo systemctl status vncserver@:1.service

傳統 service 方式 (舊版兼容)

sudo /etc/init.d/vncserver start
sudo chkconfig vncserver on

service vncserver start

步驟 5:防火牆放行 VNC 端口 (預設 5900 + 顯示器號)

# 顯示器 :1 → 端口 5901
# firewalld
sudo firewall-cmd --permanent --add-port=5901/tcp
sudo firewall-cmd --reload

# iptables
sudo iptables -A INPUT -p tcp --dport 5901 -j ACCEPT
sudo service iptables save

步驟 6:Windows VNC Viewer 連線

  1. 下載安裝 RealVNC Viewer 或 TigerVNC Viewer
  2. 新建連線:VNC Server: 192.168.1.100:1 (IP:顯示器號) 或 192.168.1.100:5901 (IP:端口)
  3. 輸入步驟 3 設定的密碼
  4. 享受遠程桌面!

進階主題

1. SSH 隧道加密 (強烈建議,VNC 明文傳輸不安全)

# Windows (PowerShell/OpenSSH)
ssh -L 5901:localhost:5901 user@192.168.1.100

# VNC Viewer 連線目標改為 localhost:1 或 localhost:5901

2. 多用戶獨立桌面

# /etc/sysconfig/vncservers
VNCSERVERS="1:alice 2:bob"
VNCSERVERARGS[1]="-geometry 1920x1080"
VNCSERVERARGS[2]="-geometry 1366x768"

# 各用戶分別執行 vncpasswd 設定密碼
# systemctl start vncserver@:1.service vncserver@:2.service

3. 動態調整解析度 (xrandr)

# VNC 會話內執行
xrandr -s 1920x1080
# 或啟動時指定 -geometry 1920x1080

4. systemd 直接管理 (不依賴 /etc/sysconfig/vncservers)

# /etc/systemd/system/vncserver@:1.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=forking
User=alice
Group=alice
WorkingDirectory=/home/alice
PIDFile=/home/alice/.vnc/%H%i.pid
ExecStartPre=-/usr/bin/vncserver -kill %i
ExecStart=/usr/bin/vncserver %i -geometry 1920x1080 -depth 24
ExecStop=/usr/bin/vncserver -kill %i

[Install]
WantedBy=multi-user.target

常見問題排查

現象可能原因解決
連線後灰屏/只有終端桌面環境未啟動、~/.vnc/xstartup 錯誤確保安裝桌面套件,xstartup 含 exec gnome-session 等
Authentication failed密碼錯誤、~/.vnc/passwd 權限非 600chmod 600 ~/.vnc/passwd、重新 vncpasswd
Connection refused服務未啟動、防火牆阻擋、端口號錯誤systemctl status vncserver@:1、firewall-cmd --list-ports
鍵盤/滑鼠失效客戶端/伺服器版本不匹配統一使用 TigerVNC 或 RealVNC 兩端

完成 安裝 → 配置顯示器/用戶 → 設定密碼 → systemd 啟動 → 防火牆放行 → SSH 隧道加密 → Viewer 連線 七步,即可獲得安全、流暢的 Linux 遠程桌面體驗。生產環境務必啟用 SSH 隧道或 VPN,切勿直接暴露 5900+ 端口至公網。

Copyright © Windows教程網 All Rights Reserved