CentOS下VNC使用

1. 介绍

本文主要介绍了VNC Server的配置和使用

2. 安装

CentOS中默认就有安装VNC,可以通过命令rpm查看

[Jerry@localhost ~]$ rpm -qa | grep vnc
vnc-4.1.2-14.el5_6.6
vnc-server-4.1.2-14.el5_6.6

或者通过yum命令查看

[Jerry@localhost ~]$ yum list | grep vnc
vnc.i386                                  4.1.2-14.el5_6.6             installed
vnc-server.i386                           4.1.2-14.el5_6.6             installed

如果系统中没有安装VNC,可以通过下面的命令来安装

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

至于VNC的源码安装方法,这里就不介绍了,因为它的源码好像还不是那么好找

2. 配置

首先我们来了解一下VNC Server的运作过程

一般在装有VNC Server的系统中就有如下文件

/etc/sysconfig/vncservers : VNC Server总配置文件
/etc/init.d/vncserver     : VNC Server启动脚本
/usr/bin/Xvnc             : VNC Server主程序文件
/usr/bin/vncviewer        : VNC View主程序文件
/usr/bin/vncserver        : VNC Server运行perl脚本, 实际调用Xvnc

其中init.d目录下的文件不用说,可以进行如下操作

service vncserver start|stop|restart|condrestart|status

2.1 配置文件

首先我们修改配置/etc/sysconfig/vncservers

[Jerry@localhost ~]$ cat /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="1:Jerry"
VNCSERVERARGS[1]="-geometry 800x600 -nolisten tcp -nohttpd -localhost"
[Jerry@localhost ~]$

在这里的意思是对用户Jerry开启了VNC Server功能,并绑定至1号窗口,而VNCSERVERARGS则是启动参数。

2.2 相关设置

使用Jerry用户登录系统,设置密码(此步可跳过)并运行vncserver

[Jerry@localhost ~]$ vncpasswd 
Password:
Verify:
[Jerry@localhost ~]$
[Jerry@localhost ~]$ vncserver 

New 'localhost:1 (Jerry)' desktop is localhost:1

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

[Jerry@localhost ~]$

这时在Jerry用户主目录下就生成了.vnc目录,在.vnc目录下同时生成了passwd  xstartup文件
其中xstartup文件默认如下

[Jerry@localhost ~]$ cat .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 &
[Jerry@localhost ~]$

我们将其修改为

[Jerry@localhost ~]$ cat .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" &
gnome
-session &

#twm 
&

# if Desktop is KDE ,uncomment below line, comment line "gnome-session $"
#startkde &
[Jerry@localhost ~]$

然后我们再把vncserver关闭,其中1表示1号窗口

[Jerry@localhost ~]$ vncserver -kill :1
Killing Xvnc process ID 27691
[Jerry@localhost ~]$

2.3 防火墙配置

我们可以选择关闭防火墙或让VNC Server端口开启

首先关闭防火墙

[root@localhost Jerry]# service iptables stop

随后修改iptables配置,即文件/etc/sysconfig/iptables,打开该文件并找到如下地方

-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

然后在这一行上面加入下面一行

-A RH-Firewall-1-INPUT -m state --state NEW -m 
tcp
 -p 
tcp
 --dport 
5900:5903
 -j ACCEPT

然后重新启动防火墙

[root@localhost Jerry]# service iptables restart

TIPS:
关于VNC端口
VNC给浏览器的端口是5800+N,给vncviewer的端口是5900+N,N是绑定的窗口好
如果想修改默认端口号,可修改脚本/usr/bin/vncserver

[root@localhost Jerry]# netstat -tulp | grep vnc
tcp        0      0 *:5801        *:*         LISTEN      28285/Xvnc         
tcp        0      0 *:5901        *:*         LISTEN      28285/Xvnc         
tcp        0      0 *:6001        *:*         LISTEN      28285/Xvnc         
tcp        0      0 *:6001        *:*         LISTEN      28285/Xvnc         

2.4 开机自启

然后我们可以在启动的时候就让其运行

[root@localhost bin]# chkconfig --level 3 vncserver on  
[root@localhost bin]# chkconfig --level 5 vncserver on

4. 使用

我们就可以在Windows上使用VNC-Viewer来进行远程连接

vnc_view

其中VCN Server处也可以为192.168.56.101:5901
其中192.168.56.101为VNC Server服务器地址,1为绑定的窗口号

5. 遗留

笔者在实际使用的过程中发现,若是通过开机启动就让VNC Server运行,Viewer根本无法连接
然而,若是用户Jerry直接在命令行下执行vncserver,Viewer倒是可以正常连接

参考:
<RedHat下详细配置VNC>
<CentOS 7启动VNC Server失败解决办法>
<怎样在 CentOS 7.0 上安装和配置 VNC 服务器>

原文地址:https://www.cnblogs.com/hzl6255/p/3291456.html