CentOS 6.3下 VNC Server 的配置

來源:互聯網
上載者:User

作業系統: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    @localtigervnc-server.x86_64                 1.0.90-0.10.20100115svn3945.el6    @local

就代表已經存在,否則就通過

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

的命令進行安裝。

單使用者root配置方法:

1. 啟動 VNC Server,這時會要求你輸入兩次進行串連的初始密碼。

[root@localhost ~]# vncserverYou will require a password to access your desktops.Password: //輸入密碼Verify: //再次輸入密碼xauth: creating new authority file /root/.XauthorityNew ‘localhost.localdomain:1 (root)’ desktop is localhost.localdomain:1Creating default startup script /root/.vnc/xstartupStarting applications specified in /root/.vnc/xstartupLog 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_MANAGERexec /etc/X11/xinit/xinitrc

3. 重啟動 VNC Server

[root@localhost ~]# vncserver -kill :1Killing Xvnc process ID 21198[root@localhost ~]# vncserverNew ‘localhost.localdomain:1 (root)’ desktop is localhost.localdomain:1Starting applications specified in /root/.vnc/xstartupLog 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 restartFlushing 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.