linux VNCserver配置

来源:互联网 发布:s. 忒修斯之船 知乎 编辑:程序博客网 时间:2024/04/29 15:59

在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 的 ID 号。

(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
版权所有 © 转载时必须以链接形式注明作者和原始出处!

原创粉丝点击