VNC 全名是 Virtual Network Computing,它讓 Linux 也能像 Windows 的遠端桌面一樣,直接看到並操作圖形介面。本文的實作環境是興寧市網路信息中心的一台 CentOS 7 HP 伺服器。
先確認系統有沒有裝 VNC:
[code][root@wic ~]# rpm -q tigervnc tigervnc-server
沒裝的話會看到:
[code]package tigervnc is not installed package tigervnc-server is not installed
如果連 X Window 桌面都沒有,得先補上:
[code][root@wic ~]# yum check-update [root@wic ~]# yum groupinstall "X Window System" [root@wic ~]# yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts [root@wic ~]# unlink /etc/systemd/system/default.target [root@wic ~]# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target [root@wic ~]# reboot
第一步:安裝 VNC 套件
[code][root@wic ~]# yum install tigervnc-server -y
第二步:建立並修改設定檔
把範本從 /lib/systemd/system/vncserver@.service 複製到 /etc/systemd/system/ 下並命名為 vncserver@:1.service:
[code][root@wic ~]# cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
打開該檔,找到這一行並替換預設使用者名稱:
[code]ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i" PIDFile=/home/<USER>/.vnc/%H%i.pid
本文直接用 root 登入,所以改成:
[code]ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i" PIDFile=/root/.vnc/%H%i.pid
若是一般使用者(例如 linoxide)則寫成:
[code]ExecStart=/sbin/runuser -l linoxide -c "/usr/bin/vncserver %i" PIDFile=/home/linoxide/.vnc/%H%i.pid
第三步:重新載入 systemd
[code][root@wic ~]# systemctl daemon-reload
第四步:設定 VNC 密碼
[code][root@wic ~]# vncpasswd
第五步:放行防火牆
這台 CentOS 7 用的是 iptables,編輯 vim /etc/sysconfig/iptables,在合適位置加上:
[code]-A INPUT -m state --state NEW -m tcp -p tcp --dport 5900:5903 -j ACCEPT
重啟防火牆:
[code]service iptables restart
若使用 CentOS 7 預設的 firewalld,則改下:
[code][root@wic ~]# firewall-cmd --permanent --add-service vnc-server [root@wic ~]# systemctl restart firewalld.service
仍然不通時,可以暫時關閉防火牆測試:
[code]systemctl stop firewalld.service systemctl disable firewalld.service
第六步:設為開機啟動並啟動服務
[code][root@wic ~]# systemctl enable vncserver@:1.service [root@wic ~]# systemctl start vncserver@:1.service
Server 端到此完成,Windows 端只要裝一個 VNC Viewer 就能連上,畫面樸素但確實可用。

