VNC 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的。VNC 是在基于 UNIX 和 Linux 操作系统的免费的开源软件,远程控制能力强大,高效实用,其性能可以和 Windows 和 MAC 中的任何远程控制软件媲美。 在 Linux 中,VNC 包括以下四个命令:vncserver,vncviewer,vncpasswd,和 vncconnect。大多数情况下用户只需要其中的两个命令:vncserver 和 vncviewer。

1.命令描述

vncserver:此服务程序必须在在主(或遥控)计算机上运行。你只能作为使用者(不需要根用户身份)使用此项服务。
vncviewer:本地应用程序,用于远程接入运行vncserver的计算机并显示其环境。你需要知道远程计算机的IP地址和vncserver设定的密码。
vncpasswd:vncserver的密码设置工具。vncserver服务程序没有设置密码将不能运行。如果你没有设置,运行vncserver时它会提示你输入一个密码。
vncconnect:告诉vncserver连接到远程一个运行vncviewer的计算机的IP和端口号。这样我就可以避免给其他人一个接入的密码。
Xvnc:一个vnc“主控”程序,一般来说不需要直接运行。(vncserver和vncviewer实际上是Xvnc的脚本)
查找所有可用的选项,运行:
Xvnc --help
出于安全的考虑,一般不建议直接以超级用户帐号运行vncserver程序。如果你需要超级用户的环境,请以一般用户登录后再使用su命令登录到超级用户帐号。
VNC运行的工作流程如下:
(1) VNC客户端通过浏览器或VNC Viewer连接至VNC Server。
(2) VNC Server传送一对话窗口至客户端,要求输入连接密码,以及存取的VNC Server显示装置。
(3) 在客户端输入联机密码后,VNC Server验证客户端是否具有存取权限。
(4) 若是客户端通过VNC Server的验证,客户端即要求VNC Server显示桌面环境。
(5) VNC Server通过X Protocol 要求X Server将画面显示控制权交由VNC Server负责。
(6) VNC Server将来由 X Server的桌面环境利用VNC通信协议送至客户端,并且允许客户端控制VNC Server的桌面环境及输入装置。

以下安装环境一CentOS6.5为例。


2.安装 VNC

首先检查一下本机是否安装了 VNC,默认情况下,CentOS 6.0 是没有安装的。
检查是否安装,输入:

[root@localhost ~]# rpm -q vnc vnc-server
得到:

package vnc is not installed
package vnc-server is not installed

提示没有安装,那么就开始安装,输入:

[root@localhost ~]# yum install vnc vnc-server
注:如果是 Ubuntu 则输入:
[root@localhost ~]# apt-get install vnc vnc-server
系统会提示你确认一些选项,输入 "Y" 确认。

首次设置VNC密码

[root@localhost ~]# vncserver

重复输入两次即可。


错误解决1

安装过程中课程出现错误:

[dix] Could not init font path element /usr/share/X11/fonts/misc, removing from list!
[dix] Could not init font path element /usr/share/X11/fonts/Type1, removing from list!
[dix] Could not init font path element /usr/share/fonts/default/Type1, removing from list!
[dix] Could not init font path element built-ins, removing from list! Fatal server error:
could not open default font 'fixed'

解决方法:

 yum install libXfont xorg-x11-xinit xorg-x11-fonts-Type1


错误解决2

安装过程中课程出现错误:

-bash vncserver: command not found

解决方法:

yum install tightvnc-server

3.配置桌面

[root@localhost ~]# vi ~/.vnc/xstartup

主要修改最后两行。改为:

# xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
# twm &
gnome-session &
一般是将原来配置文件的最后两行注释掉,并且加入了一行代码,表明使用的是 "Gnome" 桌面。

保存该文件即可。


4.配置登录帐号

[root@localhost ~]# vi /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 this URL:
# http://kbase.redhat.com/faq/docs/DOC-7028
# Use "-nolisten tcp" to prevent X connections to your VNC server via TCP.
# 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 -localhost"

编辑最后两行,将这两行注释去掉,得到:

VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1024x768"

如果你有多个帐号,则按照下面格式配置。

VNCSERVERS="1:user1 2:user2 3:user3"
VNCSERVERARGS[1]="-geometry 1024×768"
VNCSERVERARGS[2]="-geometry 1024×768"
VNCSERVERARGS[3]="-geometry 800×600 -depth 24 -nolisten tcp -nohttpd -localhost"  #不支持 tcp,不支持 http 连接,只能在本地测试访问。

如果使用user2,需要对user2用户执行如下操作

[root@<span style="background-color: rgb(240, 240, 240);">localhost </span>~]#su user2
[test@<span style="background-color: rgb(240, 240, 240);">localhost </span>~]$mkdir .vnc   //*创建.vnc目录,用于保存此用户的vnc配置文件*//
[test@<span style="background-color: rgb(240, 240, 240);">localhost </span>~]$vncpasswd    //*设置user2用户的vnc远程连接密码*//
[test@<span style="background-color: rgb(240, 240, 240);">localhost </span>~]$su root
[root@<span style="background-color: rgb(240, 240, 240);">localhost </span>~]#service vncserver restart


5.配置防火墙,系统服务

VNC server 监听的端口从 5900 开始,display:1 的监听 5901,display:2 监听 5902,以此类推。CentOS 的防火墙缺省是不允许连接这些端口的,所以需要使用下面的步骤打开防火墙:

[root@localhost ~]# vi /etc/sysconfig/iptables
上面我们只添加了一个 root 帐号和桌面 1,所以这里按照上面的规则,添加一个 "5901" 端口号即可。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5901 -j ACCEPT
重启防火墙服务。
设置 VNC 自动启动
[root@localhost ~]# chkconfig -level 345 vncserver on

  

6.测试VNC连接

以windows的vncviewer连接为例:

输入密码


得到效果




7.参考及拓展链接

安装程序下载: http://www.tightvnc.com/download.php
基于HTML5的VNC客户端: http://www.lisdn.com/html/61/n-10061.html


Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐