在CentOS4.4配置VNC
时间:2006-11-28 来源:anima
在CentOS4.4配置VNC
VNC官方网站:
http://www.realvnc.com/
关于在VNC远程桌面使用Fcitx五笔输入法:
当在VNC远程桌面激活了Fcitx的五笔输入之后,可通过按 Ctrl 键在Fcitx的五笔和英文输入之间切换。
[root@fish lwg]# rpm -ivh media/disk8/softs/RealVNC/vnc-4_1_2-x86_linux.rpm
準備中... ########################################### [100%]
1:vnc ########################################### [100%]
Checking for perl... [OK]
Checking for uname... [OK]
Checking for xauth... [OK]
[root@fish lwg]#
首次运行 vncserver,并设置连接密码:
[lwg@fish ~]$ vncserver
You will require a password to access your desktops.
Password:
Verify:
New 'fish:1 (lwg)' desktop is fish:1
Creating default startup script /home/lwg/.vnc/xstartup
Starting applications specified in /home/lwg/.vnc/xstartup
Log file is /home/lwg/.vnc/fish:1.log
运行完vncserver后,就会在主目录生成 .vnc 目录,在该目录中有一个重要的配置文件是xstartup。
主控端的使用方法:
vncviewer remotehost:id
其中id为远程桌面号。
运行主控端 vncviewer,桌面号为1:
[lwg@fish ~]$ vncviewer fish:1
VNC Viewer Free Edition 4.1.2 for X - built May 12 2006 17:42:13
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.
Tue Nov 28 21:36:13 2006
CConn: connected to host fish port 5901
CConnection: Server supports RFB protocol version 3.8
CConnection: Using RFB protocol version 3.8
Tue Nov 28 21:36:18 2006
TXImage: Using default colormap and visual, TrueColor, depth 24.
CConn: Using pixel format depth 6 (8bpp) rgb222
CConn: Using ZRLE encoding
CConn: Throughput 3725 kbit/s - changing to hextile encoding
CConn: Throughput 3725 kbit/s - changing to full colour
CConn: Using pixel format depth 24 (32bpp) little-endian rgb888
CConn: Using hextile encoding
[lwg@fish ~]$
按F8键可以弹出菜单,选择取出vncviewer。
结束VNC服务,再次运行vncserver:
[lwg@fish ~]$ vncserver
New 'fish:1 (lwg)' desktop is fish:1
Starting applications specified in /home/lwg/.vnc/xstartup
Log file is /home/lwg/.vnc/fish:1.log
[lwg@fish ~]$
服务的启动打开了几个进程:
Xvnc (ID: 4894)
vncconfig (ID: 4899)
xterm (ID: 4900)
\──── -bash (ID: 4903) (xterm的子进程)
twm (ID: 4901)
VNC默认启动的X-windows是twm,很不好看,配置在xstartup。
[lwg@fish ~]$ cat .vnc/xstartup
#!/bin/sh
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
twm &
先把原始配置备份好:
[lwg@fish ~]$ cp .vnc/xstartup .vnc/xstartup.backup
再修改xstartup配置文件:
[lwg@fish ~]$ vim .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 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &
然后重新启动vncserver服务:
[lwg@fish ~]$ vncserver -kill:1
New 'fish:2 (lwg)' desktop is fish:2
Starting applications specified in /home/lwg/.vnc/xstartup
Log file is /home/lwg/.vnc/fish:2.log
[lwg@fish ~]$ vncserver
New 'fish:2 (lwg)' desktop is fish:2
Starting applications specified in /home/lwg/.vnc/xstartup
Log file is /home/lwg/.vnc/fish:2.log
[lwg@fish ~]$ vncviewer fish:2
VNC Viewer Free Edition 4.1.2 for X - built May 12 2006 17:42:13
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.
Tue Nov 28 22:13:13 2006
CConn: connected to host fish port 5902
CConnection: Server supports RFB protocol version 3.8
Tue Nov 28 22:13:14 2006
CConnection: Using RFB protocol version 3.8
Tue Nov 28 22:13:19 2006
TXImage: Using default colormap and visual, TrueColor, depth 24.
CConn: Using pixel format depth 6 (8bpp) rgb222
CConn: Using ZRLE encoding
[lwg@fish ~]$ vncviewer fish:1
VNC Viewer Free Edition 4.1.2 for X - built May 12 2006 17:42:13
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.
Tue Nov 28 22:18:31 2006
CConn: connected to host fish port 5901
Tue Nov 28 22:18:32 2006
CConnection: Server supports RFB protocol version 3.8
CConnection: Using RFB protocol version 3.8
Tue Nov 28 22:18:37 2006
TXImage: Using default colormap and visual, TrueColor, depth 24.
CConn: Using pixel format depth 6 (8bpp) rgb222
CConn: Using ZRLE encoding
Tue Nov 28 22:18:38 2006
CConn: Throughput 6129 kbit/s - changing to hextile encoding
CConn: Throughput 6129 kbit/s - changing to full colour
CConn: Using pixel format depth 24 (32bpp) little-endian rgb888
CConn: Using hextile encoding
[lwg@fish ~]$
上面的情形没有结束原来的vncserver,还新增了一个新vncserver进程,是因为 -kill 参数后面没有留空格。
下面修正:
[lwg@fish ~]$ vncserver -kill :1
Killing Xvnc process ID 4894
[lwg@fish ~]$ vncserver -kill :2
Killing Xvnc process ID 4960
[lwg@fish ~]$ vncserver
New 'fish:1 (lwg)' desktop is fish:1
Starting applications specified in /home/lwg/.vnc/xstartup
Log file is /home/lwg/.vnc/fish:1.log
[lwg@fish ~]$ vncviewer fish:1
界面还是不好看,下面再次修改xstartup配置:
[lwg@fish ~]$ vim .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 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &
bash &
gnome-session &
fcitx &
[lwg@fish ~]$ vncserver -kill :1
Killing Xvnc process ID 5808
[lwg@fish ~]$ vncserver :1 -geometry 1024x768 -depth 24
[lwg@fish ~]$ vncviewer fish:1
好啦!(1024x768中的x为字母x)
初次启动vncserver时,已经设置了连接密码,下面命令可以用来修改连接密码:
[lwg@fish ~]$ vncpasswd
Password:
Verify:
********
如果要在用户登录时就加载vncserver服务,可以在~/.bash_profile加入下面这行:
vncserver :1 -geometry 1024x768 -depth 24 &
********
最后设置防火墙端口5800。
如果需要从远程主机启动vncserver服务,可先使用ssh远程登录VNC服务器,再通过ssh运行vncserver服务。
要使用这种方式启动vncserver服务,必须在VNC服务器的 /etc/hosts.allow文件中添加下面两行:
sshd:192.168.1.7:allow
sshd:all:deny
并允许ssh通过防火墙,以便只允许192.168.1.7主机可以通过ssh登录VNC服务器。
如果希望对每个用户生成xstart的时候都像上面那样,那么可以直接修改vncserer命令了,这是一个perl脚本。
找到第34行,修改成下面这个样子:
$defaultXStartup=(
#!/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 &
bash &
gnome-session &
fcitx &
);
原始的 /usr/bin/vncserver 命令的内容:
#!/usr/bin/env perl
#
# Copyright (C) 2002-2005 RealVNC Ltd.
# Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
#
# vncserver - wrapper script to start an X VNC server.
#
#
# First make sure we're operating in a sane environment.
#
&SanityCheck();
#
# Global variables. You may want to configure some of these for your site.
#
$geometry = "1024x768";
$depth = 16;
$vncJavaFiles = (((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") ||
((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes"));
$vncUserDir = "$ENV{HOME}/.vnc";
$xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority";
$defaultXStartup
= ("#!/bin/sh\n\n".
"[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n".
"xsetroot -solid grey\n".
"vncconfig -iconic &\n".
"xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
"twm &\n");
chop($host = `uname -n`);
# Check command line options
&ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
"-help",0,"-h",0,"--help",0);
&Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
&Kill() if ($opt{'-kill'});
# Uncomment this line if you want default geometry, depth and pixelformat
# to match the current X display:
# &GetXDisplayDefaults();
if ($opt{'-geometry'}) {
$geometry = $opt{'-geometry'};
}
if ($opt{'-depth'}) {
$depth = $opt{'-depth'};
$pixelformat = "";
}
if ($opt{'-pixelformat'}) {
$pixelformat = $opt{'-pixelformat'};
}
&CheckGeometryAndDepth();
# Create the user's vnc directory if necessary.
if (!(-e $vncUserDir)) {
if (!mkdir($vncUserDir,0755)) {
die "$prog: Could not create $vncUserDir.\n";
}
}
# Make sure the user has a password.
($z,$z,$mode) = stat("$vncUserDir/passwd");
if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
warn "\nYou will require a password to access your desktops.\n\n";
system("vncpasswd -q $vncUserDir/passwd");
if (($? >> 8) != 0) {
exit 1;
}
}
# Find display number.
if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
$displayNumber = $1;
shift(@ARGV);
if (!&CheckDisplayNumber($displayNumber)) {
die "A VNC server is already running as :$displayNumber\n";
}
} elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/)) {
&Usage();
} else {
$displayNumber = &GetDisplayNumber();
}
$vncPort = 5900 + $displayNumber;
$desktopLog = "$vncUserDir/$host:$displayNumber.log";
unlink($desktopLog);
# Make an X server cookie - use as the seed the sum of the current time, our
# PID and part of the encrypted form of the password. Ideally we'd use
# /dev/urandom, but that's only available on Linux.
srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
$cookie = "";
for (1..16) {
$cookie .= sprintf("%02x", int(rand(256)) % 256);
}
system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
if ($opt{'-name'}) {
$desktopName = $opt{'-name'};
} else {
$desktopName = "$host:$displayNumber ($ENV{USER})";
}
# Now start the X VNC Server
$cmd = "Xvnc :$displayNumber";
$cmd .= " -desktop " . "edString($desktopName);
$cmd .= " -httpd $vncJavaFiles" if ($vncJavaFiles);
$cmd .= " -auth $xauthorityFile";
$cmd .= " -geometry $geometry" if ($geometry);
$cmd .= " -depth $depth" if ($depth);
$cmd .= " -pixelformat $pixelformat" if ($pixelformat);
$cmd .= " -rfbwait 30000";
$cmd .= " -rfbauth $vncUserDir/passwd";
$cmd .= " -rfbport $vncPort";
$cmd .= " -pn";
# Add font path and color database stuff here, e.g.:
#
# $cmd .= " -fp /usr/lib/X11/fonts/misc/,/usr/lib/X11/fonts/75dpi/";
# $cmd .= " -co /usr/lib/X11/rgb";
#
foreach $arg (@ARGV) {
$cmd .= " " . "edString($arg);
}
$cmd .= " >> " . "edString($desktopLog) . " 2>&1";
# Run $cmd and record the process ID.
$pidFile = "$vncUserDir/$host:$displayNumber.pid";
system("$cmd & echo \$! >$pidFile");
# Give Xvnc a chance to start up
sleep(3);
warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
# Create the user's xstartup script if necessary.
if (!(-e "$vncUserDir/xstartup")) {
warn "Creating default startup script $vncUserDir/xstartup\n";
open(XSTARTUP, ">$vncUserDir/xstartup");
print XSTARTUP $defaultXStartup;
close(XSTARTUP);
chmod 0755, "$vncUserDir/xstartup";
}
# Run the X startup script.
warn "Starting applications specified in $vncUserDir/xstartup\n";
warn "Log file is $desktopLog\n\n";
# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
# TCP (DISPLAY=host:n)
if (-e "/tmp/.X11-unix/X$displayNumber" ||
-e "/usr/spool/sockets/X11/$displayNumber")
{
$ENV{DISPLAY}= ":$displayNumber";
} else {
$ENV{DISPLAY}= "$host:$displayNumber";
}
$ENV{VNCDESKTOP}= $desktopName;
system("$vncUserDir/xstartup >> " . "edString($desktopLog) . " 2>&1 &");
exit;
###############################################################################
#
# CheckGeometryAndDepth simply makes sure that the geometry and depth values
# are sensible.
#
sub CheckGeometryAndDepth
{
if ($geometry =~ /^(\d+)x(\d+)$/) {
$width = $1; $height = $2;
if (($width<1) || ($height<1)) {
die "$prog: geometry $geometry is invalid\n";
}
while (($width % 4)!=0) {
$width = $width + 1;
}
while (($height % 2)!=0) {
$height = $height + 1;
}
$geometry = "${width}x$height";
} else {
die "$prog: geometry $geometry is invalid\n";
}
if (($depth < 8) || ($depth > 32)) {
die "Depth must be between 8 and 32\n";
}
}
#
# GetDisplayNumber gets the lowest available display number. A display number
# n is taken if something is listening on the VNC server port (5900+n) or the
# X server port (6000+n).
#
sub GetDisplayNumber
{
foreach $n (1..99) {
if (&CheckDisplayNumber($n)) {
return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
}
}
die "$prog: no free display number on $host.\n";
}
#
# CheckDisplayNumber checks if the given display number is available. A
# display number n is taken if something is listening on the VNC server port
# (5900+n) or the X server port (6000+n).
#
sub CheckDisplayNumber
{
local ($n) = @_;
socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
close(S);
return 0;
}
close(S);
socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
close(S);
return 0;
}
close(S);
if (-e "/tmp/.X$n-lock") {
warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
warn "Remove this file if there is no X server $host:$n\n";
return 0;
}
if (-e "/tmp/.X11-unix/X$n") {
warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
warn "Remove this file if there is no X server $host:$n\n";
return 0;
}
if (-e "/usr/spool/sockets/X11/$n") {
warn("\nWarning: $host:$n is taken because of ".
"/usr/spool/sockets/X11/$n\n");
warn "Remove this file if there is no X server $host:$n\n";
return 0;
}
return 1;
}
#
# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
# format of the current X display being used. If successful, it sets the
# options as appropriate so that the X VNC server will use the same settings
# (minus an allowance for window manager decorations on the geometry). Using
# the same depth and pixel format means that the VNC server won't have to
# translate pixels when the desktop is being viewed on this X display (for
# TrueColor displays anyway).
#
sub GetXDisplayDefaults
{
local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
$red, $green, $blue);
$wmDecorationWidth = 4; # a guess at typical size for window manager
$wmDecorationHeight = 24; # decoration size
return if (!defined($ENV{DISPLAY}));
@lines = `xdpyinfo 2>/dev/null`;
return if ($? != 0);
@matchlines = grep(/dimensions/, @lines);
if (@matchlines) {
($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
$width -= $wmDecorationWidth;
$height -= $wmDecorationHeight;
$geometry = "${width}x$height";
}
@matchlines = grep(/default visual id/, @lines);
if (@matchlines) {
($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
for ($i = 0; $i < @lines; $i++) {
if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
if (($lines[$i+1] !~ /TrueColor/) ||
($lines[$i+2] !~ /depth/) ||
($lines[$i+4] !~ /red, green, blue masks/))
{
return;
}
last;
}
}
return if ($i >= @lines);
($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
($red,$green,$blue)
= ($lines[$i+4]
=~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
$red = hex($red);
$green = hex($green);
$blue = hex($blue);
if ($red > $blue) {
$red = int(log($red) / log(2)) - int(log($green) / log(2));
$green = int(log($green) / log(2)) - int(log($blue) / log(2));
$blue = int(log($blue) / log(2)) + 1;
$pixelformat = "rgb$red$green$blue";
} else {
$blue = int(log($blue) / log(2)) - int(log($green) / log(2));
$green = int(log($green) / log(2)) - int(log($red) / log(2));
$red = int(log($red) / log(2)) + 1;
$pixelformat = "bgr$blue$green$red";
}
}
}
#
# quotedString returns a string which yields the original string when parsed
# by a shell.
#
sub quotedString
{
local ($in) = @_;
$in =~ s/\'/\'\"\'\"\'/g;
return "'$in'";
}
#
# removeSlashes turns slashes into underscores for use as a file name.
#
sub removeSlashes
{
local ($in) = @_;
$in =~ s|/|_|g;
return "$in";
}
#
# Usage
#
sub Usage
{
die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
" [-geometry <width>x<height>]\n".
" [-pixelformat rgbNNN|bgrNNN]\n".
" <Xvnc-options>...\n\n".
" $prog -kill <X-display>\n\n");
}
#
# Kill
#
sub Kill
{
$opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
if ($opt{'-kill'} =~ /^:\d+$/) {
$pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
} else {
if ($opt{'-kill'} !~ /^$host:/) {
die "\nCan't tell if $opt{'-kill'} is on $host\n".
"Use -kill :<number> instead\n\n";
}
$pidFile = "$vncUserDir/$opt{'-kill'}.pid";
}
if (! -r $pidFile) {
die "\nCan't find file $pidFile\n".
"You'll have to kill the Xvnc process manually\n\n";
}
$SIG{'HUP'} = 'IGNORE';
chop($pid = `cat $pidFile`);
warn "Killing Xvnc process ID $pid\n";
system("kill $pid");
unlink $pidFile;
exit;
}
#
# ParseOptions takes a list of possible options and a boolean indicating
# whether the option has a value following, and sets up an associative array
# %opt of the values of the options given on the command line. It removes all
# the arguments it uses from @ARGV and returns them in @optArgs.
#
sub ParseOptions
{
local (@optval) = @_;
local ($opt, @opts, %valFollows, @newargs);
while (@optval) {
$opt = shift(@optval);
push(@opts,$opt);
$valFollows{$opt} = shift(@optval);
}
@optArgs = ();
%opt = ();
arg: while (defined($arg = shift(@ARGV))) {
foreach $opt (@opts) {
if ($arg eq $opt) {
push(@optArgs, $arg);
if ($valFollows{$opt}) {
if (@ARGV == 0) {
&Usage();
}
$opt{$opt} = shift(@ARGV);
push(@optArgs, $opt{$opt});
} else {
$opt{$opt} = 1;
}
next arg;
}
}
push(@newargs,$arg);
}
@ARGV = @newargs;
}
#
# Routine to make sure we're operating in a sane environment.
#
sub SanityCheck
{
local ($cmd);
#
# Get the program name
#
($prog) = ($0 =~ m|([^/]+)$|);
#
# Check we have all the commands we'll need on the path.
#
cmd:
foreach $cmd ("uname","xauth","Xvnc","vncpasswd") {
for (split(/:/,$ENV{PATH})) {
if (-x "$_/$cmd") {
next cmd;
}
}
die "$prog: couldn't find \"$cmd\" on your PATH.\n";
}
#
# Check the HOME environment variable is set
#
if (!defined($ENV{HOME})) {
die "$prog: The HOME environment variable is not set.\n";
}
chdir($ENV{HOME});
#
# Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
# eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
# we just guess at the values. If you find perl moaning here, just
# hard-code the values of AF_INET and SOCK_STREAM. You can find these out
# for your platform by looking in /usr/include/sys/socket.h and related
# files.
#
chop($os = `uname`);
chop($osrev = `uname -r`);
eval 'use Socket';
if ($@) {
eval 'require "sys/socket.ph"';
if ($@) {
if (($os eq "SunOS") && ($osrev !~ /^4/)) {
$AF_INET = 2;
$SOCK_STREAM = 2;
} else {
$AF_INET = 2;
$SOCK_STREAM = 1;
}
} else {
$AF_INET = &AF_INET;
$SOCK_STREAM = &SOCK_STREAM;
}
} else {
$AF_INET = &AF_INET;
$SOCK_STREAM = &SOCK_STREAM;
}
}
vnc下的远程协助
让双方登录到一个桌面 如果与本地运行的X-windows进行远程协助,可以在本地的X-windows打开一个终端,然后执行:
x0vncserver -PasswordFile=/当前用户主目录/.vnc/passwd
如:x0vncserver -PasswordFile=/root/.vnc/passwd
因为远程用户要x0vncserver程序支持,所以不能关闭这个程序。当vnc客户机连接到":0"号桌面时,即与本地用户共享了桌面,实现了远程。
[lwg@fish ~]$ x0vncserver -PasswordFile=.vnc/passwd
[lwg@fish ~]$ x0vncserver --help
usage: x0vncserver [<parameters>]
Parameters can be turned on with -<param> or off with -<param>=0
Parameters which take a value can be specified as -<param> <value>
Other valid forms are <param>=<value> -<param>=<value> --<param>=<value>
Parameter names are case-insensitive. The parameters are:
Global Parameters:
QueryConnectTimeout - Number of seconds to show the Accept Connection dialog
before rejecting the connection (default=10)
rfbport - TCP port to listen for RFB protocol (default=5900)
display - The X display (default=)
Log - Specifies which log output should be directed to which
target logger, and the level of output to log. Format is
<log>:<target>:<level>[, ...]. (default=)
BlacklistTimeout - The initial timeout applied when a host is first
black-listed. The host cannot re-attempt a connection until
the timeout expires. (default=10)
BlacklistThreshold - The number of unauthenticated connection attempts
allowed from any individual host before that host is
black-listed (default=5)
RemapKeys - Comma-separated list of incoming keysyms to remap. Mappings
are expressed as two hex values, prefixed by 0x, and
separated by -> (default=)
QueryConnect - Prompt the local user to accept or reject incoming
connections. (default=0)
SendCutText - Send clipboard changes to clients. (default=1)
AcceptCutText - Accept clipboard updates from clients. (default=1)
AcceptPointerEvents - Accept pointer press and release events from clients.
(default=1)
AcceptKeyEvents - Accept key press and release events from clients.
(default=1)
DisconnectClients - Disconnect existing clients if an incoming connection is
non-shared. If combined with NeverShared then new
connections will be refused while there is a client active
(default=1)
NeverShared - Never treat incoming connections as shared, regardless of
the client-specified setting (default=0)
AlwaysShared - Always treat incoming connections as shared, regardless of
the client-specified setting (default=0)
Protocol3.3 - Always use protocol version 3.3 for backwards compatibility
with badly-behaved clients (default=0)
CompareFB - Perform pixel comparison on framebuffer to reduce
unnecessary updates (default=1)
ClientWaitTimeMillis - The number of milliseconds to wait for a client which
is no longer responding (default=20000)
IdleTimeout - The number of seconds after which an idle VNC connection
will be dropped (zero means no timeout) (default=3600)
Password - Obfuscated binary encoding of the password which clients
must supply to access the server (default=)
PasswordFile - Password file for VNC authentication (default=)
ReverseSecurityTypes - Specify encryption scheme to use for reverse
connections (None) (default=None)
SecurityTypes - Specify which security scheme to use for incoming
connections (None, VncAuth) (default=VncAuth)
ZlibLevel - Zlib compression level (default=-1)
MaxCutText - Maximum permitted length of an incoming clipboard update
(default=262144)
~ImageCleanup called
[lwg@fish ~]$
vnc服务使用端口与桌面相关,vnc服务器使用tcp端口从5900开始。桌面号为1,则端口号为5901,桌面号为2,则端口号为5902。基于Java的vnc客户程序web服务,tcp端口从5800开始。
iptables -I INPUT -p tcp --dport 5901 -j ACCEPT
iptables -I INPUT -p tcp --dport 5801 -j ACCEPT 重启桌面号:
vncserver -kill 1
vncserver :1 启动1号桌面
用户 可能启动多个桌面
vncserver :2
等等,它们之间不受影响。 让系统自动管理桌面,如root的管理1号桌面,lwg管理2号桌面。
修改:vim /etc/sysconfig/vncservers
VNCSERES= "1:root"
VNCSERES="2:lwg"
VNC官方网站:
http://www.realvnc.com/
关于在VNC远程桌面使用Fcitx五笔输入法:
当在VNC远程桌面激活了Fcitx的五笔输入之后,可通过按 Ctrl 键在Fcitx的五笔和英文输入之间切换。
[root@fish lwg]# rpm -ivh media/disk8/softs/RealVNC/vnc-4_1_2-x86_linux.rpm
準備中... ########################################### [100%]
1:vnc ########################################### [100%]
Checking for perl... [OK]
Checking for uname... [OK]
Checking for xauth... [OK]
[root@fish lwg]#
首次运行 vncserver,并设置连接密码:
[lwg@fish ~]$ vncserver
You will require a password to access your desktops.
Password:
Verify:
New 'fish:1 (lwg)' desktop is fish:1
Creating default startup script /home/lwg/.vnc/xstartup
Starting applications specified in /home/lwg/.vnc/xstartup
Log file is /home/lwg/.vnc/fish:1.log
运行完vncserver后,就会在主目录生成 .vnc 目录,在该目录中有一个重要的配置文件是xstartup。
主控端的使用方法:
vncviewer remotehost:id
其中id为远程桌面号。
运行主控端 vncviewer,桌面号为1:
[lwg@fish ~]$ vncviewer fish:1
VNC Viewer Free Edition 4.1.2 for X - built May 12 2006 17:42:13
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.
Tue Nov 28 21:36:13 2006
CConn: connected to host fish port 5901
CConnection: Server supports RFB protocol version 3.8
CConnection: Using RFB protocol version 3.8
Tue Nov 28 21:36:18 2006
TXImage: Using default colormap and visual, TrueColor, depth 24.
CConn: Using pixel format depth 6 (8bpp) rgb222
CConn: Using ZRLE encoding
CConn: Throughput 3725 kbit/s - changing to hextile encoding
CConn: Throughput 3725 kbit/s - changing to full colour
CConn: Using pixel format depth 24 (32bpp) little-endian rgb888
CConn: Using hextile encoding
[lwg@fish ~]$
按F8键可以弹出菜单,选择取出vncviewer。
结束VNC服务,再次运行vncserver:
[lwg@fish ~]$ vncserver
New 'fish:1 (lwg)' desktop is fish:1
Starting applications specified in /home/lwg/.vnc/xstartup
Log file is /home/lwg/.vnc/fish:1.log
[lwg@fish ~]$
服务的启动打开了几个进程:
Xvnc (ID: 4894)
vncconfig (ID: 4899)
xterm (ID: 4900)
\──── -bash (ID: 4903) (xterm的子进程)
twm (ID: 4901)
VNC默认启动的X-windows是twm,很不好看,配置在xstartup。
[lwg@fish ~]$ cat .vnc/xstartup
#!/bin/sh
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
twm &
先把原始配置备份好:
[lwg@fish ~]$ cp .vnc/xstartup .vnc/xstartup.backup
再修改xstartup配置文件:
[lwg@fish ~]$ vim .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 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &
然后重新启动vncserver服务:
[lwg@fish ~]$ vncserver -kill:1
New 'fish:2 (lwg)' desktop is fish:2
Starting applications specified in /home/lwg/.vnc/xstartup
Log file is /home/lwg/.vnc/fish:2.log
[lwg@fish ~]$ vncserver
New 'fish:2 (lwg)' desktop is fish:2
Starting applications specified in /home/lwg/.vnc/xstartup
Log file is /home/lwg/.vnc/fish:2.log
[lwg@fish ~]$ vncviewer fish:2
VNC Viewer Free Edition 4.1.2 for X - built May 12 2006 17:42:13
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.
Tue Nov 28 22:13:13 2006
CConn: connected to host fish port 5902
CConnection: Server supports RFB protocol version 3.8
Tue Nov 28 22:13:14 2006
CConnection: Using RFB protocol version 3.8
Tue Nov 28 22:13:19 2006
TXImage: Using default colormap and visual, TrueColor, depth 24.
CConn: Using pixel format depth 6 (8bpp) rgb222
CConn: Using ZRLE encoding
[lwg@fish ~]$ vncviewer fish:1
VNC Viewer Free Edition 4.1.2 for X - built May 12 2006 17:42:13
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.
Tue Nov 28 22:18:31 2006
CConn: connected to host fish port 5901
Tue Nov 28 22:18:32 2006
CConnection: Server supports RFB protocol version 3.8
CConnection: Using RFB protocol version 3.8
Tue Nov 28 22:18:37 2006
TXImage: Using default colormap and visual, TrueColor, depth 24.
CConn: Using pixel format depth 6 (8bpp) rgb222
CConn: Using ZRLE encoding
Tue Nov 28 22:18:38 2006
CConn: Throughput 6129 kbit/s - changing to hextile encoding
CConn: Throughput 6129 kbit/s - changing to full colour
CConn: Using pixel format depth 24 (32bpp) little-endian rgb888
CConn: Using hextile encoding
[lwg@fish ~]$
上面的情形没有结束原来的vncserver,还新增了一个新vncserver进程,是因为 -kill 参数后面没有留空格。
下面修正:
[lwg@fish ~]$ vncserver -kill :1
Killing Xvnc process ID 4894
[lwg@fish ~]$ vncserver -kill :2
Killing Xvnc process ID 4960
[lwg@fish ~]$ vncserver
New 'fish:1 (lwg)' desktop is fish:1
Starting applications specified in /home/lwg/.vnc/xstartup
Log file is /home/lwg/.vnc/fish:1.log
[lwg@fish ~]$ vncviewer fish:1
界面还是不好看,下面再次修改xstartup配置:
[lwg@fish ~]$ vim .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 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &
bash &
gnome-session &
fcitx &
[lwg@fish ~]$ vncserver -kill :1
Killing Xvnc process ID 5808
[lwg@fish ~]$ vncserver :1 -geometry 1024x768 -depth 24
[lwg@fish ~]$ vncviewer fish:1
好啦!(1024x768中的x为字母x)
初次启动vncserver时,已经设置了连接密码,下面命令可以用来修改连接密码:
[lwg@fish ~]$ vncpasswd
Password:
Verify:
********
如果要在用户登录时就加载vncserver服务,可以在~/.bash_profile加入下面这行:
vncserver :1 -geometry 1024x768 -depth 24 &
********
最后设置防火墙端口5800。
如果需要从远程主机启动vncserver服务,可先使用ssh远程登录VNC服务器,再通过ssh运行vncserver服务。
要使用这种方式启动vncserver服务,必须在VNC服务器的 /etc/hosts.allow文件中添加下面两行:
sshd:192.168.1.7:allow
sshd:all:deny
并允许ssh通过防火墙,以便只允许192.168.1.7主机可以通过ssh登录VNC服务器。
如果希望对每个用户生成xstart的时候都像上面那样,那么可以直接修改vncserer命令了,这是一个perl脚本。
找到第34行,修改成下面这个样子:
$defaultXStartup=(
#!/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 &
bash &
gnome-session &
fcitx &
);
原始的 /usr/bin/vncserver 命令的内容:
#!/usr/bin/env perl
#
# Copyright (C) 2002-2005 RealVNC Ltd.
# Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
#
# vncserver - wrapper script to start an X VNC server.
#
#
# First make sure we're operating in a sane environment.
#
&SanityCheck();
#
# Global variables. You may want to configure some of these for your site.
#
$geometry = "1024x768";
$depth = 16;
$vncJavaFiles = (((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") ||
((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes"));
$vncUserDir = "$ENV{HOME}/.vnc";
$xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority";
$defaultXStartup
= ("#!/bin/sh\n\n".
"[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n".
"xsetroot -solid grey\n".
"vncconfig -iconic &\n".
"xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
"twm &\n");
chop($host = `uname -n`);
# Check command line options
&ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
"-help",0,"-h",0,"--help",0);
&Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
&Kill() if ($opt{'-kill'});
# Uncomment this line if you want default geometry, depth and pixelformat
# to match the current X display:
# &GetXDisplayDefaults();
if ($opt{'-geometry'}) {
$geometry = $opt{'-geometry'};
}
if ($opt{'-depth'}) {
$depth = $opt{'-depth'};
$pixelformat = "";
}
if ($opt{'-pixelformat'}) {
$pixelformat = $opt{'-pixelformat'};
}
&CheckGeometryAndDepth();
# Create the user's vnc directory if necessary.
if (!(-e $vncUserDir)) {
if (!mkdir($vncUserDir,0755)) {
die "$prog: Could not create $vncUserDir.\n";
}
}
# Make sure the user has a password.
($z,$z,$mode) = stat("$vncUserDir/passwd");
if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
warn "\nYou will require a password to access your desktops.\n\n";
system("vncpasswd -q $vncUserDir/passwd");
if (($? >> 8) != 0) {
exit 1;
}
}
# Find display number.
if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
$displayNumber = $1;
shift(@ARGV);
if (!&CheckDisplayNumber($displayNumber)) {
die "A VNC server is already running as :$displayNumber\n";
}
} elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/)) {
&Usage();
} else {
$displayNumber = &GetDisplayNumber();
}
$vncPort = 5900 + $displayNumber;
$desktopLog = "$vncUserDir/$host:$displayNumber.log";
unlink($desktopLog);
# Make an X server cookie - use as the seed the sum of the current time, our
# PID and part of the encrypted form of the password. Ideally we'd use
# /dev/urandom, but that's only available on Linux.
srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
$cookie = "";
for (1..16) {
$cookie .= sprintf("%02x", int(rand(256)) % 256);
}
system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
if ($opt{'-name'}) {
$desktopName = $opt{'-name'};
} else {
$desktopName = "$host:$displayNumber ($ENV{USER})";
}
# Now start the X VNC Server
$cmd = "Xvnc :$displayNumber";
$cmd .= " -desktop " . "edString($desktopName);
$cmd .= " -httpd $vncJavaFiles" if ($vncJavaFiles);
$cmd .= " -auth $xauthorityFile";
$cmd .= " -geometry $geometry" if ($geometry);
$cmd .= " -depth $depth" if ($depth);
$cmd .= " -pixelformat $pixelformat" if ($pixelformat);
$cmd .= " -rfbwait 30000";
$cmd .= " -rfbauth $vncUserDir/passwd";
$cmd .= " -rfbport $vncPort";
$cmd .= " -pn";
# Add font path and color database stuff here, e.g.:
#
# $cmd .= " -fp /usr/lib/X11/fonts/misc/,/usr/lib/X11/fonts/75dpi/";
# $cmd .= " -co /usr/lib/X11/rgb";
#
foreach $arg (@ARGV) {
$cmd .= " " . "edString($arg);
}
$cmd .= " >> " . "edString($desktopLog) . " 2>&1";
# Run $cmd and record the process ID.
$pidFile = "$vncUserDir/$host:$displayNumber.pid";
system("$cmd & echo \$! >$pidFile");
# Give Xvnc a chance to start up
sleep(3);
warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
# Create the user's xstartup script if necessary.
if (!(-e "$vncUserDir/xstartup")) {
warn "Creating default startup script $vncUserDir/xstartup\n";
open(XSTARTUP, ">$vncUserDir/xstartup");
print XSTARTUP $defaultXStartup;
close(XSTARTUP);
chmod 0755, "$vncUserDir/xstartup";
}
# Run the X startup script.
warn "Starting applications specified in $vncUserDir/xstartup\n";
warn "Log file is $desktopLog\n\n";
# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
# TCP (DISPLAY=host:n)
if (-e "/tmp/.X11-unix/X$displayNumber" ||
-e "/usr/spool/sockets/X11/$displayNumber")
{
$ENV{DISPLAY}= ":$displayNumber";
} else {
$ENV{DISPLAY}= "$host:$displayNumber";
}
$ENV{VNCDESKTOP}= $desktopName;
system("$vncUserDir/xstartup >> " . "edString($desktopLog) . " 2>&1 &");
exit;
###############################################################################
#
# CheckGeometryAndDepth simply makes sure that the geometry and depth values
# are sensible.
#
sub CheckGeometryAndDepth
{
if ($geometry =~ /^(\d+)x(\d+)$/) {
$width = $1; $height = $2;
if (($width<1) || ($height<1)) {
die "$prog: geometry $geometry is invalid\n";
}
while (($width % 4)!=0) {
$width = $width + 1;
}
while (($height % 2)!=0) {
$height = $height + 1;
}
$geometry = "${width}x$height";
} else {
die "$prog: geometry $geometry is invalid\n";
}
if (($depth < 8) || ($depth > 32)) {
die "Depth must be between 8 and 32\n";
}
}
#
# GetDisplayNumber gets the lowest available display number. A display number
# n is taken if something is listening on the VNC server port (5900+n) or the
# X server port (6000+n).
#
sub GetDisplayNumber
{
foreach $n (1..99) {
if (&CheckDisplayNumber($n)) {
return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
}
}
die "$prog: no free display number on $host.\n";
}
#
# CheckDisplayNumber checks if the given display number is available. A
# display number n is taken if something is listening on the VNC server port
# (5900+n) or the X server port (6000+n).
#
sub CheckDisplayNumber
{
local ($n) = @_;
socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
close(S);
return 0;
}
close(S);
socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
close(S);
return 0;
}
close(S);
if (-e "/tmp/.X$n-lock") {
warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
warn "Remove this file if there is no X server $host:$n\n";
return 0;
}
if (-e "/tmp/.X11-unix/X$n") {
warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
warn "Remove this file if there is no X server $host:$n\n";
return 0;
}
if (-e "/usr/spool/sockets/X11/$n") {
warn("\nWarning: $host:$n is taken because of ".
"/usr/spool/sockets/X11/$n\n");
warn "Remove this file if there is no X server $host:$n\n";
return 0;
}
return 1;
}
#
# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
# format of the current X display being used. If successful, it sets the
# options as appropriate so that the X VNC server will use the same settings
# (minus an allowance for window manager decorations on the geometry). Using
# the same depth and pixel format means that the VNC server won't have to
# translate pixels when the desktop is being viewed on this X display (for
# TrueColor displays anyway).
#
sub GetXDisplayDefaults
{
local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
$red, $green, $blue);
$wmDecorationWidth = 4; # a guess at typical size for window manager
$wmDecorationHeight = 24; # decoration size
return if (!defined($ENV{DISPLAY}));
@lines = `xdpyinfo 2>/dev/null`;
return if ($? != 0);
@matchlines = grep(/dimensions/, @lines);
if (@matchlines) {
($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
$width -= $wmDecorationWidth;
$height -= $wmDecorationHeight;
$geometry = "${width}x$height";
}
@matchlines = grep(/default visual id/, @lines);
if (@matchlines) {
($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
for ($i = 0; $i < @lines; $i++) {
if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
if (($lines[$i+1] !~ /TrueColor/) ||
($lines[$i+2] !~ /depth/) ||
($lines[$i+4] !~ /red, green, blue masks/))
{
return;
}
last;
}
}
return if ($i >= @lines);
($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
($red,$green,$blue)
= ($lines[$i+4]
=~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
$red = hex($red);
$green = hex($green);
$blue = hex($blue);
if ($red > $blue) {
$red = int(log($red) / log(2)) - int(log($green) / log(2));
$green = int(log($green) / log(2)) - int(log($blue) / log(2));
$blue = int(log($blue) / log(2)) + 1;
$pixelformat = "rgb$red$green$blue";
} else {
$blue = int(log($blue) / log(2)) - int(log($green) / log(2));
$green = int(log($green) / log(2)) - int(log($red) / log(2));
$red = int(log($red) / log(2)) + 1;
$pixelformat = "bgr$blue$green$red";
}
}
}
#
# quotedString returns a string which yields the original string when parsed
# by a shell.
#
sub quotedString
{
local ($in) = @_;
$in =~ s/\'/\'\"\'\"\'/g;
return "'$in'";
}
#
# removeSlashes turns slashes into underscores for use as a file name.
#
sub removeSlashes
{
local ($in) = @_;
$in =~ s|/|_|g;
return "$in";
}
#
# Usage
#
sub Usage
{
die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
" [-geometry <width>x<height>]\n".
" [-pixelformat rgbNNN|bgrNNN]\n".
" <Xvnc-options>...\n\n".
" $prog -kill <X-display>\n\n");
}
#
# Kill
#
sub Kill
{
$opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
if ($opt{'-kill'} =~ /^:\d+$/) {
$pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
} else {
if ($opt{'-kill'} !~ /^$host:/) {
die "\nCan't tell if $opt{'-kill'} is on $host\n".
"Use -kill :<number> instead\n\n";
}
$pidFile = "$vncUserDir/$opt{'-kill'}.pid";
}
if (! -r $pidFile) {
die "\nCan't find file $pidFile\n".
"You'll have to kill the Xvnc process manually\n\n";
}
$SIG{'HUP'} = 'IGNORE';
chop($pid = `cat $pidFile`);
warn "Killing Xvnc process ID $pid\n";
system("kill $pid");
unlink $pidFile;
exit;
}
#
# ParseOptions takes a list of possible options and a boolean indicating
# whether the option has a value following, and sets up an associative array
# %opt of the values of the options given on the command line. It removes all
# the arguments it uses from @ARGV and returns them in @optArgs.
#
sub ParseOptions
{
local (@optval) = @_;
local ($opt, @opts, %valFollows, @newargs);
while (@optval) {
$opt = shift(@optval);
push(@opts,$opt);
$valFollows{$opt} = shift(@optval);
}
@optArgs = ();
%opt = ();
arg: while (defined($arg = shift(@ARGV))) {
foreach $opt (@opts) {
if ($arg eq $opt) {
push(@optArgs, $arg);
if ($valFollows{$opt}) {
if (@ARGV == 0) {
&Usage();
}
$opt{$opt} = shift(@ARGV);
push(@optArgs, $opt{$opt});
} else {
$opt{$opt} = 1;
}
next arg;
}
}
push(@newargs,$arg);
}
@ARGV = @newargs;
}
#
# Routine to make sure we're operating in a sane environment.
#
sub SanityCheck
{
local ($cmd);
#
# Get the program name
#
($prog) = ($0 =~ m|([^/]+)$|);
#
# Check we have all the commands we'll need on the path.
#
cmd:
foreach $cmd ("uname","xauth","Xvnc","vncpasswd") {
for (split(/:/,$ENV{PATH})) {
if (-x "$_/$cmd") {
next cmd;
}
}
die "$prog: couldn't find \"$cmd\" on your PATH.\n";
}
#
# Check the HOME environment variable is set
#
if (!defined($ENV{HOME})) {
die "$prog: The HOME environment variable is not set.\n";
}
chdir($ENV{HOME});
#
# Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
# eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
# we just guess at the values. If you find perl moaning here, just
# hard-code the values of AF_INET and SOCK_STREAM. You can find these out
# for your platform by looking in /usr/include/sys/socket.h and related
# files.
#
chop($os = `uname`);
chop($osrev = `uname -r`);
eval 'use Socket';
if ($@) {
eval 'require "sys/socket.ph"';
if ($@) {
if (($os eq "SunOS") && ($osrev !~ /^4/)) {
$AF_INET = 2;
$SOCK_STREAM = 2;
} else {
$AF_INET = 2;
$SOCK_STREAM = 1;
}
} else {
$AF_INET = &AF_INET;
$SOCK_STREAM = &SOCK_STREAM;
}
} else {
$AF_INET = &AF_INET;
$SOCK_STREAM = &SOCK_STREAM;
}
}
vnc下的远程协助
让双方登录到一个桌面 如果与本地运行的X-windows进行远程协助,可以在本地的X-windows打开一个终端,然后执行:
x0vncserver -PasswordFile=/当前用户主目录/.vnc/passwd
如:x0vncserver -PasswordFile=/root/.vnc/passwd
因为远程用户要x0vncserver程序支持,所以不能关闭这个程序。当vnc客户机连接到":0"号桌面时,即与本地用户共享了桌面,实现了远程。
[lwg@fish ~]$ x0vncserver -PasswordFile=.vnc/passwd
[lwg@fish ~]$ x0vncserver --help
usage: x0vncserver [<parameters>]
Parameters can be turned on with -<param> or off with -<param>=0
Parameters which take a value can be specified as -<param> <value>
Other valid forms are <param>=<value> -<param>=<value> --<param>=<value>
Parameter names are case-insensitive. The parameters are:
Global Parameters:
QueryConnectTimeout - Number of seconds to show the Accept Connection dialog
before rejecting the connection (default=10)
rfbport - TCP port to listen for RFB protocol (default=5900)
display - The X display (default=)
Log - Specifies which log output should be directed to which
target logger, and the level of output to log. Format is
<log>:<target>:<level>[, ...]. (default=)
BlacklistTimeout - The initial timeout applied when a host is first
black-listed. The host cannot re-attempt a connection until
the timeout expires. (default=10)
BlacklistThreshold - The number of unauthenticated connection attempts
allowed from any individual host before that host is
black-listed (default=5)
RemapKeys - Comma-separated list of incoming keysyms to remap. Mappings
are expressed as two hex values, prefixed by 0x, and
separated by -> (default=)
QueryConnect - Prompt the local user to accept or reject incoming
connections. (default=0)
SendCutText - Send clipboard changes to clients. (default=1)
AcceptCutText - Accept clipboard updates from clients. (default=1)
AcceptPointerEvents - Accept pointer press and release events from clients.
(default=1)
AcceptKeyEvents - Accept key press and release events from clients.
(default=1)
DisconnectClients - Disconnect existing clients if an incoming connection is
non-shared. If combined with NeverShared then new
connections will be refused while there is a client active
(default=1)
NeverShared - Never treat incoming connections as shared, regardless of
the client-specified setting (default=0)
AlwaysShared - Always treat incoming connections as shared, regardless of
the client-specified setting (default=0)
Protocol3.3 - Always use protocol version 3.3 for backwards compatibility
with badly-behaved clients (default=0)
CompareFB - Perform pixel comparison on framebuffer to reduce
unnecessary updates (default=1)
ClientWaitTimeMillis - The number of milliseconds to wait for a client which
is no longer responding (default=20000)
IdleTimeout - The number of seconds after which an idle VNC connection
will be dropped (zero means no timeout) (default=3600)
Password - Obfuscated binary encoding of the password which clients
must supply to access the server (default=)
PasswordFile - Password file for VNC authentication (default=)
ReverseSecurityTypes - Specify encryption scheme to use for reverse
connections (None) (default=None)
SecurityTypes - Specify which security scheme to use for incoming
connections (None, VncAuth) (default=VncAuth)
ZlibLevel - Zlib compression level (default=-1)
MaxCutText - Maximum permitted length of an incoming clipboard update
(default=262144)
~ImageCleanup called
[lwg@fish ~]$
vnc服务使用端口与桌面相关,vnc服务器使用tcp端口从5900开始。桌面号为1,则端口号为5901,桌面号为2,则端口号为5902。基于Java的vnc客户程序web服务,tcp端口从5800开始。
iptables -I INPUT -p tcp --dport 5901 -j ACCEPT
iptables -I INPUT -p tcp --dport 5801 -j ACCEPT 重启桌面号:
vncserver -kill 1
vncserver :1 启动1号桌面
用户 可能启动多个桌面
vncserver :2
等等,它们之间不受影响。 让系统自动管理桌面,如root的管理1号桌面,lwg管理2号桌面。
修改:vim /etc/sysconfig/vncservers
VNCSERES= "1:root"
VNCSERES="2:lwg"
相关阅读 更多 +