CentOS 下 VNC Server 的配置与使用


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 是没有安装的。
检查是否安装,输入:

[plain] view plaincopyprint?01.[root@localhost ~]# rpm -q vnc vnc-server 
[root@localhost ~]# rpm -q vnc vnc-server得到:

[plain] view plaincopyprint?01.package vnc is not installed 
02.package vnc-server is not installed 
package vnc is not installed
package vnc-server is not installed
提示没有安装,那么就开始安装,输入:

[plain] view plaincopyprint?01.[root@localhost ~]# yum install vnc vnc-server 
[root@localhost ~]# yum install vnc vnc-server注:如果是 Ubuntu 则输入:

[plain] view plaincopyprint?01.[root@localhost ~]# apt-get install vnc vnc-server 
[root@localhost ~]# apt-get install vnc vnc-server系统会提示你确认一些选项,输入 "Y" 确认。

首次设置VNC密码

[plain] view plaincopyprint?01.[root@localhost ~]# vncserver 
[root@localhost ~]# vncserver
重复输入两次即可。

3.配置桌面

[plain] view plaincopyprint?01.[root@localhost ~]# vi ~/.vnc/xstartup 
[root@localhost ~]# vi ~/.vnc/xstartup主要修改最后两行。改为:

[plain] view plaincopyprint?01.# xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" & 
02.# twm & 
03.gnome-session & 
# 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.

[plain] view plaincopyprint?01.# 
02.# Uncomment the lines below to start a VNC server on display :2 
03.# as my 'myusername' (adjust this to your own).  You will also 
04.# need to set a VNC password; run 'man vncpasswd' to see how 
05.# to do that. 
06.# 
07.# DO NOT RUN THIS SERVICE if your local area network is 
08.# untrusted!  For a secure way of using VNC, see this URL: 
09.# http://kbase.redhat.com/faq/docs/DOC-7028 
10.# Use "-nolisten tcp" to prevent X connections to your VNC server via TCP. 
11.# Use "-localhost" to prevent remote VNC clients connecting except when 
12.# doing so through a secure tunnel.  See the "-via" option in the 
13.# `man vncviewer' manual page. 
14. 
15. 
16.# VNCSERVERS="2:myusername" 
17.# VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -localhost" 
#
# 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"
编辑最后两行,将这两行注释去掉,得到:

[plain] view plaincopyprint?01.VNCSERVERS="1:root" 
02.VNCSERVERARGS[1]="-geometry 1024x768" 
VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1024x768"
如果你有多个帐号,则按照下面格式配置。

[plain] view plaincopyprint?01.VNCSERVERS="1:user1 2:user2 3:user3" 
02.VNCSERVERARGS[1]="-geometry 1024×768" 
03.VNCSERVERARGS[2]="-geometry 1024×768" 
04.VNCSERVERARGS[3]="-geometry 800×600 -depth 24 -nolisten tcp -nohttpd -localhost"  #不支持 tcp,不支持 http 连接,只能在本地测试访问。 
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用户执行如下操作

[plain] view plaincopyprint?01.[root@<SPAN style="BACKGROUND-COLOR: rgb(240,240,240)">localhost </SPAN>~]#su user2 
02.[test@<SPAN style="BACKGROUND-COLOR: rgb(240,240,240)">localhost </SPAN>~]$mkdir .vnc   //*创建.vnc目录,用于保存此用户的vnc配置文件*// 
03.[test@<SPAN style="BACKGROUND-COLOR: rgb(240,240,240)">localhost </SPAN>~]$vncpasswd    //*设置user2用户的vnc远程连接密码*// 
04.[test@<SPAN style="BACKGROUND-COLOR: rgb(240,240,240)">localhost </SPAN>~]$su root 
05.[root@<SPAN style="BACKGROUND-COLOR: rgb(240,240,240)">localhost </SPAN>~]#service vncserver restart 
[root@localhost ~]#su user2
[test@localhost ~]$mkdir .vnc   //*创建.vnc目录,用于保存此用户的vnc配置文件*//
[test@localhost ~]$vncpasswd    //*设置user2用户的vnc远程连接密码*//
[test@localhost ~]$su root
[root@localhost ~]#service vncserver restart

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

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

[plain] view plaincopyprint?01.[root@localhost ~]# vi /etc/sysconfig/iptables 
[root@localhost ~]# vi /etc/sysconfig/iptables上面我们只添加了一个 root 帐号和桌面 1,所以这里按照上面的规则,添加一个 "5901" 端口号即可。

[plain] view plaincopyprint?01.-A INPUT -m state --state NEW -m tcp -p tcp --dport 5901 -j ACCEPT 
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5901 -j ACCEPT重启防火墙服务。
设置 VNC 自动启动

[plain] view plaincopyprint?01.[root@localhost ~]# chkconfig -level 345 vncserver on 
[root@localhost ~]# chkconfig -level 345 vncserver on 

6.测试VNC连接

以windows的vncviewer连接为例:
 

\

输入密码

\

得到效果

\

 


 

相关内容