vnc概述
中的任何遠端控制軟體媲美。在 linux 中,vnc 包括以下四個指令:vncserver,vncviewer,vncpasswd,和
vncconnect。大多數情況下隻需要其中的兩個指令:vncserver 和
vncviewer。目前,原來的at&t版本已經不再使用,因為更多有重大改善的分支版本已經出現, 像是realvnc, vnc
tight 和ultravnc。 real vnc 是目前最活躍和強大的主流應用。
vnc原理
vnc系統由用戶端,服務端和一個協定組成。vnc
的服務端目的是分享其所運作機器的螢幕, 服務端被動的允許用戶端控制它。 vnc用戶端(或viewer) 觀察控制服務端,與服務端互動。 vnc
協定 protocol (rfb)是一個簡單的協定,傳送服務端的原始圖像到用戶端(一個x,y 位置上的正方形的點陣資料),
用戶端傳送事件消息到服務端。
伺服器發送小方塊的幀緩存給用戶端,在最簡單的情況,vnc協定使用大量的帶寬,是以各種各樣的方法被發明出來減少通訊的開支,舉例來說,有各種各樣的編碼方法來決定最有效率的方法來傳送這些點陣方塊)
協定允許用戶端和服務端去協定哪種編碼會被使
用,最簡單的編碼,被大多數用戶端和服務端所支援的是, 從左到右的像素掃描資料的原始編碼,
當原始的滿屏被發送後,隻發送變化的方塊區域。這種編碼在幁間隻有小部分螢幕變化的情況下工作的非常好(像是滑鼠鍵在桌面移動的情況,或在光标處敲擊文
字),不過如果大量的像素同時變化帶寬将會增加的非常高,像是拖動一個視窗或觀看全屏錄像。
實驗環境
vnc服務端:
作業系統:red hat enterprise linux server release 5.7 (tikanga)
vnc用戶端:
作業系統:windows 7專業版 64位作業系統
vnc安裝配置
1、安裝vnc包
[root@localhost /]# cd /depot/os/mnt/cdrom/server
[root@localhost /]# rpm -ivh vnc-server-4.1.2-14.el5_6.6.x86_64.rpm
[root@localhost /]# rpm -ivh vnc-4.1.2-14.el5_6.6.x86_64.rpm
驗證vnc-server包是否安裝成功:
[root@localhost /]# rpm -qa vnc-server
vnc-server-4.1.2-14.el5_6.6
2、配置vncservers檔案
修改/etc/sysconfig/vncservers檔案,未經修改的vncservers檔案如下所示:
[root@localhost ~]# more /etc/sysconfig/vncservers
# the vncservers variable is a list of display:user pairs.
#
# uncomment the lines below to start a vnc server on display :2
# as my 'myusername' (adjust this to your own). you will also
# need to set a vnc password; run 'man vncpasswd' to see how
# to do that.
# do not run this service if your local area network is
# untrusted! for a secure way of using vnc, see
# <url:http://www.uk.research.att.com/archive/vnc/sshvnc.html>.
# use "-nolisten tcp" to prevent x connections to your vnc server via tcp.
# use "-nohttpd" to prevent web-based vnc clients connecting.
# use "-localhost" to prevent remote vnc clients connecting except when
# doing so through a secure tunnel. see the "-via" option in the
# `man vncviewer' manual page.
# vncservers="2:myusername"
# vncserverargs[2]="-geometry 800x600 -nolisten tcp -nohttpd -localhost"

将最後兩行配置資訊取消注釋,添加系統賬号
vncservers="1:root 2:etl"
vncserverargs[1]="-geometry 1024x768 -nolisten tcp -nohttpd "
vncserverargs[2]="-geometry 1024x768 -nolisten tcp -nohttpd "
vncservers 是用來設定可以使用vnc的伺服器賬号,可以設定多個,例如上面root、etl,但是中間要用空格隔開。使用vncviewer登入時,192.168.48.128:1表示是以root賬号登入,以此類推。
關于參數配置說明:
1:-geometry 表示桌面分辨率,預設為1024x768,是以上面的1024x768也可以不寫。
2:-nohttpd 表示不監聽http端口(58xx)。
3:-nolisten tcp 表示不監聽tcp端口(60xx)
4:-localhost 隻運作從本機通路。
5:alwaysshared 預設隻允許一個vncviewer連接配接,此參數表示同一個顯示端口允許多使用者同時登入.
6:-depth 表示色深,參數有8,16,24,32.
7: securitytypes none 登入不需要密碼認證vncauth預設值,要密碼認證。
3、設定vnc使用者密碼
如果此時不設定vnc使用者密碼,啟動vncserver服務,則會報如下錯誤:
[root@localhost ~]# service vncserver start
starting vnc server: 1:root [failed]
[root@localhost /]# vncpasswd
password:
verify:
[root@localhost /]# su - etl
[etl@localhost ~]$ vncpasswd
[etl@localhost ~]$
4、啟動vncserver服務
starting vnc server: 1:root
new 'localhost.localdomain:1 (root)' desktop is localhost.localdomain:1
creating default startup script /root/.vnc/xstartup
starting applications specified in /root/.vnc/xstartup
log file is /root/.vnc/localhost.localdomain:1.log
2:etl
new 'localhost.localdomain:2 (etl)' desktop is localhost.localdomain:2
creating default startup script /home/etl/.vnc/xstartup
starting applications specified in /home/etl/.vnc/xstartup
log file is /home/etl/.vnc/localhost.localdomain:2.log
[ ok ]
vnc會在使用者根目錄($home)下的".vnc"檔案夾下生成一系列檔案。其中passwd為vnc使用者密碼檔案,由vncpasswd生成。其他的都由vnc初次啟動時生成,xstartup為vnc用戶端連接配接時啟動的腳本
5、配置xstartup檔案
如下所示,将下面紫紅色的部分注釋取消。
#!/bin/sh
# uncomment the following two lines for normal desktop:
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 &
xterm -geometry 80x24+10+10 -ls -title "$vncdesktop desktop" &
twm &
切換到etl賬号,依法炮制
[root@localhost ~]# su - etl
[etl@localhost ~]$ vi /home/etl/.vnc/xstartup
[root@localhost ~]# service vncserver restart
shutting down vnc server: 1:root 2:etl [ ok ]
6、配置防火牆
如果你不配置防火牆,此時用vnc viewer連接配接的話,一般會報:"connect:connection timed out(10060)"錯誤,如下所示:
一般這種情況要麼關閉防火牆,要麼需要配置防火牆。
[root@localhost ~]# service iptables stop
flushing firewall rules: [ ok ]
setting chains to policy accept: filter [ ok ]
unloading iptables modules: [ ok ]
關閉防火牆後,用vncview連接配接伺服器沒有問題,但是一般不建議關閉防火牆,
[root@localhost ~]# service iptables restart
applying iptables firewall rules: [ ok ]
loading additional iptables modules: ip_conntrack_netbios_ns [ ok ]
[root@localhost ~]# iptables -i input -p tcp --dport 5901 -j accept
[root@localhost ~]# iptables -i input -p tcp --dport 5902 -j accept
ok,可以通過vnc連接配接到伺服器了
關于vnc服務使用的端口号與桌面号相關,vnc使用tcp端口從5900開始,對應關系如下
桌面号為“1” ---- 端口号為5901
桌面号為“2” ---- 端口号為5902
桌面号為“3” ---- 端口号為5903
……
基于java的vnc客戶程式web服務tcp端口從5800開始,也是與桌面号相關,對應關系如下
桌面号為“1” ---- 端口号為5801
桌面号為“2” ---- 端口号為5802
桌面号為“3” ---- 端口号為5803
基于上面的介紹,如果linux開啟了防火牆功能,就需要手工開啟相應的端口,以開啟桌面号為“1”相應的端口為例,指令如下
開機自啟動vncserver服務
# chkconfig vncserver on