服务器维护命令ubuntu

正在连接的ip

centos7
netstat -nat|grep ":80"|awk '{print 5}' |awk -F: '{print1}' | sort| uniq -c|sort -n

重启sshd服务

systemctl restart sshd

服务器日志

一、配置syslog(gyl4802959同学撰写)

目前,linux依旧使用syslogd作为日志监控进程,对其进行必要的配置能减少很多麻烦,并且可更有效的从系统日志监控到系统的状态。理解并完善一个syslog的配置,对于系统管理员来说显得尤为重要。
/etc/syslog.conf根据如下的格式定义规则: facility.level action
设备.优先级 动作facility.level 字段也被称为seletor(选择条件),选择条件和动作之间用空格或tab分割开。

号开头的是注释,空白行会自动跳过。

1、facility

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">facility定义日志消息的范围,其可使用的key有: auth -由 pam_pwdb 报告的认证活动。
authpriv -包括特权信息如用户名在内的认证活动
cron -与 cron 和 at 有关的计划任务信息。
daemon -与 inetd 守护进程有关的后台进程信息。
kern -内核信息,首先通过 klogd 传递。
lpr -与打印服务有关的信息。
mail -与电子邮件有关的信息
mark - syslog内部功能用于生成时间戳
news -来自新闻服务器的信息
syslog -由 syslog 生成的信息
user -由用户程序生成的信息
uucp -由 uucp 生成的信息
local0-local7 -与自定义程序使用</pre>

  • 通配符代表除了 mark 以外的所有功能除mark为内部使用外,还有security为一个旧的key定义,等同于auth,已经不再建议使用。

2、level级别

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">level定义消息的紧急程度。按严重程度由高到低顺序排列为: emerg -该系统不可用,等同panic
alert -需要立即被修改的条件
crit -阻止某些工具或子系统功能实现的错误条件
err -阻止工具或某些子系统部分功能实现的错误条件,等同error
warning -预警信息,等同warn
notice -具有重要性的普通条件
info -提供信息的消息
debug -不包含函数条件或问题的其他信息
none -没有重要级,通常用于排错</pre>

  • 所有级别,除了none其中,panic、error、warn均为旧的标识符,不再建议使用。
    在定义level级别的时候,需要注意两点: 1)优先级是由应用程序在编程的时候已经决定的,除非修改源码再编译,否则不能改变消息的优先级;

低的优先级包含高优先级,例如,为某个应用程序定义info的日志导向,则涵盖notice、warning、err、crit、alert、emerg等消息。(除非使用=号定义)

3、selector选择条件

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">通过小数点符号“.”把facility和level连接在一起则成为selector(选择条件)。
可以使用分号“;”同时定义多个选择条件。也支持三个修饰符: * - 所有日志信息
= - 等于,即仅包含本优先级的日志信息
! - 不等于,本优先级日志信息除外</pre>

4、action动作

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">由前面选择条件定义的日志信息,可执行下面的动作: file-指定日志文件的绝对路径
terminal 或 print -发送到串行或并行设备标志符,例如/dev/ttyS2
@host -远程的日志服务器
username -发送信息本机的指定用户信息窗口中,但该用户必须已经登陆到系统中
named pipe -发送到预先使用 mkfifo 命令来创建的 FIFO 文件的绝对路径※注意,不能通过“|/var/xxx.sh”方式导向日志到其他脚本中处理。</pre>

5、举例

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">例如: *.info;mail.none;news.none;authpriv.none;cron.none /var/log/messages

把除邮件、新闻组、授权信息、计划任务等外的所有通知性消息都写入messages文件中。

mail,news.=info /var/adm/info

把邮件、新闻组中仅通知性消息写入info文件,其他信息不写入。

mail.*;mail.!=info /var/adm/mail

把邮件的除通知性消息外都写入mail文件中。

mail.=info /dev/tty12

仅把邮件的通知性消息发送到tty12终端设备

*.alert root,joey

如果root和joey用户已经登陆到系统,则把所有紧急信息通知他们

. @finlandia

把所有信息都导向到finlandia主机(通过/etc/hosts或dns解析其IP地址)※注意:每条消息均会经过所有规则的,并不是唯一匹配的。</pre>

也就是说,假设mail.=info信息通过上面范例中定义的规则时,/var/adm/info、/var/adm/mail、/dev/tty12,甚至finalandia主机都会收到相同的信息。这样看上去比较烦琐,但可以带来的好处就是保证了信息的完整性,可供不同地方进行分析。

二、messages日志

首先说下我们最关注的系统/var/log/messages,这东东不仅是咱们服务器的系统日志,很多时候它也做了许多服务的日志,这也是它被称为杂货铺的原因,值得重点关注,大家一般都喜欢用以下命令看最后十条日志

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">tail -n10 /var/log/messages</pre>

其实还可以将一段日志保存成文件,正用练下自己的awk、sed和grep水平;或者直接用vim来查看,这也是算是一种经验之谈吧。我以前配置bind的主从复制,有时因为权限的原因报错;这时可以在一台报错的服务器上用命令tail -f /var/log/messages实时查看服务器的变化情况,从中查找错误的蛛丝马迹;事实证明,效果很好,而且用于lvs+keepalived的排错效也不错,其它事例依此类推。

三、secure的用法

/var/log/secure:记录登入系统存取数据的文件,例如 pop3, ssh, telnet, ftp 等都会被记录,我们可以利用此文件找出不安全的登陆IP。

四、记录登陆者的数据

/var/log/wtmp:记录登入者的讯息数据,由于本文件已经被编码过(为二进制文件),所以必须使用 last指令来取出文件的内容,你用cat等命令直接查看此文件是不行的。

五、lastlog记录系统时间

/var/log/lastlog : 记录每个使用者最近签入系统的时间, 因此当使用者签入时, 就会显示其上次签入的时间,您应该注意一下这个时间, 若不是您上次签入的时间, 表示您的帐号可能被人盗用了。 此档可用 /usr/bin/lastlog 指令读取(Freebsd下为/usr/sbin/lastlogin)。

**六、用dmesg查看启动消息 **

dmesg提供了一个简单的方法查看系统启动信息。当Linux启动的时候,内核的信息被存入内核ring缓存当中,dmesg可以显示缓存中的内容。默认情况下,dmesg打印内容到屏幕上面,当然你可以重定向输出到一个文件。如果硬件损坏的话,在dmesg日志里是有显示的,可用以下命令来查看dmesg | grep error

七、服务器的邮件日志

服务器的邮件为/var/log/messages,如果要用专业的日志分析工具来分析的话,我推荐用Awstats;由于公司的开发对邮件的要求比较低,所以我帮他们配置的就是最简单的sendmail,有时看看邮件日志里的status状态来判断邮件到底有没有正确发送;在配置Nagios服务器,我也习惯用此日志来判断报警邮件到底有没有发送,如果对自己的shell水平足够自信,也可以写脚本来收集邮件服务器的返回状态等,但专业的事情,建议还是由专业的工具来做,特别是邮件负载比较大时,每天几百万条日志或上千万条日志不是开玩笑的。

八、日志文件的专业工具

像系统的一些服务,比如Apache、Nginx、Squid、还有mysql,都有自己的特定的日志文件,由于格式比较复杂,也推荐用专业工具,如Awstats、Cacti来分析,现在用cacti用得比较多是用它分析Nginx负载均衡器的一段时间的并发情况。

九、输出Iptables日志到一个指定的文件(logboy同学撰写)

Iptables的man参考页中提到: 我们可以使用Iptables在Linux内核中建立, 维护和检查IP包过滤规则表。几个不同的表可能已经创建, 每一个表包含了很多内嵌的链, 也可能包含用户自定义的链。Iptables默认把日志信息输出到/var/log/messages文件。不过一些情况下你可能需要修改日志输出的位置。下面向大家介绍如何建立一个新的日志文件/var/log/iptables.log。通过修改或使用新的日志文件, 你可以创建更好的统计信息或者帮助你分析网络攻击信息。

(1). Iptables默认的日志文件

例如, 如果你输入下面的命令, 屏幕将显示/var/log/messages文件中的Iptables日志信息:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># tail -f /var/log/messages</pre>

输出:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">Oct 4 00:44:28 debian gconfd (vivek-4435):
Resolved address "xml:readonly:/etc/gconf/gconf.xml.defaults"
to a read-only configuration source at position 2
Oct 4 01:14:19 debian kernel:
IN=ra0 OUT= MAC=00:17:9a:0a:f6:44:00:08:5c:00:00:01:08:00
SRC=200.142.84.36 DST=192.168.1.2
LEN=60 TOS=0x00 PREC=0x00 TTL=51 ID=18374
DF PROTO=TCP SPT=46040 DPT=22 WINDOW=5840 RES=0x00 SYN URGP=0 </pre>

(2).输出Iptables日志信息到一个指定文件的方法

打开你的/etc/syslog.conf文件:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># vi /etc/syslog.conf</pre>

在文件末尾加入下面一行信息:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">kern.warning /var/log/iptables.log</pre>

保存和关闭文件。

重新启动Syslogd(如果你使用Debian/Ubuntu Linux):

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># /etc/init.d/sysklogd restart</pre>

另外, 使用下面命令重新启动Syslogd(如果你使用Red Hat/Cent OS/Fedora Core Linux):

/etc/init.d/syslog restart现在确认你的Iptables使用了log-level 4参数(前面有一个log-prefix标志)。例如:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># DROP everything and Log it
iptables -A INPUT -j LOG –log-level 4
iptables -A INPUT -j DROP</pre>

举一个例子, 丢弃和记录所有来自IP地址65.55.11.2的连接信息到/var/log/iptables.log文件。

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">iptables -A INPUT -s 64.55.11.2 -m limit
limit 5/m --limit-burst 7 -j LOG
log-prefix ‘** HACKERS **’ --log-level 4
iptables -A INPUT -s 64.55.11.2 -j DROP </pre>

命令解释:

log-level 4: 记录的级别. 级别4为警告(warning)。

log-prefix ‘*** TEXT ***’: 这里定义了在日志输出信息前加上TEXT前缀。TEXT信息最长可以是29个字符, 这样你就可以在记录文件中方便找到相关的信息。

现在你可以通过/var/log/iptables.log文件参考Iptables的所有信息:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># tail -f /var/log/iptables.log</pre>

十、日志轮询

再说下日志的轮询,Linux的日志轮询机制做得相当好,当然我们也可以通过配置/etc/logrotate.conf来修改它,有兴趣的同学可参考鸟哥的文章,这里就不作详细介绍了。
一、配置syslog(gyl4802959同学撰写)

目前,linux依旧使用syslogd作为日志监控进程,对其进行必要的配置能减少很多麻烦,并且可更有效的从系统日志监控到系统的状态。理解并完善一个syslog的配置,对于系统管理员来说显得尤为重要。
/etc/syslog.conf根据如下的格式定义规则: facility.level action
设备.优先级 动作facility.level 字段也被称为seletor(选择条件),选择条件和动作之间用空格或tab分割开。

号开头的是注释,空白行会自动跳过。

1、facility

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">facility定义日志消息的范围,其可使用的key有: auth -由 pam_pwdb 报告的认证活动。
authpriv -包括特权信息如用户名在内的认证活动
cron -与 cron 和 at 有关的计划任务信息。
daemon -与 inetd 守护进程有关的后台进程信息。
kern -内核信息,首先通过 klogd 传递。
lpr -与打印服务有关的信息。
mail -与电子邮件有关的信息
mark - syslog内部功能用于生成时间戳
news -来自新闻服务器的信息
syslog -由 syslog 生成的信息
user -由用户程序生成的信息
uucp -由 uucp 生成的信息
local0-local7 -与自定义程序使用</pre>

  • 通配符代表除了 mark 以外的所有功能除mark为内部使用外,还有security为一个旧的key定义,等同于auth,已经不再建议使用。

2、level级别

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">level定义消息的紧急程度。按严重程度由高到低顺序排列为: emerg -该系统不可用,等同panic
alert -需要立即被修改的条件
crit -阻止某些工具或子系统功能实现的错误条件
err -阻止工具或某些子系统部分功能实现的错误条件,等同error
warning -预警信息,等同warn
notice -具有重要性的普通条件
info -提供信息的消息
debug -不包含函数条件或问题的其他信息
none -没有重要级,通常用于排错</pre>

  • 所有级别,除了none其中,panic、error、warn均为旧的标识符,不再建议使用。
    在定义level级别的时候,需要注意两点: 1)优先级是由应用程序在编程的时候已经决定的,除非修改源码再编译,否则不能改变消息的优先级;

低的优先级包含高优先级,例如,为某个应用程序定义info的日志导向,则涵盖notice、warning、err、crit、alert、emerg等消息。(除非使用=号定义)

3、selector选择条件

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">通过小数点符号“.”把facility和level连接在一起则成为selector(选择条件)。
可以使用分号“;”同时定义多个选择条件。也支持三个修饰符: * - 所有日志信息
= - 等于,即仅包含本优先级的日志信息
! - 不等于,本优先级日志信息除外</pre>

4、action动作

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">由前面选择条件定义的日志信息,可执行下面的动作: file-指定日志文件的绝对路径
terminal 或 print -发送到串行或并行设备标志符,例如/dev/ttyS2
@host -远程的日志服务器
username -发送信息本机的指定用户信息窗口中,但该用户必须已经登陆到系统中
named pipe -发送到预先使用 mkfifo 命令来创建的 FIFO 文件的绝对路径※注意,不能通过“|/var/xxx.sh”方式导向日志到其他脚本中处理。</pre>

5、举例

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">例如: *.info;mail.none;news.none;authpriv.none;cron.none /var/log/messages

把除邮件、新闻组、授权信息、计划任务等外的所有通知性消息都写入messages文件中。

mail,news.=info /var/adm/info

把邮件、新闻组中仅通知性消息写入info文件,其他信息不写入。

mail.*;mail.!=info /var/adm/mail

把邮件的除通知性消息外都写入mail文件中。

mail.=info /dev/tty12

仅把邮件的通知性消息发送到tty12终端设备

*.alert root,joey

如果root和joey用户已经登陆到系统,则把所有紧急信息通知他们

. @finlandia

把所有信息都导向到finlandia主机(通过/etc/hosts或dns解析其IP地址)※注意:每条消息均会经过所有规则的,并不是唯一匹配的。</pre>

也就是说,假设mail.=info信息通过上面范例中定义的规则时,/var/adm/info、/var/adm/mail、/dev/tty12,甚至finalandia主机都会收到相同的信息。这样看上去比较烦琐,但可以带来的好处就是保证了信息的完整性,可供不同地方进行分析。

二、messages日志

首先说下我们最关注的系统/var/log/messages,这东东不仅是咱们服务器的系统日志,很多时候它也做了许多服务的日志,这也是它被称为杂货铺的原因,值得重点关注,大家一般都喜欢用以下命令看最后十条日志

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">tail -n10 /var/log/messages</pre>

其实还可以将一段日志保存成文件,正用练下自己的awk、sed和grep水平;或者直接用vim来查看,这也是算是一种经验之谈吧。我以前配置bind的主从复制,有时因为权限的原因报错;这时可以在一台报错的服务器上用命令tail -f /var/log/messages实时查看服务器的变化情况,从中查找错误的蛛丝马迹;事实证明,效果很好,而且用于lvs+keepalived的排错效也不错,其它事例依此类推。

三、secure的用法

/var/log/secure:记录登入系统存取数据的文件,例如 pop3, ssh, telnet, ftp 等都会被记录,我们可以利用此文件找出不安全的登陆IP。

四、记录登陆者的数据

/var/log/wtmp:记录登入者的讯息数据,由于本文件已经被编码过(为二进制文件),所以必须使用 last指令来取出文件的内容,你用cat等命令直接查看此文件是不行的。

五、lastlog记录系统时间

/var/log/lastlog : 记录每个使用者最近签入系统的时间, 因此当使用者签入时, 就会显示其上次签入的时间,您应该注意一下这个时间, 若不是您上次签入的时间, 表示您的帐号可能被人盗用了。 此档可用 /usr/bin/lastlog 指令读取(Freebsd下为/usr/sbin/lastlogin)。

**六、用dmesg查看启动消息 **

dmesg提供了一个简单的方法查看系统启动信息。当Linux启动的时候,内核的信息被存入内核ring缓存当中,dmesg可以显示缓存中的内容。默认情况下,dmesg打印内容到屏幕上面,当然你可以重定向输出到一个文件。如果硬件损坏的话,在dmesg日志里是有显示的,可用以下命令来查看dmesg | grep error

七、服务器的邮件日志

服务器的邮件为/var/log/messages,如果要用专业的日志分析工具来分析的话,我推荐用Awstats;由于公司的开发对邮件的要求比较低,所以我帮他们配置的就是最简单的sendmail,有时看看邮件日志里的status状态来判断邮件到底有没有正确发送;在配置Nagios服务器,我也习惯用此日志来判断报警邮件到底有没有发送,如果对自己的shell水平足够自信,也可以写脚本来收集邮件服务器的返回状态等,但专业的事情,建议还是由专业的工具来做,特别是邮件负载比较大时,每天几百万条日志或上千万条日志不是开玩笑的。

八、日志文件的专业工具

像系统的一些服务,比如Apache、Nginx、Squid、还有mysql,都有自己的特定的日志文件,由于格式比较复杂,也推荐用专业工具,如Awstats、Cacti来分析,现在用cacti用得比较多是用它分析Nginx负载均衡器的一段时间的并发情况。

九、输出Iptables日志到一个指定的文件(logboy同学撰写)

Iptables的man参考页中提到: 我们可以使用Iptables在Linux内核中建立, 维护和检查IP包过滤规则表。几个不同的表可能已经创建, 每一个表包含了很多内嵌的链, 也可能包含用户自定义的链。Iptables默认把日志信息输出到/var/log/messages文件。不过一些情况下你可能需要修改日志输出的位置。下面向大家介绍如何建立一个新的日志文件/var/log/iptables.log。通过修改或使用新的日志文件, 你可以创建更好的统计信息或者帮助你分析网络攻击信息。

(1). Iptables默认的日志文件

例如, 如果你输入下面的命令, 屏幕将显示/var/log/messages文件中的Iptables日志信息:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># tail -f /var/log/messages</pre>

输出:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">Oct 4 00:44:28 debian gconfd (vivek-4435):
Resolved address "xml:readonly:/etc/gconf/gconf.xml.defaults"
to a read-only configuration source at position 2
Oct 4 01:14:19 debian kernel:
IN=ra0 OUT= MAC=00:17:9a:0a:f6:44:00:08:5c:00:00:01:08:00
SRC=200.142.84.36 DST=192.168.1.2
LEN=60 TOS=0x00 PREC=0x00 TTL=51 ID=18374
DF PROTO=TCP SPT=46040 DPT=22 WINDOW=5840 RES=0x00 SYN URGP=0 </pre>

(2).输出Iptables日志信息到一个指定文件的方法

打开你的/etc/syslog.conf文件:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># vi /etc/syslog.conf</pre>

在文件末尾加入下面一行信息:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">kern.warning /var/log/iptables.log</pre>

保存和关闭文件。

重新启动Syslogd(如果你使用Debian/Ubuntu Linux):

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># /etc/init.d/sysklogd restart</pre>

另外, 使用下面命令重新启动Syslogd(如果你使用Red Hat/Cent OS/Fedora Core Linux):

/etc/init.d/syslog restart现在确认你的Iptables使用了log-level 4参数(前面有一个log-prefix标志)。例如:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># DROP everything and Log it
iptables -A INPUT -j LOG –log-level 4
iptables -A INPUT -j DROP</pre>

举一个例子, 丢弃和记录所有来自IP地址65.55.11.2的连接信息到/var/log/iptables.log文件。

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">iptables -A INPUT -s 64.55.11.2 -m limit
limit 5/m --limit-burst 7 -j LOG
log-prefix ‘** HACKERS **’ --log-level 4
iptables -A INPUT -s 64.55.11.2 -j DROP </pre>

命令解释:

log-level 4: 记录的级别. 级别4为警告(warning)。

log-prefix ‘*** TEXT ***’: 这里定义了在日志输出信息前加上TEXT前缀。TEXT信息最长可以是29个字符, 这样你就可以在记录文件中方便找到相关的信息。

现在你可以通过/var/log/iptables.log文件参考Iptables的所有信息:

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># tail -f /var/log/iptables.log</pre>

十、日志轮询

再说下日志的轮询,Linux的日志轮询机制做得相当好,当然我们也可以通过配置/etc/logrotate.conf来修改它,有兴趣的同学可参考鸟哥的文章,这里就不作详细介绍了。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容

  • 1)/var/log/secure:记录登录系统存取数据的文件; 例如:pop3,ssh,telnet,ftp等都...
    云扬_fb42阅读 2,756评论 0 1
  • 1 概述 系统日志是记录系统中硬件、软件和系统问题的信息,同时还可以监视系统中发生的事件。用户可以通过它来检查错误...
    ghbsunny阅读 3,730评论 0 0
  • 我们主要讲一下Linux环境中的系统记帐和系统日志管理以及怎么用一些工具更加方便有效的管理日志信息。 当我们用上面...
    大福技术阅读 4,217评论 0 3
  • utmp、wtmp和lastlog日志文件是多数重用UNIX日志子系统的关键--保持用户登录进入和退出的纪录。 1...
    Drafei阅读 21,062评论 1 0
  • 了解日志文件是很重要的事情。日志文件可以记录系统在什么时间、哪台主机、哪个服务、出现了什么信息等。可以在系统出现问...
    Zhang21阅读 5,265评论 0 5