CentOS 6.3下 VNC Server 的配置,centosvnc[root@loca


操作系统:CentOS6.3

Linux 可以通过使用 SSH 远程连接来进行控制,也可以在主机执行以下命令进行投射。

[root@localhost ~]# export DISPLAY=ipaddress:0

[root@localhost ~]# xhost +

这个命令要求ipaddress的机器装有 X Server(比如 Xmanager),但有时需要将远程整个的 Linux 桌面投射到本地,这时仍可通过安装配置 Xmanager 来实现。

本文主要介绍 VNC Server 的配置方法,需配置好 yum 源,文中的“主机”指安装 Linux 的机器。

首先验证主机上是否安装 VNC Server,使用

[root@localhost ~]# yum list | grep vnc

的命令。返回

tigervnc.x86_64 1.0.90-0.10.20100115svn3945.el6 @local

tigervnc-server.x86_64 1.0.90-0.10.20100115svn3945.el6 @local

就代表已经存在,否则就通过

[root@localhost ~]# yum install "*vnc*"

的命令进行安装。

单用户root配置方法:

1. 启动 VNC Server,这时会要求你输入两次进行连接的初始密码。

[root@localhost ~]# vncserver

You will require a password to access your desktops.

Password: //输入密码

Verify: //再次输入密码

xauth: creating new authority file /root/.Xauthority

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

这里要注意:每个用户都可以启动多个 VNC Server 远程桌面,它们用 ip 加端口号:ip:1、ip:2、ip:3 来标识区分,使用同一端口会使另外登录的用户自动退出。另,VNC Server 的大部分配置文件及日志文件都在用户 home 目录的.vnc 目录下。

用户可以自定义启动号码如:

[root@localhost ~]# vncserver :2 #注意:2前面一定要有空格。

A VNC server is already running as :2

2. Red Hat Linux 支持两种图形模式:KDE 模式和 gnome 模式。

你的 Redh Hat 使用的什么图形模式这个一般只有登录到图形界面查看一下才能知道,或者通过

[root@localhost ~]# ps -aux | grep gnome

这样的命令来判断。

如果是 gnome 桌面,那么需要修改/root/.vnc/xstartup的配置文件。

使用 vi 打开 xstartup 文件,如下:

[root@localhost /]# cd /root/.vnc

[root@localhost .vnc]# vi xstartup

删除全部内容,然后添加如下内容:

unset SESSION_MANAGER

exec /etc/X11/xinit/xinitrc

3. 重启动 VNC Server

[root@localhost ~]# vncserver -kill :1

Killing Xvnc process ID 21198

[root@localhost ~]# vncserver

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

在防火墙上打开 VNC Server 的监听端口

VNC Server 用到了三个端口:5801、5901、6001。

[root@localhost /]# cd /etc/sysconfig

[root@localhost sysconfig]# vi iptables

添加如下三行

-A INPUT -m state --state NEW -m tcp -p tcp --dport 5801 -j ACCEPT

-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 6001 -j ACCEPT

注意,一定要在

-A INPUT -j REJECT --reject-with icmp-host-prohibited

的前面,否则不会生效。

重新启动 iptables。

[root@localhost sysconfig]# /etc/rc.d/init.d/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 ip_conntrack_ftp [ OK ]

在 Windows 机器上使用 VNC Viewer 连接 VNC Server

下载VNC Viewer,运行然后输入IP:5901,输入刚才设定的密码,进入 RHEL 桌面。

多用户配置方法:

1、安装完vncserver,并将vncserver设为自启动:

#chkconfig --level 345 vncserver on

2、修改配置文件,设置可通过VNC客户端登陆的用户:

#vi /etc/sysconfig/vncservers

修改vnc配置文件为:

---------------------------

# 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"

VNCSERVERS="1:root 2:test1 3:test2"

VNCSERVERARGS[1]="-geometry 800x600"

VNCSERVERARGS[2]="-geometry 800x600"

VNCSERVERARGS[3]="-geometry 800x600"

----------------------------

# VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -nohttpd -localhost"说明:VNCSERVERS为可通过VNC客户端远程登陆的用户

格式为:1:用户1 2:用户2 3:用户3

但这样做了,仅仅用户1才能登陆,用户2、用户3是无法登陆的。

3、修改每个用户宿主目录下的vnc配置文件,允许多用户登陆

#su - root

$mkdir .vnc //*创建.vnc目录,用于保存此用户的vnc配置文件*//

$vncpasswd //*设置root用户的vnc远程连接密码*//

#vi xstartup

删除全部内容,然后添加如下内容:

---------------

unset SESSION_MANAGER

exec /etc/X11/xinit/xinitrc

---------------

#su - test1

$mkdir .vnc //*创建.vnc目录,用于保存此用户的vnc配置文件*//

$vncpasswd //*设置test1用户的vnc远程连接密码*//

#vi xstartup

-------------

unset SESSION_MANAGER

exec /etc/X11/xinit/xinitrc

-------------

同理test2

4.更新X11字体包(支持centos默认更新源更新)

yum install pixman pixman-devel libXfont


5.重启VNC服务

#service vncserver restart

6.修改权限

su - root

chmod 755 .vnc/xstartup

su - test1

chmod 755 .vnc/xstartup

su - test2

chmod 755 .vnc/xstartup

7. 关闭防火墙对VNC端口的限制

如果需要从外网控制内网的某台Linux,则需要打开防火墙相应的端口,并在路由器上进行端口映射。

VNC给浏览器的端口是5800+N,给vncviewer的端口是5900+N,N是设置的display号。

如果不想打开防火墙,但是仍需要VNC这项服务,则需要:

编辑vi /etc/sysconfig/iptables

在里边加入如下两行:

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5901 -j ACCEPT

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5902 -j ACCEPT

这里需要的是,display N 对应的端口就是 5900+N,所以不建议设的太多。

8. 停止vncserver:(选读)

停止 vncserver 的方法是执行:vncserver -kill :1

注意:“:1”,表示停止第一个vnc桌面,1代表当前启用的VNCserver序号,“:1”之前是有一个空格的。如果目前启动的序号是2,若要关闭其,则应该使用“vncserver -kill :2”.

9. 修改vnc密码:(选读)

修改vnc密码的方法是执行:vncpasswd

输入两次密码即可。

10:检查服务器端VNC Server是否正常运行中:

执行:ps -ef|grep vnc

如果vnc在执行,可以看到类似的消息:

# ps -ef|grep vnc

root 3605 1 0 Jul13 ? 00:13:51 Xvnc :1 -desktop MyIMS.local:1 (root) -httpd /usr/share/vnc/classes -auth /root/.Xauthority -geometry 800x600 -depth 16 -rfbwait 30000 -rfbauth /root/.vnc/passwd -rfbport 5901 -pn

说明是序号为":1"vnc进程在端口为5901上进行监听。

10.VNC 客户端

服务器端运行正常后,我们方可以考虑使用VNC客户端与其相连。

Windows 客户端 VNC Viewer 的配置:

a. 从 http://www.realvnc.com/download.html 下载 VNC Free Edition for Windows Version 4.1.2

b. 打开 VNCViewer :

填入VNCServer 的IP:编号(1或2或…)

c. VNCViewer 切换全屏模式:F8

11 一些配置文件:

/etc/X11/xorg.conf 设置屏幕分辨率

到这里,您的VNC应该已经能够连上,但有可能出现黑屏的问题:

a、查看/root/.vnc/下的log日志,有没有报错;有可能是xstartup权限不够。“chmod xstartup 777”更改下

权限,重启后再进。

b、重新启动下电脑,试一试。我就是各种配置之后,始终黑屏,重启电脑后OK。

12.查看VNC登陆日志:


访问在你登陆用户所在家目录的.vnc/主机名:1.log文件

例如:

/root/.vnc/www.test.com:1.log

----------其他配置-------------

VNCServer配置


redhat 一般都自动安装了vncserver了 只要在服务列表中选中启动一下就可以.


通过编辑文件$HOME/.vnc/xstartup来启动你喜欢的窗口管理器。使用


startkde &来启动KDE,使用gnome-session &来启动GNOME.


首次创建 ~/.vnc/xstartup 时,指定的窗口管理器是 twm ,它是一个极小的窗口管理器,几乎每台 X Window 系统机器上都有 twm。twm 不具备完整“桌面管理器”(象 KDE、GNOME 或 WindowMaker)的大部分花哨功能。下面是修改过的示例:

#!/bin/sh

[ -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 &


#exec wmaker


#exec startkde


gnome-session&

上面的示例中,注释掉了缺省 twm 和 xterm 的缺省启动。配置了这个桌面来避免背景和标题栏上的颜色渐变,并使用极少的动画效果。


关闭服务器上自己的vnc连接


vncserver -kill :桌面号


开机自动运行vncserver


1.设置vncserver启动脚本(/etc/init.d/vncserver)实现系统自启动时启动vncserver.


2.编辑/etc/sysconfig/vncservers


VNCSERVERS="1:user1"


ARGS="-geometry 800x600 -alwaysshared "


在ARGS 中修改"800x600"来适应自己的X桌面实际参数配置。可以在这里添加任何其他VNC服务器参数配置。在VNCSERVERS中修改user1为你希望运行VNC桌面的用户。VNCSERVERS中的1表示VNC以桌面1运行,如果希望添加其他的桌面,可以修改配置如下:


VNCSERVERS="1:user12:user23:user3"


3.设置vnc server的访问密码


vncpasswd


在RedHat系统上,使用以下命令启动VNC:


/etc/init.d/vncserver start

----------------

更改root与vnc密码,配置vnc

更改root密码。

登录到root用户下

#passwd

更改vnc密码

#vncpasswd

配置vnc

#vi /etc/sysconfig/vncservers

找到vncservers="1:myusername"这个字符串,去掉注释,把myusername改为你的登录用户名。然后保存退出。

#service vncserver start//这一步是让他生成xstatup文件

#vi .vnc/xstatup//这一步在root@localhost#下运行

unset SESSION_MANAGER

exec /etc/X11/xinit/xinitrc(去掉注释)

#vi /etc/sysconfig/iptables

在其中加入5901端口,然后保存退出

#service iptables restart

#service vncserver restart

完成

如果要设置除去root用户以外的用户

首先创建一个用户

root@localhost# cp -r .vnc /home/userName

root@localhost#chown-R userName:groupName .vnc

如果连接VNC出现too many security failures这个问题:可能是因为VNC关闭了,重新启动一下VNC服务。#service vncserver restart


本文出自 “一路向北” 博客,请务必保留此出处http://showerlee.blog.51cto.com/2047005/1127490

相关内容

    暂无相关文章