歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> CentOS 6.3下 VNC Server 的配置

CentOS 6.3下 VNC Server 的配置

日期:2017/3/3 16:16:31   编辑:關於Linux

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

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

[root@localhost ~]# yum install "*vnc*" pixman pixman-devel libXfont -y

的命令進行安裝。

單用戶root配置方法:

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

[root@localhost ~]# vncserver
You will require a password to access your desktops.

Password: //輸入密碼
Verify: //再次輸入密碼
xauth: creating new authority file /root/.Xauthority

New ‘localhost.localdomain:1 (root)’ desktop is localhost.localdomain:1

Creating default startup script /root/.vnc/xstartup
Starting applications specified in /root/.vnc/xstartup
Log 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_MANAGER

exec /etc/X11/xinit/xinitrc

-------------------------

注:若啟動黑屏則添加如下腳本內容

-------------------------------

#[-x /etc/vnc/xstartup]]&& exec /etc/vnc/xstartup

[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources

xsetroot -solid grey

vncconfig -iconic &

xterm -gemetry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &

#twm &

gnome-session &

-------------------------------

3. 重啟動 VNC Server

[root@localhost ~]# vncserver -kill :1
Killing Xvnc process ID 21198

[root@localhost ~]# vncserver

New ‘localhost.localdomain:1 (root)’ desktop is localhost.localdomain:1

Starting applications specified in /root/.vnc/xstartup
Log 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 restart
Flushing 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:user1 2:user2 3: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

Copyright © Linux教程網 All Rights Reserved