---
title: "CentOS 6.0下vncserver安装配置"
author: "netfork"
date: "2012-09-24T23:04:06Z"
tags: []
categories: ["操作系统"]
slug: "centos-6-0-vncserver"
draft: false
lastmod: "2012-09-24T23:04:06Z"
id: 1684663
url: "https://netfork.iteye.com/blog/1684663"
---

原创作品，允许转载，转载时请务必以超链接形式标明文章 [原始出处](http://linuxjcq.blog.51cto.com/3042600/748382) 、作者信息和本声明。否则将追究法律责任。[http://linuxjcq.blog.51cto.com/3042600/748382](http://linuxjcq.blog.51cto.com/3042600/748382)

1\. 安装必要的软件包

系统为CentOS 6.0，为最小化安装。

a. 安装vncserver服务端和客户端端

```


yum install tigervnc tigervnc-server -y 


```

b. 安装fontforge

```


yum install fontforge -y 



```

如果不安装fontforge，vncviewer客户端连接上来时，文字会变成方块。

c. 安装桌面软件

```


yum groupinstall Desktop -y 


```

安装gnome桌面，我习惯使用的是gnome，也可以使用yum groupinstall "KDE Desktop"。

2\. 添加需要的用户（如果用户已经存在可以略过）

因为我是安装oracle软件需要桌面，所以添加oracle用户。

```


useradd -u 10000 oracl 


```

3\. 添加vncserver服务启动参数

```


vi /etc/sysconfig/vncservers 



VNCSERVERS
=
"1:oracle"
 



VNCSERVERARGS[1]="-geometry 1024x768 -localhost 

```

VNCSERVERS参数的格式为：

VNCSERVERS="number01:user01 number02:user02 ..."

VNCSERVERARGS\[number01]="-geometry 分辨率 ..."

VNCSERVERARGS\[number01]="-geometry 分辨率 ..."

...。

a. 指定-localhost选项，进监听本机IP地址，因为使用ssh隧道来加密数据，使用端口转发，所以可以监听本机。

b. 如果监听地址不是本机，则需要iptables开启相应的端口，端口的号的规则为：

port: number+5900。

4\. 配置相应的用户

a. 登录用户设置密码

```


su - oracle 



vncpasswd 


Password: 


Verify: 

```

设置客户端连接时的密码。

b. 添加主机名

```


vi /etc/hosts 



192.168.1.11 linuxjcq0 

```

将自己的主机名和对应的IP添加到hosts文件中，不然会报如下错误：

```


xauth: (stdin):1:  bad display name "hostname:1" in "add" command 


```

c. 设置密码

```


vncpasswd 



Password: 


Verify 

```

设置客户端连接时的密码。

c. 生成配置文件

```


vncserver 



 


New 'linuxjcq01:1 (oracle)' desktop is linuxjcq01:1 


 


Creating default startup script /home/oracle/.vnc/xstartup 


Starting applications specified in /home/oracle/.vnc/xstartup 


Log file is /home/oracle/.vnc/linuxjcq01:1.log 


```

执行vncserver命令生成相应的配置文件，例如：xstartup。

d. 设置配置文件

```


vi .vnc/xstartup 



# twm & 


gnome-session & 

```

设置为gnome-session桌面，如果在前面安装了kde桌面，设置为：startkde。

e. 结束设置

```


vncserver -kill :1 



Killing Xvnc process ID 15883 


# 退出设置用户 


exit 

```

杀死刚才创建的vncserver，退出账户。如果不想配置为服务，也可以在用户下直接启动vncserver

5\. 配置和启动服务

a. 如果想开机启动可以设置

```


chkconfig vncserver on 


```

b. 启动，停止，重启

```


service vncserver start|stop|restart


service vncserver start
Starting VNC server: 1:oracle
New 'linuxjcq01:1 (oracle)' desktop is linuxjcq01:1
 
Starting applications specified in /home/oracle/.vnc/xstartup
Log file is /home/oracle/.vnc/linuxjcq01:1.lo
```

6\. 设置ssh隧道

a. 设置连接名和要连接的服务器主机名或者IP

[![](http://img1.51cto.com/attachment/201112/114314132.jpg)](http://img1.51cto.com/attachment/201112/114314132.jpg)

b.设置连接的用户名和密码（这边使用的是密钥）

[![](http://img1.51cto.com/attachment/201112/114355318.jpg)](http://img1.51cto.com/attachment/201112/114355318.jpg)

c. 设置端口转发

[![](http://img1.51cto.com/attachment/201112/114424980.jpg)](http://img1.51cto.com/attachment/201112/114424980.jpg)

将本机5901端口接收到的数据通过ssh隧道发送到服务器，服务器再把数据转发给服务器本机的5901端口。

d. 连接刚才设置的会话

7\. 使用客户端连接

a. 下载客户端

到这个http://www.realvnc.com/网站下载vncviewer.exe文件，个人版客户端是免费的，填下信息就可以获得。

b. 连接设置

[![](http://img1.51cto.com/attachment/201112/115053345.jpg)](http://img1.51cto.com/attachment/201112/115053345.jpg)

设置VNC Server为：localhost:5901，和端口转发中的本机配置相对应

c.连接

点击"connect"连接

[![](http://img1.51cto.com/attachment/201112/122922753.jpg)](http://img1.51cto.com/attachment/201112/122922753.jpg)

8\. 命令说明

```


vncserver :
<
number
>
 [-name 
<
desktop-name
>
] [-depth 
<
depth
>
] [-geometry 
<
width
>
x
<
height
>
] 



 


vncserver -kill <
X-display
>
 



verserver -list 

```

 

本文出自 “[linuxjcq](http://linuxjcq.blog.51cto.com) ” 博客，请务必保留此出处[http://linuxjcq.blog.51cto.com/3042600/748382](http://linuxjcq.blog.51cto.com/3042600/748382)