什么是VNC,以下是百度百科名片:
VNC (Virtual Network Computing)是虚拟网络计算机的缩写。VNC是一款优秀的远程控制工具软件,由著名的AT&T的欧洲研究实验室开发的。VNC是在基于UNIX和Linux操作系统的免费的开放源码软件,远程控制能力强大,高效实用,其性能可以和Windows和MAC中的任何远程控制软件媲美。 在Linux中,VNC包括以下四各命令:vncserver,vncviewer,vncpasswd,和vncconnect。大多数情况下我只需要其中的两个命令:vncserver和vncviewer。

一、安装VNC

这里使用station5做为服务端测试,ip为:192.168.0.5
默认情况下RHEL 5.x已安装VNC 4.1 可通过命令查看

[root@station5 ~]# rpm -qa vnc-server
vnc-server-4.1.2-14.el5_3.1

 

如果默认没有安装,可进入进光盘目录下执行

[root@station5 Server]# rpm -ivh vnc-server-4.1.2-14.el5_3.1.i386.rpm

二、启动vnc服务

[root@station5 ~]# vncserver :1    #这里的1表示每个用户连接需要占用一个桌面

You will require a password to access your desktops.

Password:                           
Verify:
                                #这里提示输入客户端连接时使用的密码
xauth:  creating new authority file /root/.Xauthority

New 'station5.example.com:1 (root)' desktop is station5.example.com:1

Creating default startup script /root/.vnc/xstartup
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/station5.example.com:1.log

由于是第一次执行,才需要密码,下次执行的时候就没有这个提示了,这个密码会保存在用户主目录下的 .vnc的passwd
以下是.vnc目录下的内容:

[root@station5 .vnc]# ls
passwd                 #保存经过加密的密码
station5.example.com:1.log        #vnc的日记
station5.example.com:1.pid        #进程的PID
xstartup                #每次执行vnc后都会执行的配置文件

防火墙放行vnc端口,如果防火墙是关闭的,可以略过配置防火墙。vnc服务使用的端口号与桌面号相关,vnc使用tcp端口从5900开始,与桌面号的对就关系如下:

桌面号 1 端口号为:5901
桌面号 2 端口号为:5902

以此类推。。。

我这里开放桌面号1的端口

[root@station5 ~]# iptables -I INPUT -p tcp --dport 5901 -j ACCEPT

三、测试vnc服务

使用另一台电脑进行测试,首先当然是安装vnc

[root@localhost Server]# rpm -ivh vnc-4.1.2-14.el5_3.1.i386.rpm

安装完成后,在终端输入

[root@localhost Server]# vncviewer 192.168.0.5:1

提示输入密码
p_w_picpath

输入密码后可登录vnc服务器,只是登录后只有一个简单的shell
p_w_picpath

PS:如发现没有vncviewer这个命令,说明还要安装一个vnc.i386的软件包,在光盘目录有。

接下来配置vnc图形桌面环境为KDE或GNOME

四、配置vnc图形桌面环境为GNOME

编辑用户主目录下面的vnc 配置文件

[root@station5 ~]# vim /root/.vnc/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 &

最后一行 twm& 表示启动 twm 修改成 gnome-session& 启动gnome ,修改成startkde&启动KDE
如果Linux服务器端启用了GNOME或KDE图形环境,当vnc客户端连接服务器后,可能只显示
灰屏,这里需要去掉以下两行#注释,如果服务器处在init3模式,不需要改
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc

我这里使用gnome

#!/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" &
gnome-session &

修改完成后,重启服务

[root@station5 ~]# vncserver -kill :1
Killing Xvnc process ID 22344
[root@station5 ~]# vncserver :1

New 'station5.example.com:1 (root)' desktop is station5.example.com:1

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/station5.example.com:1.log

再次使用客户端连接
p_w_picpath

五、设置开机启动vnc
以上提到的是手工启动方法,当服务器重启后就会失效,要做到系统启动后,自动启动vcn服务,要
修改/etc/sysconfig/vncservers文件,在后面加上以下内容

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

[root@station5 ~]# chkconfig vncserver on    #设置服务开机启动