1. 在Linux下配置使用VNC
1.1 VNC简介
VNC(Virtual Network Computing,虚拟网络计算机)是一种可以对远程计算机进行远程控制的软件,支持linux远程桌面管理,也适合其它操作系统。
VNC也是一种C/S架构的协议,需要客户端和服务端同时执行,在服务端监听5800,5801,5901等多个可能的端口,而客户端只需要向服务端发起连接请求,并输入账号密码(不是linux系统内用户的账号密码,而是VNC自己的密码)即可访问了。如果你的办公电脑(客户机)和另一台电脑(服务机)离的比较远,为了方便使用,你可以先在服务机上安装VNC服务,然后在客户机上安装VNC Viewer客户端,通过连接服务机进行远程操作。
1.2 VNC远程控制CentOS系统的配置
1.2.1 查看CentOS系统中是否有安装VNC(默认安装)
输入命令: rpm -q tigervnc tigervnc-server //查看是否安装VNC
如果显示结果为:
[root@localhost Desktop]# rpm -q tigervnc tigervnc-server
package tigervnc is not installed
package tigervnc-server is not installed
说明你机器上没有安装vnc。如果没有安装,则需要分别输入命令安装vnc和vncserver
输入命令: su // 获得root权限
输入命令: yum install tigervnc //安装tigervnc
显示结果为:
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
* base: centos.ustc.edu.cn
* extras: mirrors.zju.edu.cn
* updates: mirrors.zju.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package tigervnc.x86_64 0:1.1.0-18.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================
=
Package Arch Version Repository Size
===============================================================================
=
Installing:
tigervnc x86_64 1.1.0-18.el6 base 184 k
Transaction Summary
===============================================================================
=
Install 1 Package(s)
Total download size: 184 k
Installed size: 437 k
Is this ok [y/N]: y
Downloading Packages:
tigervnc-1.1.0-18.el6.x86_64.rpm | 184 kB 00:00
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID
c105b9de:
NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS-6 Key (CentOS 6 Official Signing Key)
<centos-6-key@centos.org>
Package: centos-release-6-5.el6.centos.11.1.x86_64
(@anaconda-CentOS-201311272149.x86_64/6.5)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : tigervnc-1.1.0-18.el6.x86_64 1/1
Verifying : tigervnc-1.1.0-18.el6.x86_64 1/1
Installed:
tigervnc.x86_64 0:1.1.0-18.el6
Complete!
输入命令: yum install tigervnc-server //安装tigernvc-server
显示结果为:
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: centos.ustc.edu.cn
* extras: mirrors.zju.edu.cn
* updates: mirrors.zju.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package tigervnc-server.x86_64 0:1.1.0-18.el6 will be installed
--> Processing Dependency: pixman >= 0.27.2 for package:
tigervnc-server-1.1.0-18.el6.x86_64
--> Processing Dependency: libX11 >= 1.6.0 for package:
tigervnc-server-1.1.0-18.el6.x86_64
--> Processing Dependency: xorg-x11-fonts-misc for package:
tigervnc-server-1.1.0-18.el6.x86_64
--> Running transaction check
---> Package libX11.x86_64 0:1.5.0-4.el6 will be updated
---> Package libX11.x86_64 0:1.6.3-2.el6 will be an update
--> Processing Dependency: libX11-common = 1.6.3-2.el6 for package:
libX11-1.6.3-2.el6.x86_64
---> Package pixman.x86_64 0:0.26.2-5.el6_4 will be updated
---> Package pixman.x86_64 0:0.32.8-1.el6 will be an update
---> Package xorg-x11-fonts-misc.noarch 0:7.2-11.el6 will be installed
--> Running transaction check
---> Package libX11-common.noarch 0:1.5.0-4.el6 will be updated
---> Package libX11-common.noarch 0:1.6.3-2.el6 will be an update
--> Processing Conflict: libX11-1.6.3-2.el6.x86_64 conflicts libxcb <
1.9.1-3
--> Restarting Dependency Resolution with new changes.
--> Running transaction check
---> Package libxcb.x86_64 0:1.8.1-1.el6 will be updated
---> Package libxcb.x86_64 0:1.11-2.el6 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================
=
Package Arch Version Repository Size
===============================================================================
=
Installing:
tigervnc-server x86_64 1.1.0-18.el6 base 1.0 M
Updating:
libxcb x86_64 1.11-2.el6 base 142 k
Installing for dependencies:
xorg-x11-fonts-misc noarch 7.2-11.el6 base 5.8 M
Updating for dependencies:
libX11 x86_64 1.6.3-2.el6 base 586 k
libX11-common noarch 1.6.3-2.el6 base 169 k
pixman x86_64 0.32.8-1.el6 base 243 k
Transaction Summary
===============================================================================
=
Install 2 Package(s)
Upgrade 4 Package(s)
Total download size: 7.9 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): libX11-1.6.3-2.el6.x86_64.rpm | 586 kB 00:00
(2/6): libX11-common-1.6.3-2.el6.noarch.rpm | 169 kB 00:00
(3/6): libxcb-1.11-2.el6.x86_64.rpm | 142 kB 00:00
(4/6): pixman-0.32.8-1.el6.x86_64.rpm | 243 kB 00:00
(5/6): tigervnc-server-1.1.0-18.el6.x86_64.rpm | 1.0 MB 00:00
(6/6): xorg-x11-fonts-misc-7.2-11.el6.noarch.rpm | 5.8 MB 00:01
----------------------------------------------------------------------
----------
Total 2.6 MB/s | 7.9 MB 00:03
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : libX11-common-1.6.3-2.el6.noarch 1/10
Updating : pixman-0.32.8-1.el6.x86_64 2/10
Installing : xorg-x11-fonts-misc-7.2-11.el6.noarch 3/10
Updating : libxcb-1.11-2.el6.x86_64 4/10
Updating : libX11-1.6.3-2.el6.x86_64 5/10
Installing : tigervnc-server-1.1.0-18.el6.x86_64 6/10
Cleanup : libX11-1.5.0-4.el6.x86_64 7/10
Cleanup : libX11-common-1.5.0-4.el6.noarch 8/10
Cleanup : libxcb-1.8.1-1.el6.x86_64 9/10
Cleanup : pixman-0.26.2-5.el6_4.x86_64 10/10
Verifying : tigervnc-server-1.1.0-18.el6.x86_64 1/10
Verifying : libxcb-1.11-2.el6.x86_64 2/10
Verifying : xorg-x11-fonts-misc-7.2-11.el6.noarch 3/10
Verifying : libX11-1.6.3-2.el6.x86_64 4/10
Verifying : pixman-0.32.8-1.el6.x86_64 5/10
Verifying : libX11-common-1.6.3-2.el6.noarch 6/10
Verifying : libX11-common-1.5.0-4.el6.noarch 7/10
Verifying : pixman-0.26.2-5.el6_4.x86_64 8/10
Verifying : libX11-1.5.0-4.el6.x86_64 9/10
Verifying : libxcb-1.8.1-1.el6.x86_64 10/10
Installed:
tigervnc-server.x86_64 0:1.1.0-18.el6
Dependency Installed:
xorg-x11-fonts-misc.noarch 0:7.2-11.el6
Updated:
libxcb.x86_64 0:1.11-2.el6
Dependency Updated:
libX11.x86_64 0:1.6.3-2.el6 libX11-common.noarch 0:1.6.3-2.el6
pixman.x86_64 0:0.32.8-1.el6
Complete!
1.2.2 输入命令vncserver 启动vncserver服务,第一次启动vncserver需要输入密码两次。这时默认打开一个远程连接桌面,编号为1,若输入命令vncserver :2,则打开编号为2的远程连接桌面
输入命令: vncserver
显示结果为:
[root@localhost Desktop]# vncserver
New 'localhost.localdomain:1 (aa)' desktop is localhost.localdomain:1
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/localhost.localdomain:1.log
[root@localhost Desktop]# vncserver :2
New 'localhost.localdomain:2 (aa)' desktop is localhost.localdomain:2
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/localhost.localdomain:2.log
1.2.3 把远程桌面的用户加入到配置文件中
输入命令: vi /etc/sysconfig/vncservers //使用该命令打开配置文件
在文件中修改下面两行命令:
VNCSERVERS="1:root" 或 VNCSERVERS="1:root 2:root" 或更多 //指定可远程登录的用户名
VNCSERVERARGS[1]="-geometry 1024x768 -alwaysshared" //指定远程桌面分辨率
1.2.4 开启VNC端口(5901)
输入命令: vi /etc/sysconfig/iptables //使用该命令打开配置文件
在文件中新增下面这行命令:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5901 -j ACCEPT //新增5901这个端口
1.2.5 重启防火墙
输入命令: service iptables restart //重启防火墙
显示结果为:
[root@localhost Desktop]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
或者直接关闭防火墙:
service iptables stop //关闭防火墙
显示结果为:
[root@localhost Desktop]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
检测防火墙状态:
service iptables status //检测防火墙状态
显示结果为:
[root@localhost Desktop]# service iptables status
iptables: Firewall is not running.
1.2.6 设置开机自动启动vnc(可选)
输入命令: vi /etc/rc.d/rc.local //使用该命令打开配置文件
增加该条: /etc/init.d/vncserver start //设置开机自启vnc方式一
1.2.7 设置开机自启动vncserver
输入命令: chkconfig vncserver on //设置自动启动vnc方式二
输入命令: chkconfig --list|grep vnc //查看是否已经是系统的服务
这个时候重启系统,那么vncserver就会以服务的方式自动起来,但是现在如果想立即使用vnc则直接敲命令vncserver即可
1.2.8 如果启动vncserver时提示
WARNING:The first attempt to start Xvnc failed, possibly because the font
catalog is not properly configured. Attempting to determine an appropriate
font path for this system and restart Xvnc using that font path ...
Could not start Xvnc.
这是因为版本原因导致字体问题,需要安装、更新X11的包,或者看提示pixman安装此包。
命令如下:yum install pixman pixman-devel libXfont -y-
1.2.9 查询服务器IP
输入命令: ip addr //在连接时用
显示结果为:
1.3 更改vncserver端口配置
1.3.1 vnc-server修改端口号
输入命令: vi /usr/bin/vncserver //打开文件
文件中有这行代码 :$vncPort = 5900 + $displayNumber
可以通过修改displayNumber的值,来修改vnc-server的端口号
1.3.2 在文件中添加窗口界面
输入命令: vi /etc/sysconfig/vncservers //打开文件可设置如下
修改下面配置:
VNCSERVERS="1:root 2:root" //指定可远程登录的用户名
VNCSERVERARGS[1]="-geometry 1024x768 -alwaysshared" //指定远程桌面分辨率
VNCSERVERARGS[2]="-geometry 1024x768 -alwaysshared" //指定远程桌面分辨率
注意:
(1)-alwaysshared表示同一个显示端口允许多用户同时登录 -depth代为色深,参数有8、16、24、32;
(2)这里的“用户名”是指linux系统用户的名称;
(3)上面三行中第一行是设定可以使用VNC服务器的帐号,可以设定多个,但中间要用空格隔开。注意前面的数字“1”或是“2”,当你要从其它电脑来连接VNC服务器时,就需要用IP:1这种方法,而不能直接用IP。如假定你的VNC服务器IP是192.168.1.100,那想进入VNC服务器,并以peter用户登录时,需要在vncviewer里输入IP的地方输入:192.168.1.100:1,如果是root,那就是192.168.1.100:2
1.3.3 设置vnc访问密码:vncpasswd
输入命令: vncpasswd //修改vnc远程登录密码
1.3.4 修改远程桌面显示配置文件
输入命令: vim /root/.vnc/xstartup
1.3.5 重启vncserver
输入命令: service vncserver restart //重启vncserver
关闭防火墙: service iptables stop(一定要再次关闭防火墙)
1.3.6 在远程连接系统时输入root登陆密码后出现Authentication failure的提示解决办法:
每次服务器关闭后,都要kill掉端口号,重新申请端口号
输入命令: vncserver -kill :2
显示结果为:
[root@localhost Desktop]# vncserver -kill :2
Killing Xvnc process ID 13783
1.4 windows作为客户端连入linux服务端配置过程
1.4.1 windows下安装VNC Viewer,通过windows主机连入我们的linux
1.4.2 配置VNC Server为 IP:端口号
1.4.3 提示Encryption 点击Continue
1.4.4 填写密码