VNC介紹及相關命令總結

What is VNCserver?

VNC stands for Virtual Network Computing. It was originally developed by AT&T as a way to administer machines without using the console. If you have usedWindows Terminal Services (RDP), VNC will seem very familiar.

Why use VNCserver?

In Linux, everything can be done from a shell. However, there may be a time when you need to access the machine as if you were at the console.

Getting Started

You will need several things to get started:
  • root privledges
  • VNC client software (tightVNC, you can download it here.)
  • A good password!
As I mentioned above, this example is done with RHEL, which comes standard with VNCserver installed. To start the vncserver simply invoke the following commands:
[root@roswell etc]# service vncserver start
Starting VNC server:                                       [  OK  ]
[root@roswell etc]# 
[root@roswell etc]# vncpasswd
Password: 
Verify: 
[root@roswell etc]#
[root@roswell etc]# vncserver

New 'roswell:1 (root)' desktop is roswell:1

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/roswell:1.log

[root@roswell etc]# 


So what did we do there? First, we started the vncserver service. It may or may not have already been running on your system. Next we set a password to access the VNC desktop. When you set the password, you will not see any characters on the screen, and you must enter the password twice. You will only need to do this the very first time you run vncserver. The password will be saved in the Linux filesystem, and you can change it at any time by invoking thevncpasswd command again. Last, to activate the VNC desktop, we simply invoked thevncserver command. Notice the output; the desktop is named "roswell:1" which can also be replaced via the machines IP address.

Connecting

Assuming you already installed TightVNC or another VNC client, enter the desktop name:



You can replace the server name with an IP address if you are logging in from outside your LAN. Remember, if you are usingNAT port 5900 must be forwarded to your VNCserver.



Upon successful connection, you will be prompted for a password. You will then see a terminal screen that will allow you to execute commands:



VNCserver in Runlevel 5 (KDE or Gnome)

If you are new to linux, running VNC server with a terminal isn't going to do you much good. You might want to have a menu-driven GUI like Windows. No problem. Follow these steps:


First, we are going to assume that VNCserver is running under the root user, as shown with the example above. For this example, I will be editing my VNCserver to enter Gnome. You can specify a KDE desktop if you have KDE installed on your server. Make sure you are in the root directory.
[root@roswell ~]# ls -a
.                                   cacti-0.8.6c.tar.gz  .gnome2_private     .lftp                  queue.dat          temp
..                                  client.cfg           .gnupg              machinedependent.dat  .recently-used      Templates
.config                             FAHlog-Prev.txt      .gstreamer-0.8      .metacity             .rhn-applet.cache  .themes
.cshrc                              FAHlog.txt           .gtkrc              .mozilla              .rhn-applet.conf   .thumbnails
.bash_history                       Desktop              .fonts.cache-1      .gtkrc-1.2-gnome2     .rnd               .Trash
.bash_logout                        .dmrc                .gconf              .ICEauthority         .mysql_history      scripts
.bash_profile                       .eggcups             .gconfd             .icons                .nautilus          .sh_history
.viminfo                            .bashrc              .bashdevl           .esd_auth             .gnome              php-4.11
.ssh                                .vnc                  cacti-0.8.6c       .gnome2                install.log
[root@roswell ~]# cd .vnc
[root@roswell .vnc]# ls
passwd         roswell:1.pid  roswell:2.pid  roswell:3.pid  roswell:4.pid  roswell:5.pid             roswell.area51.lan:1.pid
roswell:1.log  roswell:2.log  roswell:3.log  roswell:4.log  roswell:5.log  roswell.area51.lan:1.log  roswell.area51.lan:2.log
xstartup
[root@roswell .vnc]# vi xstartup


Using vi (vim) to edit the xstartup file, make sure your file matches this one:


#!/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 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
startx &


Notice that the last line is "startx &" as this command will launch Gnome upon login via VNCserver. If you are using a KDE desktop, the line "startkde &" should replace the last line.

VNC相關命令:vncconfig vncpasswd vncservervncviewer

1.檢查vnc客戶端(tscleint/vncviewer)和服務器是否已經安裝在你的系統中
# rpm -qa|grep vnc
vnc-4.1.2-14.el5_3.1
vnc-server-4.1.2-14.el5_3.1
如果沒有安裝vncviewer,那麼運行:
# yum install vnc
客戶端我們也可以使用tscleint。

2.配置vncserver
vim /etc/sysconfig/vncservers
增加:VNCSERVERS="1:root"

3.vncpasswd創建一個vnc的口令
注意這將同時在你的home目錄下,創建一個隱藏的目錄.vnc,其中有一個文件passwd保存着你的vnc口令.

4. #service vncserver start

5. # vncserver (第一次啓動時,需要指定一個密碼)
You will require a password to access your desktops.
Password:
Verify:
New 'lpi:1 (root)' desktop is lpi:1
Creating default startup script /root/.vnc/xstartup
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/lpi:1.log

可以以多個用戶身份,啓動多個VNC,共享多個用戶的桌面:
# lsof -i:{5901,5904}
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
Xvnc 2373 root 4u IPv4 6975 TCP *:5901 (LISTEN)
Xvnc 4236 test 4u IPv4 16231 TCP *:5904 (LISTEN)

可以手工指定display#,例如:
# vncserver :99
New 'lpi.123.com:99 (root)' desktop is server01.123.com:99
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/server01.123.com:99.log

6.停止 vncserver(vncserver -kill :display#):
# vncserver -kill :1
Killing Xvnc process ID 2373

7.設置自動運行:ntsysv

8.VNC給瀏覽器的端口是5800+N,給vncviewer的端口是5900+N,N是設置的display號
http://192.168.1.1:5801/
ultravnc:192.168.1.1:5901
# lsof -i:5801,5901
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
Xvnc 31742 root 4u IPv4 51969 TCP *:5901 (LISTEN)
Xvnc 31742 root 5u IPv4 51970 TCP *:5801 (LISTEN)

9.修改VNC密碼
# vncpasswd /root/.vnc/passwd
Password:
Verify:

VNC日誌文件位於:$HOME/.vnc下的log文件,此目錄也包含了一個VNC的xstartup啓動文件。

以GNOME方式顯示,修改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 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
gnome-session &
# tsclient
出現錯誤:unable to connect to host: Connection refused (111)
一般是沒有指定連接的display#,例如地址輸入:192.168.1.1出錯,192.168.1.1:1就正確了
VNCserver auto run
啓動vncserver服務時,出現no displays configured :此錯誤,請編輯vncservers文件
#vim /etc/sysconfig/vncservers
# 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="1:root"
VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -nohttpd -localhost"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章