Submitted by admin on 2012, March 6, 6:24 PM
先看看squid原始文档怎么解释吧
# TAG: logformat
# Usage:
#
# logformat <name> <format specification>
#
# Defines an access log format.
#
# The <format specification> is a string with embedded % format codes
#
# % format codes all follow the same basic structure where all but
# the formatcode is optional. Output strings are automatically escaped
# as required according to their context and the output format
# modifiers are usually not needed, but can be specified if an explicit
# output format is desired.
#
# % ["|[|'|#] [-] [[0]width] [{argument}] formatcode
#
# " output in quoted string format
# [ output in squid text log format as used by log_mime_hdrs
# # output in URL quoted format
# ' output as-is
#
# - left aligned
# width field width. If starting with 0 the
# output is zero padded
# {arg} argument such as header name etc
#
# Format codes:
#
# % a literal % character
# >a Client source IP address
# >A Client FQDN
# >p Client source port
# <A Server IP address or peer name
# la Local IP address (http_port)
# lp Local port number (http_port)
# <la Local IP address of the last server or peer connection
# <lp Local port number of the last server or peer connection
# ts Seconds since epoch
# tu subsecond time (milliseconds)
# tl Local time. Optional strftime format argument
# default %d/%b/%Y:%H:%M:%S %z
# tg GMT time. Optional strftime format argument
# default %d/%b/%Y:%H:%M:%S %z
# tr Response time (milliseconds)
# dt Total time spent making DNS lookups (milliseconds)
#
# HTTP cache related format codes:
#
# [http::]>h Original request header. Optional header name argument
# on the format header[:[separator]element]
# [http::]>ha The HTTP request headers after adaptation and redirection.
# Optional header name argument as for >h
# [http::]<h Reply header. Optional header name argument
# as for >h
# [http::]un User name
# [http::]ul User name from authentication
# [http::]ui User name from ident
# [http::]us User name from SSL
# [http::]ue User name from external acl helper
# [http::]>Hs HTTP status code sent to the client
# [http::]<Hs HTTP status code received from the next hop
# [http::]Ss Squid request status (TCP_MISS etc)
# [http::]Sh Squid hierarchy status (DEFAULT_PARENT etc)
# [http::]mt MIME content type
# [http::]rm Request method (GET/POST etc)
# [http::]ru Request URL
# [http::]rp Request URL-Path excluding hostname
# [http::]rv Request protocol version
# [http::]et Tag returned by external acl
# [http::]ea Log string returned by external acl
# [http::]<st Sent reply size including HTTP headers
# [http::]>st Received request size including HTTP headers. In the
# case of chunked requests the chunked encoding metadata
# are not included
# [http::]>sh Received HTTP request headers size
# [http::]<sh Sent HTTP reply headers size
# [http::]st Request+Reply size including HTTP headers
# [http::]<sH Reply high offset sent
# [http::]<sS Upstream object size
# [http::]<pt Peer response time in milliseconds. The timer starts
# when the last request byte is sent to the next hop
# and stops when the last response byte is received.
# [http::]<tt Total server-side time in milliseconds. The timer
# starts with the first connect request (or write I/O)
# sent to the first selected peer. The timer stops
# with the last I/O with the last peer.
#logformat squid %ts.%03tu %6tr %>a %Ss/%03>Hs %<st %rm %ru %un %Sh/%<A %mt
#logformat squidmime %ts.%03tu %6tr %>a %Ss/%03>Hs %<st %rm %ru %un %Sh/%<A %mt [%>h] [%<h]
#logformat common %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st %Ss:%Sh
#logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
#Default:
# none
{} modifier or argument. Also used to specify header names
> request (client) 客户请求
< reply (server) 服务端回应
a address 访问用户ip地址
A address name 访问用户电脑名称
h all headers 浏览器头信息
i ident
p port 端口
r request line (no query)
t time 访问时间
u user
l local address/port (where request was accepted)
自己squid配置文件日志格式定义如下:
logformat cccdn %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st %tr "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
squid/缓存 | 评论:0
| Trackbacks:0
| 阅读:892
Submitted by admin on 2012, March 6, 6:23 PM
linux | 评论:0
| Trackbacks:0
| 阅读:940
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
| 阅读:1107
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
| 阅读:792
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
| 阅读:1228
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
| 阅读:770
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
| 阅读:859
Submitted by admin on 2012, March 3, 8:59 PM
windows | 评论:0
| Trackbacks:0
| 阅读:809