Linux下VNC配置使用总结:开启+桌面配置+安全访问

操作环境:CentOS 5.3 + Windows XP SP3 32bit + RealVNC 4.1.2 i386 + TigerVNC.

参考:潇湘隐者-Linux系统VNC配置实践总结萨米的博客-VNC配置孤叶风铃-Linux 开启VNCSERVER远程桌面设置:利用putty进行vnc + ssh tunneling

VNC是基于RFB(Remote FrameBuffer)的一款开源的远程桌面控制软件。目前,原来的AT&T版本已经不再使用,因为更多有重大改善的分支版本已经出现,像是RealVNC,VNC tight 和UltraVNC。Real VNC 是当前最活跃和强大的主流应用。

 一、安装VNC Server

CentOS 5.3默认已经安装vnc与vnc-server,对应的是realvnc的4.1.2版本。

对于没有安装vnc的Linux系统可以:

1、yum安装,CentOS 5.3 yum源自带了vnc与vnc-server;

 [root@localhost ~]# yum install vnc

 [root@localhost ~]# yum install vnc-server

2、可以从realvnc官网下载,解压归档后得到两个rpm包,安装vnc-server即可。

 

二、配置VNC Server

VNC Server的配置文件为 /etc/sysconfig/vncservers,在文件末尾添加以下两句:

VNCSERVERS="2:hubery"
VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -nohttpd -localhost"
#桌面分辨率为800*600 ,阻止图形桌面通过TCP端口 ,不能通过WEB访问vncserver,不能通过不安全的方式从远程登录。

 关于参数配置说明:

1:-geometry 表示桌面分辨率,默认为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默认值,要密码认证。

 

由于root用户使用的是第一个VNC Server,我们添加Server是从2开始的,所以这里桌面号是2,用户是hubery。

VNC使用的起始端口是5900和5800,桌面号是2时,VNC Viewer访问的端口是5902,WEB方式(java)访问的端口号是5802。

root用户的配置也是这个文件,若要配置需要在文件末尾同样加上类似以上两句。

以上配置,vncserver将在服务启动时打开vncserver :2。

 

三、防火墙配置

如果不熟悉防火墙iptables,可以直接关掉(重启失效):

[root@localhost ~]# iptables -L

[root@localhost ~]# service iptables stop

但一般不建议这样做,我们需要在防火墙里打开某些端口:

[root@localhost ~]# iptables -I INPUT -p tcp --dport 5901:5902 -j ACCEPT
[root@localhost ~]# iptables -I INPUT -p udp --dport 5901:5902 -j ACCEPT

同样,上述操作在计算机重启之后也会失效,以下操作将配置保存到配置文件,使其永久生效:

[root@localhost ~]# service iptables save

 

四、启动VNC Server

在启动Server之前需要给远程控制设置一个访问密码:

[root@localhost ~]# su hubery
[hubery@localhost root]$ vncpasswd
Password:
Verify:
[hubery@localhost root]$ vncserver :2
xauth:  creating new authority file /home/hubery/.Xauthority

New 'localhost:2 (hubery)' desktop is localhost:2

Creating default startup script /home/hubery/.vnc/xstartup
Starting applications specified in /home/hubery/.vnc/xstartup
Log file is /home/hubery/.vnc/localhost:2.log

[hubery@localhost root]$

 关闭某个vncserver:

vncserver -kill :2

 

五、VNC Viewer访问

我的VNC客户端是在Win XP下安装的TigerVNC Viwer,可以从其官网免费下载。安装VNC Viewer后以 Server_IP:桌面号 的形式访问。

 

六、桌面配置(可选)

以上配置登陆之后得到的是twm桌面,看起来像一个终端,但它还是桌面,我们可以从命令行启动桌面应用,例如firefox,但是一般人是用不习惯、不会去用它的,可以换其它的桌面吗?当然可以,其配置文件为(用户根目录下)~/.vnc/xstartup,将twm修改为gnome-session或startkde即可切换成gnome或kde。

 

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

 

切换之后重启服务是必须的:

[root@localhost .vnc]# service vncserver restart

 或

[root@localhost .vnc]# su hubery
[hubery@localhost .vnc]$ vncserver -kill :2
Killing Xvnc process ID 20353
[hubery@localhost .vnc]$ vncserver :2

New 'localhost:2 (hubery)' desktop is localhost:2

Starting applications specified in /home/hubery/.vnc/xstartup
Log file is /home/hubery/.vnc/localhost:2.log

[hubery@localhost .vnc]$


使用Viewer重新登陆,你会发现我们熟悉的桌面又回来了:

 

七、安全访问VNC(可选)

如果直接使用vncviewer来进行访问,有两点不利因素:
1. 口令传输是明文,很容易被侦听到.
2. 防火墙需要打开59xx端口,这在通常的单位里是不可能的.

幸运的是,我们有ssh这个强大的工具,象X11 Forwarding(另文论述),我们可以使用ssh隧道来保护通讯过程,下面就进行简单介绍.

我依然使用Win XP下的Tiger VNC做客户端,其实对于Linux下也是可以的(更简单)。

1、在Session下配置Host Name为Server IP,Port为SSH端口22;

2、在Connection-》SSH-》Tunnels配置Source port为VNC Server端口号5902,Destination为localhost:5902,并Add添加;

 3、使用TigerVNC Viewer访问,地址现在是localhost:2。

至此,我们使用了加密的VNC,而且也不需要配置防火墙打开端口,也就是说使用ssh隧道在没有步骤三的情况下也是有效的。

posted @ 2014-12-06 20:53  回首郑板桥  阅读(26523)  评论(0编辑  收藏  举报