几天前实践了Linux中VNC的远程连接!今天做一个总结,记录一下实践过程,以备后用和朋友参考!

系统环境: CentOS-5.5-x86_64-bin

其实CentOS5.5本身就安装了 VNCServer 只是默认不是自动启动的

CentOS中也有远程桌面的相关设置,如图:

df585d2c3bd278be8a13998f.jpg


这样设置也可以用windows下的VNCViewer来连接,经测试必须要加连接密码才可以,

最大的问题是:每一次连接都要在CentOS里确认允许连接才可以连得上,


而且当CentOS注销用户时,远程的VNC也被断开了

怎么能让VNC可以在linux注销用户和不登录时也可以连接上呢?

在网上找了很多相关资料:终于解决问题,

下面记录一下我的配置步骤:配置登录用户(/etc/sysconfig/vncservers)-->配置密码(vncpasswd)-->配置登录界面(~/.vnc/xstartup)


在CentOS下安装配置VNC的过程

首先检查vnc客户端和服务器是否已经安装:

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

centos5已包含vnc的安装包

1. 将用户名称加入到配置文件:(注:这里的“用户名”是指linux系统用户的名称)

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

# VNCSERVERS="2:myusername"
# VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -nohttpd -localhost"

VNCSERVERS="2:root"
VNCSERVERARGS[2]="-geometry 800x600"



2.设置用户root的密码

[root@localhost ~]# vncpasswd
Password:
Verify:


3.配置登录界面

[root@localhost]$cd ~/.vnc/

[gavin@centos.vnc]$vi xstartup (vi命令: i 插入 :q 强制退出  :wq! 退出并写入)


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


说明:
在文件中,将"
#xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &
"销掉,增加一行"gnome-session &",表示启动GNOME桌面系统。
当然,也可以改为"startkde &",表示启动KDE桌面系统。


4.重启vncserver

[root@localhost ~]# service vncserver restart 或 /sbin/service vncserver start
关闭 VNC 服务器:2:root                                    [确定]
启动 VNC 服务器:2:root
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

                                                          [确定]


5.Windows登陆到VNCServer

查看连接:

[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 127.0.0.1:2208              0.0.0.0:*                   LISTEN      3600/hpiod        
tcp        0      0 0.0.0.0:5802                0.0.0.0:*                   LISTEN      7821/Xvnc          
tcp        0      0 0.0.0.0:5901                0.0.0.0:*                   LISTEN      4742/vino-server  
tcp        0      0 0.0.0.0:5902                0.0.0.0:*                   LISTEN      7821/Xvnc          
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      3351/portmap      
tcp        0      0 0.0.0.0:10000               0.0.0.0:*                   LISTEN      3950/perl          
tcp        0      0 0.0.0.0:6002                0.0.0.0:*                   LISTEN      7821/Xvnc          
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      3627/cupsd        
tcp        0      0 0.0.0.0:1021                0.0.0.0:*                   LISTEN      3383/rpc.statd    
tcp        0      0 127.0.0.1:2207              0.0.0.0:*                   LISTEN      3605/python        
tcp        0      0 :::80                       :::*                        LISTEN      3699/httpd        
tcp        0      0 :::6002                     :::*                        LISTEN      7821/Xvnc          
tcp        0      0 :::22                       :::*                        LISTEN      3618/sshd          


(1).从IE登录的方法

直接从IE浏览器中输入如下地址:

http://xxx.xxx.xxx.xxx:5802

输入密码即可使用。

(2).从VNCview登录的方法

在windows上安装vncview,然后输入xxx.xxx.xxx.xxx:2,连接登录,输入密码即可。

备注:如果从vncview登录时,提示connectionrefused(10061),则是因为linux防火墙的问题,登录centos系统,选择System——>Preferences——>RemoteDesktop,勾选sharing中的两项,Security两项不勾,点击关闭,然后再重新用vncview登录即可。



端口连接的问题 用IEhttp://IP:5802

用VNCVIEWER IP:2来连接


温习以下知识:

其中遇到了的问题和忘记的问题是:

vi 忘记怎么用了

i 插入字符
:q! 强制退出,修改结果不写入文件
:wq! 强制退出,修改结果写入文件

VNC的意思

cd ~/.vnc 来找到配置文件 xstartup

需要记的是

查看端口命令: netsat -tulnp

重启服务完整命令: /sbin/service vncserver restart

这里的 /sbin 就相当于 windows 下的system32 吧



附注:

设置vncserver启动脚本(/etc/init.d/vncserver)实现系统自启动时启动vncserver.
# 在/etc/rc.d/rc.local文件中加入下面一行
/etc/init.d/vncserver start

#也可以用chkconfig命令实现系统自启动时启动vncserver
[root@localhost ~]# chkconfig --level 2345 vncserver on