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