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

CentOS 5/6/7 完整安裝配置 VNC Server 教學(含防火牆、多用戶、系統服務)

編輯:Linux 教學

環境確認

rpm -q vnc vnc-server
# 未安裝則:yum install tigervnc-server -y  (CentOS 7)
# 或:yum install vnc-server -y  (CentOS 5/6)

配置 VNC 服務(多用戶、多桌面)

編輯 /etc/sysconfig/vncservers:

VNCSERVERS="1:user1 2:user2 3:root"
VNCSERVERARGS[1]="-geometry 1024x768 -depth 24"
VNCSERVERARGS[2]="-geometry 1280x800 -depth 24"
VNCSERVERARGS[3]="-geometry 800x600 -depth 16"
  • 顯示編號 :1 對應 TCP port 5901,:2 → 5902,以此類推。
  • HTTP/Java 瀏覽器存取 port 為 5800 + 顯示編號(如 5801)。

設定各用戶 VNC 密碼

以該用戶身分執行(非 root):


vncpasswd
# 輸入兩次密碼(僅前 8 字元有效)

密碼檔儲存在 ~/.vnc/passwd(權限 600)。

啟動與設定開機自啟


# CentOS 5/6 (SysVinit)
service vncserver start
chkconfig vncserver on

# CentOS 7+ (systemd)
# 需為每個顯示編號建立服務單元
cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
# 編輯服務檔,設定 User=user1
systemctl daemon-reload
systemctl enable vncserver@:1.service
systemctl start vncserver@:1.service

自訂桌面環境(編輯 xstartup)

編輯 ~/.vnc/xstartup:

#!/bin/sh
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &

# 選擇一種桌面環境啟動
gnome-session &      # GNOME
# startkde &         # KDE
# xfce4-session &    # XFCE
# twm &              # 極簡視窗管理器

修改後重啟服務:service vncserver restart。

防火牆開放連接埠


# iptables (CentOS 5/6)
iptables -A INPUT -p tcp --dport 5901:5903 -j ACCEPT
iptables -A INPUT -p tcp --dport 5801:5803 -j ACCEPT
service iptables save

# firewalld (CentOS 7+)
firewall-cmd --permanent --add-port=5901-5903/tcp
firewall-cmd --permanent --add-port=5801-5803/tcp
firewall-cmd --reload

Windows 客戶端連線

  • VNC Viewer (RealVNC/TightVNC): 輸入 IP:顯示編號,如 192.168.1.100:1。
  • 瀏覽器 (Java) : 開啟 https://IP:5801。

常見問題排除

  • Connection refused (10061): 防火牆未開放 port、服務未啟動、或 VNCSERVERS 格式錯誤。
  • 灰色畫面/只有終端機: xstartup 未正確啟動桌面環境,檢查 gnome-session 等指令是否存在。
  • Root 登入失敗: 預設限制 root 遠程圖形登入,建議使用一般用戶再 su - 或 sudo。
Copyright © Windows教程網 All Rights Reserved