[root@dba ~]# yum -y install gtk-vnc*
Loaded plugins: refresh-packagekit, security
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package gtk-vnc.x86_64 0:0.3.10-3.el6 will be installed
---> Package gtk-vnc-python.x86_64 0:0.3.10-3.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================
Installing:
gtk-vnc x86_64 0.3.10-3.el6 rhel-Server 95 k
gtk-vnc-python x86_64 0.3.10-3.el6 rhel-Server 17 k
Transaction Summary
======================================================================================================================================
Install 2 Package(s)
Total download size: 111 k
Installed size: 284 k
Downloading Packages:
--------------------------------------------------------------------------------------------------------------------------------------
Total 2.4 MB/s | 111 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : gtk-vnc-0.3.10-3.el6.x86_64 1/2
Installing : gtk-vnc-python-0.3.10-3.el6.x86_64 2/2
Verifying : gtk-vnc-python-0.3.10-3.el6.x86_64 1/2
Verifying : gtk-vnc-0.3.10-3.el6.x86_64 2/2
Installed:
gtk-vnc.x86_64 0:0.3.10-3.el6 gtk-vnc-python.x86_64 0:0.3.10-3.el6
Complete!
[root@dba ~]# yum -y install gtk-vnc*
Loaded plugins: refresh-packagekit, security
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package gtk-vnc.x86_64 0:0.3.10-3.el6 will be installed
---> Package gtk-vnc-python.x86_64 0:0.3.10-3.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================
Installing:
gtk-vnc x86_64 0.3.10-3.el6 rhel-Server 95 k
gtk-vnc-python x86_64 0.3.10-3.el6 rhel-Server 17 k
Transaction Summary
======================================================================================================================================
Install 2 Package(s)
Total download size: 111 k
Installed size: 284 k
Downloading Packages:
--------------------------------------------------------------------------------------------------------------------------------------
Total 2.4 MB/s | 111 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : gtk-vnc-0.3.10-3.el6.x86_64 1/2
Installing : gtk-vnc-python-0.3.10-3.el6.x86_64 2/2
Verifying : gtk-vnc-python-0.3.10-3.el6.x86_64 1/2
Verifying : gtk-vnc-0.3.10-3.el6.x86_64 2/2
Installed:
gtk-vnc.x86_64 0:0.3.10-3.el6 gtk-vnc-python.x86_64 0:0.3.10-3.el6
Complete!
vncpasswd
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"
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
Password:
Verify:
[etl@localhost ~]$
4、启动vncserver服务
[root@localhost ~]# service vncserver start
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文件
如下所示,将下面紫红色的部分注释取消。
clip_image004
#!/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
clip_image006
[root@localhost ~]# service vncserver restart
Shutting down VNC server: 1:root 2:etl [ OK ]
Starting VNC server: 1:root
New 'localhost.localdomain:1 (root)' desktop is localhost.localdomain:1
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
Starting applications specified in /home/etl/.vnc/xstartup
Log file is /home/etl/.vnc/localhost.localdomain:2.log
[ OK ]
6、配置防火墙
如果你不配置防火墙,此时用VNC Viewer连接的话,一般会报:"connect:Connection timed out(10060)"错误,如下所示:
clip_image008
clip_image010
一般这种情况要么关闭防火墙,要么需要配置防火墙。
[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
Flushing firewall rules: [ OK ]
Setting chains to policy ACCEPT: filter [ OK ]
Unloading iptables modules: [ OK ]
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
clip_image012
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
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29313086/viewspace-1720831/ ,如需转载,请注明出处,否则将追究法律责任。