Total Pageviews

Monday 29 July 2013

Using VNC to Remote Control Linux from Mac

从Mac OS X中远程控制Linux非常简单,在Mac中无需安装任何软件,当然Linux  server需要安装VNC服务。
环境概述:
Linux:Oracle Enterprise Linux 5.4
Mac:Mac OS X Lion(实际上Snow Leopard或者Leopard应该都是可以的)
VNC Server:Real VNC Free Edition
1. 在OEL中安装Real VNC Server
rpm -Uvh vnc-4_1_3-x86_linux.rpm
2. link X11字体文件位置,否则VNC启动会报错
# cd /usr/X11R6/lib
# ln -s /usr/share/X11 X11
# ls -l
total 4
lrwxrwxrwx 1 root root   14 Sep  5 18:10 X11 -> /usr/share/X11
drwxr-xr-x 3 root root 4096 Aug 31 15:46 modules
如果不执行以上命令,可能在启动VNC以后,在log文件中会看到如下报错:
Couldn't open RGB_DB '/usr/X11R6/lib/X11/rgb'
 
Fatal server error:
could not open default font 'fixed'
xsetroot:  unable to open display 'hpserver:1'
vncconfig: unable to open display "hpserver:1"
xterm Xt error: Can't open display: hpserver:1
twm:  unable to open display "hpserver:1"
2. 启动VNC,可以用任意用户启动VNC,建议不要使用root
$ vncserver
 
New 'hpserver:1 (kamus)' desktop is hpserver:1
 
Starting applications specified in /home/kamus/.vnc/xstartup
Log file is /home/kamus/.vnc/hpserver:1.log
3. 检查vnc log文件,确认vnc监听的端口
vi /home/kamus/.vnc/hpserver:1.log
可以在log文件中看到如下字样,这表示通过RFB协议访问VNC服务器应该指向5901,而通过http协议访问则指向5801。
vncext:      Listening for VNC connections on port 5901
vncext:      Listening for HTTP connections on port 5801
4. 确认VNC已经正常启动,注意在log文件中可能存在warning或者报错,我们通过检查端口来确认VNC是否正常启动。
# lsof|grep 5901|grep vnc
Xvnc      20656     kamus    3u     IPv4     206853                  TCP *:5901 (LISTEN)
如果上述命令,没有返回值,则表明VNC没有成功启动。
5. 用Mac连接VNC
Cmd+K快捷键或者Finder->Go->Connect to Server,打开如下界面,输入:vnc://server-ip:port,port就是上面第三步检查的通过RFB协议访问VNC服务器的端口,通常为590x。

但是默认启动的VNC界面是比较简陋的,如下图这样。当然,也完全可以使用。

6. 如果我们想在VNC中使用gnome界面,那么需要修改一下启动vnc的脚本。该脚本通常位于启动VNC的用户Home下的.vnc目录中,也就是跟log文件在一个目录下,文件名为xstartup。
$ vi ~/.vnc/xstartup
添加如下行。
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
修改完毕以后,整个xstartup脚本如下所示。
#!/bin/sh
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
 
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
twm &
然后停止VNC,再重新启动。
$ vncserver -kill :1
Killing Xvnc process ID 21210
$ vncserver
 
New 'hpserver:1 (kamus)' desktop is hpserver:1
 
Starting applications specified in /home/kamus/.vnc/xstartup
Log file is /home/kamus/.vnc/hpserver:1.log
7. 再次从Mac中连接VNC,就可以看到美观的gnome界面了。

如何停止vncserver。
[orat0@hpserver2 ~]$ vncserver -list
 
TigerVNC server sessions:
 
X DISPLAY # PROCESS ID
:1  701
[orat0@hpserver2 ~]$ 
[orat0@hpserver2 ~]$ 
[orat0@hpserver2 ~]$ vncserver -kill :1
Killing Xvnc process ID 701
------------------------------------------- 
参考文章:
How To Setup Linux VNC Server In 3 Minutes

VNC server works similar to Telnet or SSH daemon, except that it allows VNC clients running on diverse OS platforms to remotely access Linux Desktop Manager of the server itself, rather than the TTY text-based console!

As such, most of Windows-to-Linux users prefer using VNC server than SSH daemon, who are generally favor on the vivid Desktop Manager (either KDE or Gnome) than the cold black-and-white TTY console!
For the newbie (as myself), here is my another 3-minute Linux guide about How to setup VNC server in Red Hat Enterprise Linux?

Though this Linux-VNC guide is tested on Redhat Enterprise Linux 4, it served the general guide of installing VNC in Linux distributions.

  1. Locate the vnc-server-4.0-8.1.rpm package from the Red Hat Linux installation CD or download the latest free version of VNC server from Real VNC.
     
  2. Install the rpm package of VNC server:
    rpm -Uvh vnc-server-4.0-8.1.rpm
     
  3. VNC server is using different set of login authentication than the Linux /etc/passwd. Meaning that, you have to create VNC login password for individual Linux login accounts who are granted remote access with VNC client.
    To create VNC login password, just execute vncpasswd command and enter a valid password twice as usual.
     
  4. Unlike VNC server for Windows, each user account can run more than one VNC servers in Linux.
    To start an instance of VNC server, just execute vncserver command, which will create the $HOME/.vnc directory with VNC default startup scripts inside (if $HOME/.vnc is not currently exists).
     
  5. The vncserver command will startup a VNC server that open 3 next available TCP/IP port, starting from 5801, 5901, and 6001 respectively.
    Port 5801 is for VNC client connection over HTTP protocol, port 5901 is for VNC client connection over RFB protocol, and port 6001 is to allows X applications to connect to the VNC server. The lsof or netstat command will able to show the network port opened by Xvnc server.

    By executing vncserver for the second time or execute the vncserver :2 command, this will startup VNC server that bind and listen to port 5802, 5902, and 6002 respectively.

    To connect to Linux VNC server over HTTP protocol, just type walkernews.net:5801 (replace walkernews.net with your VNC server IP/hostname) at any javascripts-enabled web browser, such as Mozilla Firefox, Opera, or Internet Explorer.

    To connect to Linux VNC server over RFB protocol, just type walkernews.net:5901 at the VNC client.
     
  6. To kill the VNC server in Linux, just execute vncserver -kill :1 will kill the first VNC server that bind and listen to port 5801, 5901, and 6001. The vncserver -kill :2 will kill the second VNC server, and so on.

How to get KDE or Gnome Desktop Manager running in Linux VNC server?

Don’t worry, it’s just a 3-seconds Linux trick! Edit the default VNC server startup scripts $HOME/.vnc/xstartup and un-remark the two “critical” lines, as per the scripts suggestion:
#!/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" &
twm &

Isn’t easy to setup and configure Real VNC in Linux, particularly with Red Hat Linux?