Submitted by admin on 2012, March 6, 6:13 PM
管理Linux系统以及应用程序的日志非常重要且具有趣味性。我们想要得到的是重要的、有用的信息,并不是大量的垃圾。你得有能力在日志中找到你需要的信息。古老的 syslogd 工具已经工作了许多个年头。但它现在已经不足以面对更加复杂的需要。为了代替它,我们有了新一代的 Linux 日志管理工具,syslog-ng(syslog-next-generation)。
比较 syslog ,syslog-ng 具有众多高级的功能:更好的网络支持,更加方便的配置,集中式的网络日志存储,并且更具有弹性。比如,使用syslogd时,所有的iptables日志与其他内核日志一起全部存储到了kern.log文件里。Syslog-ng则可以让你有选择性的将iptables部分分出到另外的日志文件中。Syslogd仅能使用UDP协议,Syslog-ng 可以使用UDP和TCP协议。所以你可以在加密的网络隧道中传输日志到集中日志服务器。
安装
所有的Linux发行版本已经默认提供了Syslog-ng包。目前的稳定版本为1.6。如果你使用yum或者apt-get等工具安装Syslog-ng,他们会自动卸载sysklogd包。如果sysklogd没有被自动卸载,亲你手动删除它并且确保syslogd服务已经停止。(sysklogd 是软件包名,syslogd 是服务名)
配置文件结构
由于功能具有弹性,所以配置文件的学习可能稍微曲折。Syslog-ng 的配置文件在 /etc/syslog-ng/syslog-ng.conf ,或者是 /etc/syslog-ng.conf 。你可以通过 man 5 syslog-ng 来学习所有的选项以及字段的含义。一般 syslog-ng 可能含有以下5个章段:
options{}
全局设置。 These can be overridden in any of the next four sections
source{}
信息来源, 来源可以是文件, 本地 sockets, 或者远程主机。
destination{}
信息目标, 可以是文件, 本地 sockets, 或者远程主机。
filter{}
过滤选项非常强大且复杂;你可以过滤日志中的任何方面,比如基础的 syslogd 快捷字段(facility names–man 5 syslog.conf 获取更多信息),log 等级,主机名,以及任何log中出现的字段或者是数字。
log{}
此段将来源 目的 过滤 都给连接起来并且告诉syslog-ng如何处理日志。
一些典型的全局选项:
options {
sync (0);
log_fifo_size (2048);
create_dirs (yes);
group (logs);
dir_group (logs);
perm (0640);
dir_perm (0750);
};
在 /etc/syslog-ng.conf 中使用的全局选项字段必须是被定义过的。 上面例子的具体含义是:
sync
在写入磁盘之前保留多少行的消息输出队列。(这里将日志输出称为“消息”。)0 是首选的数值,这可以让你确保捕获了所有信息,且磁盘也不会在系统以外掉电时丢失信息。
log_fifo_size
输出队列中消息的最大行数。默认为100行。你可以通过计算得出一个合适的数值,以下是引用一份邮件列表,里面提到了相关的内容:
每个消息来源最大接收数据的1024字节。单条日志信息为20至30字节。所以程序的一次执行可以送出1024/20=50条消息。对每个日志发送方,你的 log 接受能力必须满足这个数值。若你有两个日志源,则你必须能一次接受100条信息。
Each message source receives maximum 1024 bytes of data. A single log message is about 20 to 30 bytes. So on a single run each log source may emit 1024/20 = 50 messages. Your log fifo must be able to hold this number of messages for each source. So for two sources, you’ll need at least 100 slots in your fifo.
那两个选项非常容易混淆。 来自远程主机的消息会突然到达,所以你要保证你的 log_fifo_size 参数足以接纳那些突然增加的消息。 不然你就会丢失日志了, 当然这还受限于你的网络速度以及I/O速率。
create_dirs
启用或禁用自动为目标文件创建目录的功能。 在上文中值为“yes”, 所以远程主机会更具需要自动创建目录。
group
dir_group
设定日志文件以及目录的所有人(owner),所以你不必以超级用户(root)的身份去浏览那些日志。
perm
dir_perm
默认新日志文件的权限设置。
Source, Destination, 和 Filter 描述
Source, destination, 和 filter 的名字可以是任意的,看看下面的例子:
source s_local { internal(); unix-stream(”/dev/log”); file(”/proc/kmsg” log_prefix(”kernel: “)); };
destination d_auth { file(”/var/log/auth.log”); };
filter f_auth { facility(auth, authpriv); };
如果你喜欢你可以将 “source s_local” 改为 “source frederick_remington_depew”,当然 “destination d_auth” 也可以为 “destination moon.” 通常我们将 source 的名字中含有 “s” 字段,destination 的名字中含有 “d” 字段, filter 的名字中含有 “f” 字段,但这并不是必须的。source, destination, 和 filter 中其他的选项必须使用已经定义的字段,你可以通过 man 5 syslog-ng.conf 查看这些参数。
上面的例子 “source s_local” 将所有本地生成的日志信息作为一个信息源。而 “destination d_auth” 选项又将这些日志输入了 /var/log/auth.log 文件,当然这是通过 “filter f_auth” 将它们连接起来的。 auth 和 authpriv 是 syslog 标准的设备名称。
最后的日志申明(Log statements)将他们链接到了一起:
log {source(s_local); filter(f_auth); destination(d_auth); };
那么这四行就将本地所有的 auth 和 authpriv 日志筛出并写入了 /var/log/auth.log。
启用远程日志记录
当然,使用老的 syslogd 也可以很好的完成远程日志记录的工作,但那真的不太好,因为他只使用UDP协议传送数据包。首先你需要将所有的客户端上都装好 syslog-ng。并且在日志服务器的 syslog-ng.conf 文件中加入以下配置文件,这样才可以接受远程客户端日志,并将每台机器的日志写入单独的文件。
source s_remote { tcp(); };
destination d_clients { file(”/var/log/HOSTS/$HOST/”); };
log { source(s_remote); destination(d_clients); };
这是一个非常简单但十分有用的例子,他收集所有客户端本地的日志并将日志发送至远程日志服务器:
#sample syslog-ng.conf for a remote client
source s_local { internal(); unix-stream(”/dev/log”); file(”/proc/kmsg” log_prefix(”kernel: “)); };
destination d_loghost {tcp(”192.168.1.10″ port(514));};
log { source(s_local); destination(loghost); };
syslog-ng.conf 配置参考
一份完整的样板配置文件会显得比较长,你可以查看 syslog-ng 安装时附带的配置文件。Debian 用户得到的是一份通过 syslogd 配置而定制的配置文件。我们将给出一份日志服务器的配置文件,以及一个远程日志客户端的配置文件,来看看他们是怎么工作起来的吧:
#sample syslog-ng.conf for a central logging server
options {
sync (0);
log_fifo_size (2048);
create_dirs (yes);
group (logs);
dir_group (logs);
perm (0640);
dir_perm (0750);
};
source s_local { internal(); unix-stream(”/dev/log”); file(”/proc/kmsg” log_prefix(”kernel: “)); };
destination d_auth { file(”/var/log/auth.log”); };
filter f_auth { facility(auth, authpriv); };
source s_remote { tcp(); };
destination d_clients { file(”/var/log/HOSTS/$HOST”); };
log { source(s_remote); destination(d_clients); };
log { source(s_local); filter(f_auth); destination(d_auth); };
修改完 syslog-ng 之后要重启服务:
# /etc/init.d/syslog-ng restart
测试
现在你可以在日志服务器以及客户端上进行一些测试了。由于在本地服务器中最先出现的日志一般是认证日志,所以你可以先尝试着打开一个新的登录窗口,使用su 或者是 sudo 都可以。接着检查 /var/log/auth.log 文件。在客户端上做一些操作,接着检查 /var/log/HOSTS 是否已经为远程客户端创建了新的目录。
另外一个方法是使用更加高级的 logger 命令:
# logger “this is a test”
# logger -p auth.debug “this is a test”
这将在你的日志文件中创建如下内容:
Apr 1 16:08:42 localhost.localdomain logger: this is a test
现在我们已经掌握了 syslog-ng 基本的功能,下周我们讲学习如何将 syslog-ng 客户端以及服务器按照你的意愿配置的更加有规则、易于管理,并且讨论如何建立一个安全的、加密的 syslog-ng 传输。
-----------使用syslog-ng搭建日志服务器---
一、环境
Gentoo-2007.0_amd64
二、安装:为了简便,我用了系统自带的syslog-ng
三、服务器配置:
#cat /etc/syslog-ng/syslog-ng.conf
#
# configuration file for syslog-ng, customized for remote logging
#
options {
use_fqdn(yes);
chain_hostnames(off);
keep_hostname(off);
sync(0);
# The default action of syslog-ng 1.6.0 is to log a STATS line
# to the file every 10 minutes. That's pretty ugly after a while.
# Change it to every 12 hours so you get a nice daily update of
# how many messages syslog-ng missed (0).
stats(43200);
create_dirs(yes);
};
source s_internal { internal(); };
destination d_syslognglog { file("/var/log/syslog-ng.log"); };
log { source(s_internal); destination(d_syslognglog); };
source s_sys { file ("/proc/kmsg" log_prefix("kernel: ")); unix-stream ("/dev/log"); internal(); };
destination d_cons { file("/dev/console"); };
destination d_mesg { file("/var/log/messages"); };
destination d_auth { file("/var/log/secure"); };
destination d_mail { file("/var/log/maillog"); };
destination d_spol { file("/var/log/spooler"); };
destination d_boot { file("/var/log/boot.log"); };
destination d_cron { file("/var/log/cron"); };
destination d_rsync { file("/var/log/rsync"); };
destination d_mlal { usertty("*"); };
filter f_filter1 { facility(kern); };
filter f_filter2 { level(info) and
not (facility(mail)
or facility(authpriv) or facility(cron)); };
filter f_filter3 { facility(authpriv); };
filter f_filter4 { facility(mail); };
filter f_filter5 { level(emerg); };
filter f_filter6 { facility(uucp) or
(facility(news) and level(crit)); };
filter f_filter7 { facility(local7); };
filter f_filter8 { facility(cron); };
filter f_filter9 { facility(daemon); };
filter f_filter10 { facility(local6); };
#log { source(s_sys); filter(f_filter1); destination(d_cons); };
log { source(s_sys); filter(f_filter2); destination(d_mesg); };
log { source(s_sys); filter(f_filter3); destination(d_auth); };
log { source(s_sys); filter(f_filter4); destination(d_mail); };
log { source(s_sys); filter(f_filter5); destination(d_mlal); };
log { source(s_sys); filter(f_filter6); destination(d_spol); };
log { source(s_sys); filter(f_filter7); destination(d_boot); };
log { source(s_sys); filter(f_filter8); destination(d_cron); };
# Remote logging
source s_remote {
udp(ip(0.0.0.0) port(514));
};
destination r_mesg { file("/var/log/syslog-ng/$YEAR/$MONTH/$HOST/messages" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes)); };
destination r_auth { file("/var/log/syslog-ng/$YEAR/$MONTH/$HOST/secure" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes)); };
destination r_mail { file("/var/log/syslog-ng/$YEAR/$MONTH/$HOST/maillog" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes)); };
destination r_spol { file("/var/log/syslog-ng/$YEAR/$MONTH/$HOST/spooler" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes)); };
destination r_boot { file("/var/log/syslog-ng/$YEAR/$MONTH/$HOST/boot.log" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes)); };
destination r_cron { file("/var/log/syslog-ng/$YEAR/$MONTH/$HOST/cron" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes)); };
destination r_daemon { file("/var/log/syslog-ng/$YEAR/$MONTH/$HOST/daemon" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes)); };
destination r_local6 { file("/var/log/syslog-ng/$YEAR/$MONTH/network/messages" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes)); };
#destination d_separatedbyhosts {
# file("/var/log/syslog-ng/$HOST/messages" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes));
#};
#log { source(s_remote); destination(d_separatedbyhosts); };
log { source(s_remote); filter(f_filter2); destination(r_mesg); };
log { source(s_remote); filter(f_filter3); destination(r_auth); };
log { source(s_remote); filter(f_filter4); destination(r_mail); };
log { source(s_remote); filter(f_filter6); destination(r_spol); };
log { source(s_remote); filter(f_filter7); destination(r_boot); };
log { source(s_remote); filter(f_filter8); destination(r_cron); };
log { source(s_remote); filter(f_filter9); destination(r_daemon); };
log { source(s_remote); filter(f_filter10); destination(r_local6); };
四、客户端配置
linux使用的syslog守护进程主要有两种,syslog和syslog-ng
1、syslog
#vi /etc/syslog.conf
*.* @loghost
loghost为日志服务器的IP或者主机名,主机明必须能正确解析到日志服务器IP。
然后重新启动syslog服务:
#/etc/init.d/syslog restart
2、syslog-ng
在配置文件syslog-ng.conf中加入两行:
destination d_udp { udp("loghost" port(514)); };
log { source(src); destination(d_udp); };
重新启动syslog-ng服务
#/etc/init.d/syslog-ng restart
winodws服务器的配置
因为windows服务器不支持日志服务器,因此需要安装一个转换软件:
下载地址为:
https://engineering.purdue.edu/ECN/Resources/Documents/UNIX/evtsys/
根据系统的版本下载32位和64位的程序。
解压后是两个文件evtsys.dll和evtsys.exe
把这两个文件拷贝到 c:\windows\system32目录下。
打开Windows命令提示符(开始->运行 输入CMD)
C:\>evtsys –i –h 192.168.10.100 #(日志服务器的IP地址)
-i 表示安装成系统服务
-h 指定log服务器的IP地址
如果要卸载evtsys,则:
net stop evtsys
evtsys -u
启动该服务:
C:\>net start evtsys
linux | 评论:0
| Trackbacks:0
| 阅读:1079
Submitted by admin on 2012, March 6, 8:52 AM
精典错误
/libxmlrpc/encoding.c:101:undefined reference to 'libiconv_close'
collect2: ld returned 1 exit status
make:*** [sapi/fpm/php-fpm] Error 1
解决方法:
#make ZEND_EXTRA_LIBS='-liconv'
错误一、编译php出错
/php-5.3.2/ext/fileinfo/libmagic/apprentice.c:147:internal compiler error:Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://bugzilla.redhat.com/bugzilla> for instructions.
The bug is not reproducible,so it is likely a hardware or OS problem.
make:*** [ext/fileinfo/libmagic/apprentice.lo] Error 1
解决方法:内存大于1G即可,这是php5.3.2的一个bug
--------------------------------------------------------------------
错误二、重新构造configure文件出错
./buildconf --force
Forcing buildconf
buildconf:checking installation…
buildconf:autoconf version 2.59 (ok)
buildconf:Your version of autoconf likely contains buggy cache code.
Running vcsclean for you.
To avoid this,install autoconf-2.13.
Can't figure out your VCS, not cleaning.
解决方法:编译安装autoconf-2.13
再将autoconf-2.13的auotconf文件至/usr/local/autoconf
--------------------------------------------------------------------
错误三、编译时缺少库
configure: error: libXpm.(a|so) not found.
解决方法:yum install libXpm-devel
--------------------------------------------------------------------
错误四、编译时缺少gmp.h文件
configure: error: Unable to locate gmp.h
解决方法:yum install gmp-devel
--------------------------------------------------------------------
错误五
Configure: error: xml2-config not found. Please check your libxml2 installation.
解决方法:
#yum install libxml2 libxml2-devel (For Redhat & Fedora)
# aptitude install libxml2-dev (For ubuntu)
--------------------------------------------------------------------
错误六
Checking for pkg-config… /usr/bin/pkg-config
configure: error: Cannot find OpenSSL’s <evp.h>
解决方法:
#yum install openssl openssl-devel
--------------------------------------------------------------------
错误七
Configure: error: Please reinstall the BZip2 distribution
解决方法:
# yum install bzip2 bzip2-devel
--------------------------------------------------------------------
错误八
Configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/
解决方法:
# yum install curl curl-devel (For Redhat & Fedora)
# install libcurl4-gnutls-dev (For Ubuntu)
--------------------------------------------------------------------
错误九:
Configure: error: libjpeg.(also) not found.
解决方法:
# yum install libjpeg libjpeg-devel
--------------------------------------------------------------------
错误十
Configure: error: libpng.(also) not found.
--------------------------------------------------------------------
解决方法:
# yum install libpng libpng-devel
--------------------------------------------------------------------
错误十一
Configure: error: freetype.h not found.
解决方法:
#yum install freetype-devel
--------------------------------------------------------------------
错误十二
Configure: error: Unable to locate gmp.h
解决方法:
# yum install gmp-devel
--------------------------------------------------------------------
错误十三
Configure: error: Cannot find MySQL header files under /usr.
Note that the MySQL client library is not bundled anymore!
解决方法:
# yum install mysql-devel (For Redhat & Fedora)
# apt-get install libmysql++-dev (For Ubuntu)
--------------------------------------------------------------------
错误十四
Configure: error: Please reinstall the ncurses distribution
解决方法:
# yum install ncurses ncurses-devel
--------------------------------------------------------------------
错误十五
Checking for unixODBC support… configure: error: ODBC header file ‘/usr/include/sqlext.h’ not found!
解决方法:
# yum install unixODBC-devel
--------------------------------------------------------------------
错误十六
Configure: error: Cannot find pspell
解决方法:
# yum install pspell-devel
--------------------------------------------------------------------
错误十七
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决方法:
# yum install libmcrypt libmcrypt-devel (For Redhat & Fedora)
# apt-get install libmcrypt-dev
--------------------------------------------------------------------
错误十八
Configure: error: snmp.h not found. Check your SNMP installation.
解决方法:
# yum install net-snmp net-snmp-devel
--------------------------------------------------------------------
错误十九
configure:error:Cannot find ldap.h
解决方法:
#yum install openldap-devel openldap
错误二十
configure:error:xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
解决方法:
#yum install libxslt libxslt-devel
错误二十一
checking for libevent >=1.4.11 install prefix… configure: error: Could not find libevent >=1.4.11 in /usr/local/php
解决方法:
安装libevent-1.4.11以上版本至/usr/local
tar xzvf libevent-1.4.14-stable.tar.gz
cd libevent-1.4.14-stable
./configure --prefix=/usr/local
make&&make install
在编译。/configure时添加--with-libevent-dir=/usr/local即可
错误二十二
cc1: out of memory allocating 2036 bytes after a total of 81846272 bytes
make: *** [ext/date/lib/parse_date.lo] Error 1
报错:
/usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
make:*** [sapi/fpm/php-fpm] Error 1
解决方法:
安装ltdl
#cd /libmcrypt-2.5.7/libltdl/
#./configure --enable-ltdl-install
#ldconfig
#cd php-5.3.6
#make ZEND_EXTRA_LIBS='-liconv'
php | 评论:0
| Trackbacks:0
| 阅读:765
Submitted by admin on 2012, March 5, 3:03 PM
我们使用ssh链接linux主机时,可能出现“Host key verification failed.“的提示,ssh连接不成功。
可能的提示信息如下:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!Someone could be eavesdropping on you right now (man-in-the-middle attack)!It is also possible that the RSA host key has just been changed.The fingerprint for the RSA key sent by the remote host is23:00:20:83:de:02:95:f1:e3:34:be:57:3f:cf:2c:e7.Please contact your system administrator.Add correct host key in /home/xahria/.ssh/known_hosts to get rid of this message.Offending key in /home/xahria/.ssh/known_hosts:8RSA host key for localhost has changed and you have requested strict checking.Host key verification failed.
网上很多的解决方案是:vi ~/.ssh/known_hosts 删除与想要连接的主机相关的行;或者直接删除known_hosts这个文件。 当然这个方案也是可行的,但并非解决问题的根本办法,因为继续使用,今后还会出现这样的情况,还得再删除。
下面简单讲一下这个问题的原理和比较长久的解决方案。
用OpenSSH的人都知ssh会把你每个你访问过计算机的公钥(public key)都记录在~/.ssh/known_hosts。当下次访问相同计算机时,OpenSSH会核对公钥。如果公钥不同,OpenSSH会发出警告,避免你受到DNS Hijack之类的攻击。
SSH对主机的public_key的检查等级是根据StrictHostKeyChecking变量来配置的。默认情况下,StrictHostKeyChecking=ask。简单所下它的三种配置值:
1.StrictHostKeyChecking=no
#最不安全的级别,当然也没有那么多烦人的提示了,相对安全的内网测试时建议使用。如果连接server的key在本地不存在,那么就自动添加到文件中(默认是known_hosts),并且给出一个警告。
2.StrictHostKeyChecking=ask #默认的级别,就是出现刚才的提示了。如果连接和key不匹配,给出提示,并拒绝登录。
3.StrictHostKeyChecking=yes #最安全的级别,如果连接与key不匹配,就拒绝连接,不会提示详细信息。
对于我来说,在内网的进行的一些测试,为了方便,选择最低的安全级别。在.ssh/config(或者/etc/ssh/ssh_config)中配置:
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
(注:这里为了简便,将knownhostfile设为/dev/null,就不保存在known_hosts中了)
参考资料:http://www.symantec.com/connect/articles/ssh-host-key-protection
linux | 评论:0
| Trackbacks:0
| 阅读:1190
Submitted by admin on 2012, March 5, 2:38 PM
使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄、收藏。可是为什么要这么写却不知其然。本文用一个最短的例子说明脚本的原理。
脚本代码如下:
##############################################
#!/usr/bin/expect
set timeout 30
spawn ssh -l username 192.168.1.1
expect "password:"
send "ispass\r"
interact
##############################################
1. [#!/usr/bin/expect]
这一行告诉操作系统脚本里的代码使用那一个shell来执行。这里的expect其实和linux下的bash、windows下的cmd是一类东西。
注意:这一行需要在脚本的第一行。
2. [set timeout 30]
基本上认识英文的都知道这是设置超时时间的,现在你只要记住他的计时单位是:秒
3. [spawn ssh -l username 192.168.1.1]
spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。所以不要用 “which spawn“之类的命令去找spawn命令。好比windows里的dir就是一个内部命令,这个命令由shell自带,你无法找到一个dir.com 或 dir.exe 的可执行文件。
它主要的功能是给ssh运行进程加个壳,用来传递交互指令。
4. [expect "password:"]
这里的expect也是expect的一个内部命令,有点晕吧,expect的shell命令和内部命令是一样的,但不是一个功能,习惯就好了。这个命令的意思是判断上次输出结果里是否包含“password:”的字符串,如果有则立即返回,否则就等待一段时间后返回,这里等待时长就是前面设置的30秒
5. [send "ispass\r"]
这里就是执行交互动作,与手工输入密码的动作等效。
温馨提示: 命令字符串结尾别忘记加上“\r”,如果出现异常等待的状态可以核查一下。
6. [interact]
执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。如果你只是登录过去执行
#!/usr/bin/expect #注意安装的路径,不确定 whereis expect 一下
# Change a login shell to bash
set user [lindex $argv 0]
spawn bash $user
expect "]:"
send "/bin/bash "
expect eof
exit
linux | 评论:0
| Trackbacks:0
| 阅读:743
Submitted by admin on 2012, March 3, 11:23 PM
ssh -l 6552:ip:22 ip
ssh -f -N -g -R 9001:www.baidu.com:80 ip
ssh -L 2002:ip:22 ip
例子B:一次同时映射多个端口
ssh -L 8888:www.host.com:80 -L 110:mail.host.com:110 -L 25:mail.host.com:25 user@host -N
这个命令将自动把服务器的80,110,25端口映射到本机的8888,110和25端口 以上命令在ubuntu 9.10 上测试通过...
1.将发往本机的80端口访问转发到174.139.9.66的8080端口
ssh -C -f -N -g -L 80:174.139.9.66:8080 master@174.139.9.66
2.将发往174.139.9.66的8080访问转发到本机的80端口
ssh -C -f -N -g -R 80:174.139.9.66:8080 master@174.139.9.66
http://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/
http://blog.csdn.net/xyyangkun/article/details/7025854
linux | 评论:0
| Trackbacks:0
| 阅读:828
Submitted by admin on 2012, March 3, 8:59 PM
windows | 评论:0
| Trackbacks:0
| 阅读:786
Submitted by admin on 2012, March 3, 4:03 PM
在前面的文章中,我简单介绍了影响linux性能的几个方面以及如何解决这些方面的问题,但是如何才能从系统上发现是某个方面或某几个方面出现问题了呢,这就需要使用linux系统提供的几个常用性能分析工具,下面就具体讲述这几个常用性能分析工具的使用。
1.vmstat命令
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,很多linux发行版本都默认安装了此命令工具,利用vmstat命令可以对操作系统的内存信息、进程状态、CPU活动等进行监视,不足之处是无法对某个进程进行深入分析。
vmstat使用语法如下:
vmstat [-V] [-n] [delay [count]]
各个选项及参数含义如下:
l -V:表示打印出版本信息,是可选参数。
l -n:表示在周期性循环输出时,输出的头部信息仅显示一次。
l delay:表示两次输出之间的间隔时间。
l count:表示按照“delay”指定的时间间隔统计的次数。默认为1。
例如:
vmstat 3
表示每3秒钟更新一次输出信息,循环输出,按ctrl+c停止输出。
vmstat 3 5
表示每3秒更新一次输出信息,统计5次后停止输出。
2.iostat命令
iostat是I/O statistics(输入/输出统计)的缩写,主要的功能是对系统的磁盘I/O操作进行监视。它的输出主要显示磁盘读写操作的统计信息,同时也会给出CPU使用情况。同vmstat一样,iostat也不能对某个进程进行深入分析,仅对系统的整体情况进行分析。
iostat一般都不随系统安装,要使用iostat工具,需要在系统上安装一个Sysstat的工具包,Sysstat是一个开源软件,官方地址为http://pagesperso-orange.fr/sebastien.godard
可以选择源代码包或rpm包的方式安装,这里不在讲述安装过程,安装完毕,系统会多出3个命令:iostat、sar和mpstat。然后就可以直接在系统下运行iostat命令了。
iostat使用语法如下:
iostat [ -c | -d ] [ -k ] [ -t ] [ -x [ device ] ] [ interval [ count ] ]
各个选项及参数含义如下:
-c:显示CPU的使用情况。
-d:显示磁盘的使用情况。
-k:每秒以k bytes为单位显示数据。
-t:打印出统计信息开始执行的时间。
-x device:指定要统计的磁盘设备名称,默认为所有的磁盘设备。
interval:指定两次统计间隔的时间;
count:按照“interval”指定的时间间隔统计的次数。
3.sar命令
sar命令很强大,是分析系统性能的重要工具之一,通过sar指令,可以全面的获取系统的CPU、运行队列、磁盘I/O、分页(交换区)、内存、CPU中断、网络等性能数据。
sar使用格式为:
sar [options] [g1] [-o filename] [interval [count] ]
各个选项及参数含义如下:
l options 为命令行选项,sar命令的选项很多,下面只列出常用选项:
Ø -A:显示系统所有资源设备(CPU、内存、磁盘)的运行状况。
Ø -u:显示系统所有CPU在采样时间内的负载状态。
Ø -P:显示当前系统中指定CPU的使用情况。
Ø -d:显示系统所有硬盘设备在采样时间内的使用状况。
Ø -r:显示系统内存在采样时间内的使用状况。
Ø -b:显示缓冲区在采样时间内的使用情况。
Ø -v:显示进程、文件、I节点和锁表状态。
Ø -n:显示网络运行状态。参数后面可跟DEV、EDEV、SOCK和FULL。DEV显示网络接口信息,EDEV显示网络错误的统计数据,SOCK显示套接字信息,FULL显示三个所有的信息。它们可以单独或者一起使用。
Ø -q:显示了运行队列的大小,它与系统当时的平均负载相同。
Ø -R:显示进程在采样时间内的活动情况。
Ø -y:显示终端设备在采样时间内的活动情况。
Ø -w:显示系统交换活动在采样时间内的状态。
l -o filename:表示将命令结果以二进制格式存放在文件中,filename是文件名。
l interval:表示采样间隔时间,是必须有的参数。
l count:表示采样次数,是可选参数,默认值是1。
例如:
要查看系统CPU的整体负载状况,每3秒统计一次,统计5次,可以使用以下组合:
sar –u 3 5
系统的CPU计数是从0开始的,如果要查看第二颗CPU的运行负载,使用下面组合:
sar –P 1 3 5
要查看系统磁盘的读写性能,使用以下组合:
sar -d 3 5
同理,查看系统内存使用情况、网络运行状态,可以分别使用下面命令:
sar -r 5 2
sar -n DEV 5 3
4. 系统性能分析标准
性能调优的主要目的是使系统能够有效的利用各种资源,最大的发挥应用程序和系统之间的性能融合,使应用高效、稳定的运行。但是,衡量系统资源利用率好坏的标准没有一个严格的定义,针对不同的系统和应用也没有一个统一的说法,因此,这里提供的标准其实是一个经验值,表15.1给出了判定系统资源利用状况的一般准则:
表1给出了判定系统资源利用状况的一般准则:
表1

其中:
%user:表示CPU处在用户模式下的时间百分比。
%sys:表示CPU处在系统模式下的时间百分比。
%iowait:表示CPU等待输入输出完成时间的百分比。
swap in:即si,表示虚拟内存的页导入,即从SWAP DISK交换到RAM。
swap out:即so,表示虚拟内存的页导出,即从RAM交换到SWAP DISK。
5. 说在最后
以上我们讲解了三个常用的系统性能分析工具,其实linux下性能分析工具还有很多,例如uptime可以检查CPU的平均负载,free可以查看系统内存的使用状况,ps、top可以配合监控系统的进程运行状态,netstat可以监测网络流量状况等等,这些命令的使用方法在我前面文章已经讲述很多,故不在这里讲解。
系统性能优化是个涉及面广、繁琐、长久的工作,寻找出现性能问题的根源往往是最难的部分,一旦找到出现问题的原因,性能问题也就迎刃而解。因此,解决问题的思路变得非常重要。
例如,linux系统下的一个网站系统,用户反映,网站访问速度很慢,有时无法访问。
针对这个问题,第一步要做的是检测网络,可以通过ping命令检查网站的域名解析是否正常,同时,ping服务器地址的延时是否过大等等,通过这种方式,首先排除网络可能出现的问题;如果网络没有问题,接着进入第二步,对linux系统的内存使用状况进行检查,因为网站响应速度慢,一般跟内存关联比较大,通过free、vmstat等命令判断内存资源是否紧缺,如果内存资源不存在问题,进入第三步,检查系统CPU的负载状况,可以通过sar、vmstat、top等命令的输出综合判断CPU是否存在过载问题,如果CPU没有问题,继续进入第四步,检查系统的磁盘I/O是否存在瓶颈,可以通过iostat、vmstat等命令检查磁盘的读写性能,如果磁盘读写也没有问题,linux系统自身的性能问题基本排除,最后要做的是检查程序本身是否存在问题。通过这样的思路,层层检测,步步排查,性能问题就“无处藏身”,查找出现性能问题的环节也就变得非常简单。
linux | 评论:0
| Trackbacks:0
| 阅读:728