天天看點

CentOS vncserver配置

一、VNC簡單介紹:   

VNC,全稱為Virtual NetworkComputing,它是一個桌面共享系統。它的功能,類似于windows中的遠端桌面功能。VNC使用了 RFB(RemoteFrameBuffer,遠端幀緩沖)協定來實作遠端控制另外一台計算機。它把鍵盤、滑鼠動作發送到遠端計算機,并把遠端計算機的屏 幕發回到本地。VNC技術與平台無關,VNC Viewer可以和VNCServer在不同的作業系統上。VNC幾乎支援所有的作業系統,也支援Java,甚至可以通過支援Java的浏覽器來通路 VNCServer。多個VNC用戶端可以同時連接配接到一個VNC Server上。VNC最初由AT&T開發的,它的源代碼是開源的。

二、VNC在centos上安裝VNC和VNCserver:

檢視是否安裝了vnc以及VNCserver

              [root@localhost ~]# rpm -q vnc vnc-server

                     vnc-4.1.2-14.el5_3.1

                     vnc-server-4.1.2-14.el5_3.1

              如果沒有安裝,則安裝他們

              [root@localhost ~]# yum install vnc vnc-server     #ubuntu的話,就是執行apt-get install vnc vnc-server

1.需要在每個使用者下啟動vncserver,生成該使用者遠端桌面連接配接時的配置檔案以及通路密碼,方法就是切換到每個使用者,執行 vncserver。

(注 釋:在啟動VNCserver時,你也可以為server指定一個display參數。你可以把display了解為一個桌面,每個使用者都可以有自己的桌 面。VNC用戶端在連接配接時,可以指定連接配接到哪個桌面上。在系統中,display号不能重複,也就是說,如果有使用者已經建立了名為“:1”的 display,另外一個使用者就不能再使用“:1”了,他可以使用“:2”。

指定display号碼的啟動VNC server的指令是:   vncserver : 1

            [root@localhost ~]# vncserver         #運作vncserver。

                     You will require a password to access your desktops.

                     enter password:                       #首次運作會讓你設定密碼,密碼要求6位以上

                     verify password:                       #确認密碼

                     xauth: creating new authority file /root/.Xauthority   #首次運作會生成本使用者的相關配置檔案,以及日志檔案,生成位置在~.vnc/目錄下

                     New 'CPAYNET005:1 (root)' desktop is CPAYNET005:1 注意這個數字1

                     Creating default startup script /root/.vnc/xstartup

                     Starting applications specified in /root/.vnc/xstartup

                     Log file is /root/.vnc/CPAYNET005:1.log     #注意這個數字

            [root@localhost ~]# vncserver   -kill   :1    #殺死剛才運作的vncserver,數字1正是上面1.log那個1。注意上面紅色字型。

            [root@localhost ~]# vi ~/.vnc/xstartup       #編輯這個配置腳本檔案,配置使遠端登入為gnome或者kde桌面。

            #!/bin/sh

            # Uncomment the following two lines for normal desktop:

            # unset SESSION_MANAGER

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

            gnome-session &

            #startkde &#kde若是用kde桌面,則将本行開始#号去掉,注釋上面的gnome

              [root@localhost ~]#su - otheruser     #切換到别的使用者

              [otheruser@localhost ~]$ vncserver #為使用者名otheruser生成通路密碼,以及遠端連接配接時的配置檔案

                       You will require a password to access your desktops.

                       enter passwd:

                       verify password:

                      xauth: creating new authority file /home/otheruser/.Xauthority  #首次運作會生成本使用者的相關配置檔案,以及日志檔案,生成位置在~.vnc/目錄下

                     New 'CPAYNET005:1 (otheruser)' desktop is CPAYNET005:1 注意這個數字1

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

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

           [root@localhost ~]# vncserver   -kill   :1    #殺死剛才運作的vncserver,數字1正是上面1.log那個1。注意上面紅色字型。

           [root@localhost ~]# vi ~/.vnc/xstartup       #編輯這個配置腳本檔案,配置使遠端登入為gnome或者kde桌面。原來vncserver預設使

                                                                #用的視窗管理器是twm,這是一個很簡陋的視窗管理器,你可以把你的桌面改成GNOME或KDE。

          # exec /etc/X11/xinit/xinitrc

             [otheruser@localhost ~]$exit            #切換回root

如果還有别的使用者,重複執行上面紫色字型的步驟

2.接下來編輯vnc的配置檔案

              [root@localhost~]#vi /etc/sysconfig/vncservers

以下是檔案内容:

# The VNCSERVERS variable is a list of display:user pairs.  

# The VNCSERVERS variable is a list of display:user pairs.

# Uncomment the line below to start a VNC server on display : 1

# 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/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:user1 2:user2 3:user3 “

VNCSERVERARGS [ 1 ] = “ -geometry 1024×768 “

VNCSERVERARGS [ 2 ] = “ -geometry 1024×768 “

VNCSERVERARGS [ 3 ] = “ -geometry 800×600 -depth 24 -nolisten tcp -nohttpd -localhost“ 

解釋一下這個檔案:

VNCSERVERS這一行是配置在系統啟動時啟動幾個VNC server,上面的例子裡運作了三個VNC server,其中user1在display :1,user2在display :2,user3在display :3。

VNCSERVERARGS這三行,分别為VNC server 1, 2, 3配置啟動參數,上面的例子裡對user1和user2使用螢幕分辨率1024×768,

對user3使用800×600,24色,不支援tcp,不支援http連接配接,隻能在本地測試通路。     

設定完成後就可以啟動vncserver服務了。

                    [root@localhost~]#/sbin/service vncserver start

啟動失敗的原因有:

                      1./etc/sysconfig/vncservers檔案參數不對

                      2.沒有為vncservers提到的使用者生成各自的配置檔案

                      3.在生成配置檔案後,沒有使用指令vncserver -kill :1 将那個程序殺死

3. 最後,需要配置一下防火牆,允許VNC用戶端連接配接VNC server。

VNC server監聽的端口從5900開始,display:1的監聽5901,display:2監聽5902,以此類推。CentOs的防火牆預設是不允許連接配接這些端口的,是以需要使用下面的步驟打開防火牆(需要root權限):

vi /etc/sysconfig/iptables  

vi /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

這句話的含義是,允許其它機器通路本機的5900到5903端口,這樣,display:1, display:2, display:3的使用者就可以連接配接到本機。

然後使用root身份重新啟動防火牆:

                   [root@localhost~]#/sbin/service iptables restart

好了,現在就可以運作用戶端軟體,連接配接到VNC server上了。

VNC用戶端軟體很多,在linux下有vncviewer,KDE還提供了一個krdc(它的菜單項就是”Remote Desktop Connection”,遠端桌面連接配接)。

在window也有不少vnc用戶端,你可以到http://www.realvnc.com/去下載下傳一個,安裝就可以用了。

假設VNC server的IP位址是192.168.1.1,display是:1。在VNC viewer的server欄中輸入:“192.168.1.1:1”,然後連接配接。OK,你可以看到自己的桌面了。

a、在linux下,運作vncviewer指令即可,伺服器位址的寫法形如192.168.3.119:1

b、在windows下,運作windows版本的vncviewer即可,用法與linux下相近。

c、用浏覽器(平台無關),作為java applet來實作,以形如http://192.168.3.119:5801的方式來啟動   (vnc端口從5800開始依次類推,一般會是5800,5900)

4.設定vncserver為自動啟動服務

如果server重新開機了,那你就需要重新運作一次vncserver指令來啟動VNC server,這很麻煩。有沒有更好的方法呢?

有!我們可以把VNC server啟動成背景服務。四種方法,我使用的是第三種方法C:

A方法:

執行如下步驟:

首先要允許VNC server在系統啟動過程中被啟動。這可以通過“系統設定–>伺服器設定–>服務”菜單來配置,把vncserver一項選上就可以了。

B方法:

如果使用指令行的話,以root身份運作以下兩條指令:

cd    / etc / rc5 . d  

cd / etc / rc5 . dmv K35vncserver S35vncserver

編輯好這個檔案後,儲存,然後以root身份運作:

以後每次系統重新開機時,都會自動啟動這三個使用者的vncserver。

注意:上面三個使用者必須已經使用vncpasswd指令設定過vnc密碼,不然他的vncserver啟動會失敗!

C方法:

            [root@localhost ~]# chkconfig vncserver on

D方法:

           把這行指令/etc/init.d/vncserver start加到/etc/rc.local啟動項裡。

附件:

一、檢視vnc啟動情況

[root@localhost ~]# netstat -tulnp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address             Foreign Address          State    PID/Program name

tcp        0    0

0.0.0.0

:5801            0.0.0.0:*                LISTEN    5248/Xvnc

:5901              0.0.0.0:*                LISTEN    5248/Xvnc

0.0.0.0:6001

            0.0.0.0:*                LISTEN    5248/Xvnc

tcp        0    0 :::6001                   :::*                     LISTEN    5248/Xvnc

二、使用VNC View遠端通路Linux,總是沒有辦法切換出SCIM輸入法,研究了很久,終于讓我找到了解決方法

1)修改.vnc/xstartup檔案,在最後一行加入:scim -d,意思為打開SCIM輸入法

2)使用VNC View通路Linux系統以後,修改scim輸入法的快捷鍵,因為預設打開輸入法的快捷鍵為ctrl+space,與本地機器上切換輸入法的快捷鍵沖突,系統會先接收到本地的指令,是以沒辦法使用預設快捷鍵打開輸入法。将打開SCIM輸入法的快捷鍵修改為Ctrl+Alt+Space。

繼續閱讀