CentOS下管理日志服务详解

2020-01-30 16:14:58于丽

[root@localhost ~]# tail -f /var/log/messages

Oct 21 04:48:35 localhost avahi-daemon[3152]: Joining mDNS multicast group on interface eth0.IPv4 with address 192.168.80.130.

Oct 21 04:48:35 localhost avahi-daemon[3152]: Registering new address record for 192.168.80.130 on eth0.

Oct 21 04:48:35 localhost NET[4094]: /sbin/dhclient-script : updated /etc/resolv.conf

Oct 21 04:48:35 localhost dhclient: bound to 192.168.80.130 -- renewal in 760 seconds.

Oct 21 05:01:15 localhost dhclient: DHCPREQUEST on eth0 to 192.168.80.254 port 67

Oct 21 05:01:15 localhost dhclient: DHCPACK from 192.168.80.254

Oct 21 05:01:15 localhost dhclient: bound to 192.168.80.130 -- renewal in 723 seconds.

Oct 21 05:13:18 localhost dhclient: DHCPREQUEST on eth0 to 192.168.80.254 port 67

Oct 21 05:13:18 localhost dhclient: DHCPACK from 192.168.80.254

Oct 21 05:13:18 localhost dhclient: bound to 192.168.80.130 -- renewal in 749 seconds.

Oct 21 05:22:58 localhost kernel: Installing knfsd (copyright (C) 1996 okir@monad.swb.de).

Oct 21 05:22:58 localhost kernel: NFSD: Using /var/lib/nfs/v4recovery as the NFSv4 state recovery directory

Oct 21 05:22:58 localhost kernel: NFSD: starting 90-second grace period

大部分日志文件中所使用的日志记录格式都是相同的。下面以公共日志文件“/var/log/messages”为例来说明日志记录的基本格式。

日志文件中的每一行表示一条消息,每个消息均由四个字段的固定格式组成。

时间标签:消息发出的日期和时间。

主机名:生成消息的计算机的名称。

子系统名称:发出消息的应用程序的名称。

消息:消息的具体内容。

例如上面所显示的最后一条消息:

Oct 21 05:22:58 localhost kernel: NFSD: starting 90-second grace period

这条消息的含义为:

10月21日05:22:58在localhost这台主机上,由内核kernel中的nfsd服务所产生的信息,信息的内容为“starting 90-second grace period”。管理日志服务

RHEL系统中的内核及系统日志功能主要由rsyslogd服务提供,该服务的配置文件为"/etc/rsyslog.conf"。

rsyslogd服务默认已经安装并自动运行,/etc/rsyslog.conf配置文件则可以对日志进行设置,它可以指定那些信息需要记录,以及记录在哪里。

例:查看/etc/rsyslog.conf配置文件中的主要内容。

/etc/rsyslog.conf文件中的每一行代表一条设置值,每一条设置值的语法如下:

        消息类型   执行动作

其中的"消息类型"指定哪些消息需要记录,"执行动作"则告诉系统日志服务该如何处理这些消息。

"消息类型"必须以下列的格式指定消息的种类: