天天看點

Linux下配置使用VNC

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
           

如果顯示結果為:

[[email protected] 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)
 &[email protected]&gt
 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
           

顯示結果為:

[[email protected] 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
[[email protected] 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  //重新開機防火牆
           

顯示結果為:

[[email protected] 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  //關閉防火牆
           

顯示結果為:

[[email protected] Desktop]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
           

檢測防火牆狀态:

service iptables status  //檢測防火牆狀态
           

顯示結果為:

[[email protected] 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 //在連接配接時用
           

顯示結果為:

Linux下配置使用VNC

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
           

顯示結果為:

[[email protected] 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:端口号
Linux下配置使用VNC
1.4.3 提示Encryption 點選Continue
Linux下配置使用VNC
1.4.4 填寫密碼
Linux下配置使用VNC
Linux下配置使用VNC
1.4.5 連接配接linux成功

繼續閱讀