linux VNCserver配置

來源:互聯網
上載者:User

在Linux作業系統最流行的圖形化操作軟體是VNC,正如windows下的mstsc遠端桌面,在大多數 Linux 發行版都帶了 VNC Server 的發行包,通過rpm -qa|grep vnc檢查,若沒有,下載rpm包進行安裝即可.

1.[root@localhost ~]# rpm -qa|grep vnc
vnc-4.1.2-14.el5_3.1
vnc-server-4.1.2-14.el5_3.1

2.啟動伺服器端的VNC 服務

[root@localhost ~]# /etc/init.d/vncserver start
Starting VNC server:                                       [  OK  ]

3.運行 vncserver 命令,如果第一次配置 VNC Server,會要求提供登入 VNC 使用的密碼。以後也可以使用 vncpasswd 來修改密碼。

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

Password:
Verify:

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

4.修改設定檔 /root/.vnc/xstartup

[root@localhost ~]# cat /root/.vnc/xstartup ,如下兩行的注釋去掉.
#!/bin/sh

# Uncomment the following two lines for normal desktop:
 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 -geometry 80×24+10+10 -ls -title “$VNCDESKTOP Desktop” &
twm &

5.在 Windows 使用 VNC Viewer 登入了

輸入伺服器位址:ip:1
然後提示輸入密碼,輸入你剛才設定的密碼就好了.
注意:IP 位址後面的 :1 的意思是 Linux 上面 VNC 設定的 Display No. 每運行一個 vncserver 就會多建立一個 Display,Display No 也就會加1。

6.在 SSH終端中輸入 vncserver,讓 VNC 開啟一個新的 Display

[root@localhost ~]# vncserver              

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

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/localhost.localdomain:2.log

附一些常見的問題:

VNC Viewer:http://download.csdn.net/source/1080853

以後如果伺服器重新啟動過,就要重新輸入 vncserver 後才能登入。如果伺服器一直沒有重啟過,就不用了。如果要殺掉 vncserver 進程,可執行 vncserver :1 kill ,1表示 display 的 識別碼。

(1) 重設VNC密碼
[root@localhost ~]# vncpasswd
Password:
Verify:
(2) 啟動和kill vncserver
[root@localhost ~]# vncserver :23
 
New ‘localhost.localdomain:23 (root)’ desktop is localhost.localdomain:23

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/localhost.localdomain:23.log

如下:kill  display 的 ID 1,1的pid是:6653
[root@localhost ~]# ps -auxwf|grep vnc|grep -v grep
Warning: bad syntax, perhaps a bogus ‘-’? See /usr/share/doc/procps-3.2.7/FAQ
root      6653  0.0  1.3  13660  6836 pts/0    S    04:44   0:00 Xvnc :1 -desktop localhost.localdomain:1 (root) -httpd /usr/share/vnc/classes -auth /root/.Xauthority -geometry 1024×768 -depth 16 -rfbwait 30000 -rfbauth /root/.vnc/passwd -rfbport 5901 -pn
root      6661  0.0  0.2   4276  1304 pts/0    S    04:44   0:00 vncconfig -iconic
root      6716  0.1  2.1  20972 11276 pts/0    S    04:51   0:01 Xvnc :2 -desktop localhost.localdomain:2 (root) -httpd /usr/share/vnc/classes -auth /root/.Xauthority -geometry 1024×768 -depth 16 -rfbwait 30000 -rfbauth /root/.vnc/passwd -rfbport 5902 -pn
root      6936  0.4  2.1  20856 11116 pts/0    S    05:06   0:00 Xvnc :23 -desktop localhost.localdomain:23 (root) -httpd /usr/share/vnc/classes -auth /root/.Xauthority -geometry 1024×768 -depth 16 -rfbwait 30000 -rfbauth /root/.vnc/passwd -rfbport 5923 -pn
[root@localhost ~]# kill -9 6653
[root@localhost ~]# ps -auxwf|grep vnc|grep -v grep
Warning: bad syntax, perhaps a bogus ‘-’? See /usr/share/doc/procps-3.2.7/FAQ
root      6716  0.1  2.1  20972 11276 pts/0    S    04:51   0:01 Xvnc :2 -desktop localhost.localdomain:2 (root) -httpd /usr/share/vnc/classes -auth /root/.Xauthority -geometry 1024×768 -depth 16 -rfbwait 30000 -rfbauth /root/.vnc/passwd -rfbport 5902 -pn
root      6936  0.3  2.1  20856 11116 pts/0    S    05:06   0:00 Xvnc :23 -desktop localhost.localdomain:23 (root) -httpd /usr/share/vnc/classes -auth /root/.Xauthority -geometry 1024×768 -depth 16 -rfbwait 30000 -rfbauth /root/.vnc/passwd -rfbport 5923 -pn

當然,vncserver -kill :2也可以了,
[root@localhost ~]# vncserver -kill :2
Killing Xvnc process ID 6716
[root@localhost ~]# ps -auxwf|grep vnc|grep -v grep
Warning: bad syntax, perhaps a bogus ‘-’? See /usr/share/doc/procps-3.2.7/FAQ
root      6936  0.2  2.1  20856 11116 pts/0    S    05:06   0:00 Xvnc :23 -desktop localhost.localdomain:23 (root) -httpd /usr/share/vnc/classes -auth /root/.Xauthority -geometry 1024×768 -depth 16 -rfbwait 30000 -rfbauth /root/.vnc/passwd -rfbport 5923 -pn

(3)重新啟動一個手動kill的vncserver

[root@localhost ~]# vncserver :1

Warning: localhost.localdomain:1 is taken because of /tmp/.X1-lock
Remove this file if there is no X server localhost.localdomain:1
A VNC server is already running as :1
[root@localhost ~]# rm -rf /tmp/.X1-lock
[root@localhost ~]# vncserver :1        

Warning: localhost.localdomain:1 is taken because of /tmp/.X11-unix/X1
Remove this file if there is no X server localhost.localdomain:1
A VNC server is already running as :1
[root@localhost ~]# rm -rf /tmp/.X11-unix/X1
[root@localhost ~]# vncserver :1           

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

[root@localhost ~]#
[root@localhost ~]# ps -auxwf|grep vnc|grep -v grep
Warning: bad syntax, perhaps a bogus ‘-’? See /usr/share/doc/procps-3.2.7/FAQ
root      6936  0.0  2.4  22296 12680 pts/0    S    05:06   0:00 Xvnc :23 -desktop localhost.localdomain:23 (root) -httpd /usr/share/vnc/classes -auth /root/.Xauthority -geometry 1024×768 -depth 16 -rfbwait 30000 -rfbauth /root/.vnc/passwd -rfbport 5923 -pn
root      7337  1.0  2.1  20252 10856 pts/0    S    05:19   0:00 Xvnc :1 -desktop localhost.localdomain:1 (root) -httpd /usr/share/vnc/classes -auth /root/.Xauthority -geometry 1024×768 -depth 16 -rfbwait 30000 -rfbauth /root/.vnc/passwd -rfbport 5901 -pn

說明:因是手動刪除的,所以也要把.lock刪除掉,才能重新啟動了.

(4).VNC服務使用的連接埠號碼與案頭號的關係

    VNC服務使用的連接埠號碼與案頭號相關,VNC使用TCP連接埠從5900開始,對應關係如下

    案頭號為“1”  —- 連接埠號碼為5901

    案頭號為“2”  —- 連接埠號碼為5902

    案頭號為“3”  —- 連接埠號碼為5903

    ……

    基於Java的VNC客戶程式Web服務TCP連接埠從5800開始,也是與案頭號相關,對應關係如下

    案頭號為“1”  —- 連接埠號碼為5801

    案頭號為“2”  —- 連接埠號碼為5802

    案頭號為“3”  —- 連接埠號碼為5803

    ……

    基於上面的介紹,如果Linux開啟了防火牆功能,就需要手工開啟相應的連接埠,以開啟案頭號為“1”相應的連接埠為例,命令如下

    [root@localhost~]# iptables -I INPUT -p tcp –dport 5901 -j ACCEPT

    [root@localhost ~]# iptables -I INPUT -p tcp –dport 5801 -j ACCEPT

(5)配置VNC圖形案頭環境為KDE或GNOME案頭環境

[root@localhost ~]# cat /root/.vnc/xstartup
#!/bin/sh

# Uncomment the following two lines for normal desktop:
 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 -geometry 80×24+10+10 -ls -title “$VNCDESKTOP Desktop” &
twm &

將這個xstartup檔案的最後一行修改為“startkde &”,再重新啟動vncserver服務後就可以登陸到KDE案頭環境

將這個xstartup檔案的最後一行修改為“gnome-session &”,再重新啟動vncserver服務後就可以登陸到GNOME案頭環境

重新啟動vncserver服務的方法:

[root@localhost ~]# vncserver -kill :1

[root@localhost ~]# vncserver :1

(6) 配置多個案頭

    可以使用如下的方法啟動多個案頭的VNC

    vncserver :1

    vncserver :2

    ……

    但是這種手工啟動的方法在伺服器重新啟動之後將失效,因此,下面介紹如何讓系統自動管理多個案頭的VNC,方法是將需要自動管理的資訊添加到/etc/sysconfig/vncservers設定檔中,
    先以案頭1為test使用者案頭2為test1使用者為例進行配置如下:

    格式為:VNCSERVERS=”案頭號:使用的使用者名稱 案頭號:使用的使用者名稱”

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

    VNCSERVERS=”1:test 2:test1″

    VNCSERVERARGS[1]=”-geometry 1024×768″

    VNCSERVERARGS[2]=”-geometry 1024×768″

(7) 設定VNC服務隨系統啟動自動載入

 第一種方法:使用“ntsysv”命令啟動圖形化服務配置程式,在vncserver服務前加上星號,點擊確定,配置完成。

 第二種方法:使用“chkconfig”在命令列模式下進行操作,命令使用如下:

    [root@localhost ~]# chkconfig vncserver on

    [root@testdb ~]# chkconfig –list vncserver

    vncserver       0:off   1:off   2:on    3:on    4:on    5:on    6:off

 第三種方法:把這行命令/etc/init.d/vncserver start加到/etc/rc.local啟動項裡。

文章作者:孫劍和
本文地址:http://www.sunjianhe.com/?p=685
著作權 轉載時必須以連結形式註明作者和原始出處!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.