Linux日志系统小记


概述:最近做ssh无密码认证实验时,在ssh服务配置文件中发现 authpriv,当时有种似曾相识的感觉。干脆就是复习了下syslogd这个daemon,又发现在Redhat 6 中syslogd改名为rsyslogd.

博客是以 centos 6.6为例

  • rsyslog简介

  • rsyslog的配置文件rsyslog.conf说明

  • rsyslog配置文件语法格式

  • rsyslog启动方法

  • 系统其它日志和应用程序日志

  • 日志消息来源和等级英文参考

 

rsyslog简介

rsyslog 是在redhat 6.x以后使用的系统日志系统,其进程名为 rsyslogd.(在redhat 5.x中日志系统为syslog进程名为syslogd,本人当时man syslog 和 man rsyslogd 其实也差不多,没有多大改进)。UNIX和Linux操作系统都是采用rsyslog进行系统日志的管理和配置。Linux系统内核和许多程序会产生各种错误信息、警告信息和其他的提示信息。管理员了解系统的运行状态是很有用的,所以应该根据信息的来源以及信息的重要程度把它们写到不同日志文件中去。而执行这个过程的程序就是rsyslog。

默认情况下linux日志都保存在/var/log目录。

 

rsyslog的配置文件rsyslog.conf

 

[root@zabbix ~]# cat /etc/rsyslog.conf         

默认rsyslog.conf配置文件比syslog.conf多这里以rsyslog.conf为例

# rsyslog v5 configuration file

# For more information see/usr/share/doc/rsyslog-*/rsyslog_conf.html

# If you experience problems, seehttp://www.rsyslog.com/doc/troubleshoot.html

#以上为配置文件说明

#### MODULES ####

$ModLoad imuxsock # provides support for local system logging(e.g. via logger command)

$ModLoad imklog   #provides kernel logging support (previously done by rklogd)

#$ModLoad immark  #provides --MARK-- message capability

# Provides UDP syslog reception

#$ModLoad imudp

#$UDPServerRun 514

# Provides TCP syslog reception

#$ModLoad imtcp

#$InputTCPServerRun 514

#默认加载的模块,基本上不需要改。

 

#### GLOBAL DIRECTIVES ####

# Use default timestamp format

$ActionFileDefaultTemplateRSYSLOG_TraditionalFileFormat

# File syncing capability is disabled by default. This featureis usually not required,

# not useful and an extreme performance hit

#$ActionFileEnableSync on

# Include all config files in /etc/rsyslog.d/

$IncludeConfig /etc/rsyslog.d/*.conf

#上为全局配置,基本上不需要改。

 

#配置文件接下来的 规则rule 是我们最关心的,它关系到日志文件存放在哪,怎么归类之类,你下想你/var/log/下一般会看到很文件,他们都是归好类的。

#### RULES ####

# Log all kernel messages to the console.

# Logging much else clutters up the screen.

#kern.*                                                /dev/console

#

# Log anything (except mail) of level info or higher.

# Don't log private authentication messages!

*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# 把除邮件、授权和定时任务以外的其他info级别的信息记录到/var/log/messages日志文件中

# The authpriv file has restricted access.

authpriv.*                                             /var/log/secure

# 把所有授权信息记录到/var/log/secure日志文件中,这也是为什么用户登录记录会在这个文件了,你可以tail/var/log/secure 查看你最近的登录记录。

# Log all the mail messages in one place.

mail.*                                                  -/var/log/maillog

# 把所有级别的邮件信息记录到/var/log/maillog日志文件中

# Log cron stuff

cron.*                                                 /var/log/cron

# 把所有级别的定时任务信息记录到/var/log/cron日志文件中

# Everybody gets emergency messages

*.emerg                                                 *

# 把emerg级别的信息发送给所有登录用户

# Save news errors of level crit and higher in a special file.

uucp,news.crit                                         /var/log/spooler

#uccp子系统和重要网络新闻子系统信息记录到/var/log/spooler日志文件中

# Save boot messages also to boot.log

local7.*                                               /var/log/boot.log

# 把所有的系统启动信息记录到/var/log/boot.log日志文件中,可以看到开机是启动那些东东。

 

#配置文件以下部分为转发规则forwarding rule,你想想啊,每台linux服务器都记录了自己的日志,几台还好,多了话难到要一台一台的登录上去看吗?rsyslog很聪明他已经想好了。可以转发到一台上,分析这很容易罗。

# ### begin forwarding rule ###

# The statement between the begin ... end define a SINGLEforwarding

# rule. They belong together, do NOT split them. If you createmultiple

# forwarding rules, duplicate the whole block!

# Remote Logging (we use TCP for reliable delivery)

# An on-disk queue is created for this action. If the remotehost is

# down, messages are spooled to disk and sent when it is upagain.

#$WorkDirectory /var/lib/rsyslog # where to place spool files

#$ActionQueueFileName fwdRule1 # unique name prefix for spoolfiles

#$ActionQueueMaxDiskSpace 1g  # 1gb space limit (use as much as possible)

#$ActionQueueSaveOnShutdown on # save messages to disk onshutdown

#$ActionQueueType LinkedList  # run asynchronously

#$ActionResumeRetryCount -1   # infinite retries if host is down

# remote host is: name/ip:port, e.g. 192.168.0.1:514, portoptional

#*.* @@remote-host:514

#去掉#号生效  *.*为所有日志型 ,所以以上行意思为 这台服务器连接到远程remote-host的514端口,并将所有日志转发一份到远程 remote-host这台服务器。

# ### end of the forwarding rule ###

 

# A template to for higher precision timestamps + severitylogging

$templateSpiceTmpl,"%TIMESTAMP%.%TIMESTAMP:::date-subseconds% %syslogtag%%syslogseverity-text%:%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"

 

:programname, startswith, "spice-vdagent"/var/log/spice-vdagent.log;SpiceTmpl

 

rsyslog配置文件语法格式

[消息来源.消息级别]   [动作]  [facility.priority] [action]

[消息来源.消息级别][动作]之间以Tab键进行分隔,同一行rsyslog配置中允许出现多个[消息来源.消息级别],但必须要使用分号“;”进行分隔,

例如:mail.*; cron.*       /var/log/test.log

消息来源表示发出消息的子系统。

rsyslog消息来源及说明

 

   

 

   

authpriv

安全/授权信息

mail

邮件子系统

cron

定时任务

news

网络新闻子系统

daemon

守护进程

syslog

syslogd内部产生的信息

ftp

ftp守护进程

user

一般用户级别信息

kern

内核信息

uucp

UUCP子系统

lpr

打印机子系统

local0-local7

本地用户

优先级代表消息的紧急程度,如下表所示按级别由高到低列出了消息级别及说明。

rsyslog消息级别及说明

 

   

 

   

emerg

最紧急的消息

warning

警告消息

alert

紧急消息

notice

普通但重要的消息

crit

重要消息

info

通知性消息

err

出错消息

debug

调试级的消息——消息量最多

rsyslog消息级别是向上匹配的,也就是说如果指定了一个消息级别,那么指定级别及比该指定级别更高级的消息都会被包括进去。例如,warning表示所有大于或者等于warning级别的消息都会被处理,包括emergalertcriterrwarning。如果指定的是err级别,那么就只是所有错误信息。

如果只想匹配某个确定级别的消息,而不希望包括更高级别的消息,可以使用等号“=”进行指定。例如希望处理cronnotice级别的消息:cron.=notice       /var/log/test.log

除此之外,rsyslog还支持两个特殊的消息级别关键字:”!=” “*”以及none。其中”!=”为不等于,“*”表示匹配所有来源或级别的消息,none表示忽略所有消息。

[动作]

是用于指定消息的处理方式。rsyslog支持把消息保存到日志文件中、发送给指定的用户、显示在终端上或者通过网络发送到另外一台rsyslog服务器上进行处理。表中列出了所有可用的动作及其说明。

   

   

文件名

将消息保存到指定的文件中

@主机名或IP地址

转发消息到另外一台syslog服务器上进行处理

*

把消息发送到所有用户的终端上

/dev/console

把消息发送到本地主机的终端上

| 程序

通过管道把消息重定向到指定的程序

用户名列表

把消息发送给指定的用户,用户名以逗号“,”进行分隔

 

rsyslog启动方法

1,  以chkconfig方式来开机自动启动chkconfig  rsyslog on/off

2,  以 service 方式 servicersyslog start|stop|restart|reload|force-reload|status

3,  以 killall方式  killall –HUP rsyslogd

 

系统其它日志和应用程序日志

Linux系统中有些程序进程自己单独产生应用程序日志系统,(如http日志在 /var/log/http/ 这个目录,由其进本身生成),另外系统还提供了大量的其他日志文件dmesg、wtmp、btmp、.bash_history。

dmesg日志:记录内核日志信息

日志文件/var/log/dmesg中记录了系统启动过程中的内核日志信息,包括系统的设备信息,以及在启动和操作过程中系统记录的任何错误和问题的信息,例如,用户新添加了一个磁盘,如果该磁盘设备能被Linux系统正确识别,那么在dmesg日志文件中应该能够看到它的信息。

wtmp和btmp用户登录日志

/var/log/wtmp和/var/log/btmp是Linux系统上用于保存用户登录信息的日志文件。其中wtmp用于保存用户成功登录的记录,而btmp则用于保存用户登录失败的日志记录,它们为系统安全审计提供了重要的信息依据。这两个文件都是二进制的,无法直接使用文本编辑工具打开,必须通过last和lastb命令进行查看。

查看成功的用户登录记录用last,查看失败用户登录记录用lastb。


Linux日志系统小记_第1张图片

Linux日志系统小记_第2张图片


日志消息来源和等级英文参考:

The  facility is one of the following keywords: auth, authpriv, cron, daemon,kern, lpr,

mail, mark, news, security(same as auth), syslog, user, uucp and local0 through local7.

The  keyword security  should not be usedanymore and mark is only for internal use and

therefore should not be usedin applications.  Anyway, you may want tospecify and redi-

rect  these messages here.  The facilityspecifies the subsystem that produced the mes-

sage, i.e. all mail programslog with the mail facility (LOG_MAIL) if  they  log using

syslog.

 

The  priority is one of the following keywords, in ascending order: debug,info, notice,

warning, warn (same aswarning), err, error (same as err), crit,  alert,  emerg, panic

(same  as emerg).   The keywords error, warnand panic are deprecated and should not be

used anymore.  The priority defines the severity of themessage


本文是 巧妙绝情 一个字一个图打出来,参考了好多资料,感谢他们的分享,基于open source分享精神,转载请注明出出。
支持我,请点击 巧妙绝情 谢谢