︿
Top

1、前言

VNC (Virtual Network Computing)簡單說就是遠端連線了,因為有時會需要用到 X-Win (ex. 查看 UPS 設定) 又不想到主機前去使用因此就裝 VNC Server 來方便我連到 CentOS 去,開始玩吧。




文章目錄

1、前言
2、實作環境
3、安裝及設定
          步驟1.修改 vncserver 設定檔
          步驟2.啟動 VNC Service
          步驟3.設定開機自動啟動 vncserver 服務
4、補充:IPTable 設定
5、參考



2、實作環境

  • CentOS 5.1 (Linux 2.6.18-53.el5)
  • vnc-server-4.1.2-9.el5



3、安裝及設定

步驟1.修改 vncserver 設定檔

在修改前我們先確定系統是否有安裝 VNC Server 套件。
rpm -q vnc vnc-server
 vnc-server-4.1.2-9.el5

修改 vnc server 設定檔,只加入如下一行即可而 100 的意思為屆時連接 VNC Service 時的 Port (5900 + 100),所以設定 100 到時連接 VNC Server 就使用 :6000
vi /etc/sysconfig/vncservers
 #VNCSERVERS="2:myusername"                //預設值
 #VNCSERVERARGS2="-geometry 800x600 -nolisten tcp -nohttpd -localhost"
 VNCSERVERS="100:weithenn"                //新增此行
 VNCSERVERARGS100="-geometry 1024x768"    //指定屆時連接到 Xwindows 的解析度




步驟2.啟動 VNC Service

再啟動 VNC Service 以前我們先設定屆時連接 VNC Server 的 Password,記得密碼必須超過 6 個字元否則會出現 Password must be at least 6 characters - try again 訊息請您加強密碼強度,當 VNC Password 設定後會自動建立密碼檔我的例子就會建在 /home/user/weithenn/.vnc/passwd。
vncpasswd
 Password:   //輸入 VNC 密碼
 Verify:     //再輸入一次 VNC 密碼以確認

設定完密碼後啟動 VNC Service 吧。
/etc/rc.d/init.d/vncserver start
 Starting VNC server: 100:weithenn xauth:  creating new authority file /home/user/weithenn/.Xauthority
 New 'centos-jiakai.weithenn.idv.tw:100 (weithenn)' desktop is centos-jiakai.weithenn.idv.tw:100
 Creating default startup script /home/user/weithenn/.vnc/xstartup
 Starting applications specified in /home/user/weithenn/.vnc/xstartup
 Log file is /home/user/weithenn/.vnc/centos-jiakai.weithenn.idv.tw:100.log
                                                                  OK  

Windows 當 VNC Client 的話就下載 RealVNC 吧,安裝完畢後輸入要連接的 VNC Server 及剛才設定的 Port :6000,若正確的話就會跳出密碼認證視窗。


但此時應該是看到如下畫面醜醜的畫面 (twm window manager)。


所以我們透過修改 ~/.vnc/xstartup 來讓連接時變成你所安裝的 X-Win (Gnome or KDE)。
vi ~/.vnc/xstartup
 # Uncomment the following two lines for normal desktop:
 unset SESSION_MANAGER            //將此行註解拿掉
 exec /etc/X11/xinit/xinitrc      //將此行註解拿掉

修改後記得重新啟動 VNC Service 才會載入新的設定。
/etc/rc.d/init.d/vncserver restart
 Shutting down VNC server: 100:weithenn                      [OK]
 Starting VNC server: 100:weithenn
 New 'centos-jiakai.weithenn.idv.tw:100 (weithenn)' desktop is centos-jiakai.weithenn.idv.tw:100
 Starting applications specified in /home/user/weithenn/.vnc/xstartup
 Log file is /home/user/weithenn/.vnc/centos-jiakai.weithenn.idv.tw:100.log
                                                              [OK]  

檢查相關 Listen Port 是否正確執行。
netstat -tnl
 Active Internet connections (only servers)
 Proto  Recv-Q  Send-Q   Local Address   Foreign Address     State
 tcp         0       0   0.0.0.0:6000    0.0.0.0:*           LISTEN

檢查相關 Process 是否正確執行。
ps ax |grep Xvnc
 20116 ?        S      0:00 Xvnc :100 -desktop centos-test.weithenn.idv.tw:100 (weithenn) -httpd /usr/share/vnc/classes -auth /home/user/weithenn/.Xauthority -geometry 1024x768 -depth 16 -rfbwait 30000 -rfbauth /home/user/weithenn/.vnc/passwd -rfbport 6000 -pn

此時我們在連到 VNC Server 應該會看到你所安裝的 X-Win。

Gnome

KDE



步驟3.設定開機自動啟動 vncserver 服務

使用 chkconfig 指令來查看 vncserver 在各 runlevel 下狀態。
chkconfig --list |grep vncserver
 vncserver        0:off   1:off   2:off   3:off   4:off   5:off   6:off

設定 lighttpd 在 runlevel 為 2、3、5 時會啟動服務。
chkconfig --levels 235 vncserver on     
檢查剛才的設定是否生效。
chkconfig --list |grep vncserver
 vncserver        0:off   1:off   2:on    3:on    4:off   5:on    6:off




4、補充:IPTable 設定

若照上述設定都完成了但就是連不上 VNC Server?有可能是您忘了開啟 IPTable Port (或是忘了關掉 IPTable Service),若有開啟 IPTable 的話就加入您所要開啟的 VNC Port,然後重新啟動 iptable service。
vi /etc/sysconfig/iptables
 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22   -j ACCEPT
 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6000 -j ACCEPT  //加入此行

將 VNC Service Port 加入 iptable 允許規則後我們重新啟動 iptable service,之後再試試應該就可順利連結 VNC Server 了。
/etc/rc.d/init.d/iptables restart
 Flushing firewall rules:                                     [OK]
 Setting chains to policy ACCEPT: filter                      [OK]
 Unloading iptables modules:                                  [OK]
 Applying iptables firewall rules:                            [OK]
 Loading additional iptables modules: ip_conntrack_netbios_n  [OK]  




5、參考

文章標籤: