了解

日志文件记录了时间,地点,人物,事件四大信息,故系统出现故障时,可以查询日志文件。
系统的日志文件默认都集中放置到/var/log/目录内,其中又以message记录的信息最多。
日志文件的重要性主要体现在以下三方面:

解决系统方面的错误
解决网络服务的问题
过往事件记录


日志文件的权限通常是设置为root能够读取而已。由于日志文件可以记载系统很多的详细信息,所以一个有经验的主机管理员会随时随地查阅自己的日志文件,以掌握系统的最新动态。

注意:系统断电没日志。

版本

rhel-server-5.4
sysklogd 5版本的包名 syslogd: system application 记录应用日志 klogd: linux kernel 记录内核日志
事件记录格式:日期时间 主机 进程[pid]: 事件内容C/S架构:通过TCP或UDP协议的服务完成日志记录传送,将分布在不同主机的日志实现集中管理
CentOS 6~7
centos6 [root@centos6 ~]#ps aux |grep syslog root 1654 0.0 0.1 249160 1680 ?
Sl20:28 0:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5 root 2381 0.0 0.0
103336 852 pts/0 S+ 20:38 0:00 grep syslog [root@centos6 ~]#which rsyslogd
/sbin/rsyslogd [root@centos6 ~]#rpm -qf /sbin/rsyslogd rsyslog-5.8.10-10.el6_6.x
86_64 [root@centos6 ~]#yum info rsyslog-5.8.10-10.el6_6.x86_64 Loaded plugins:
fastestmirror, refresh-packagekit, security Loading mirror speeds from cached
hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates:
mirrors.aliyun.com Installed Packages Name : rsyslog Arch : x86_64 Version : 5.8
.10 Release : 10.el6_6 Size : 2.1 M Repo : installed From repo :
anaconda-CentOS-201703281317.x86_64 Summary : Enhanced system logging and
kernel message trapping daemons URL : http://www.rsyslog.com/ License : (GPLv3+
and ASL 2.0) Description : Rsyslog is an enhanced, multi-threaded syslog
daemon. It supports MySQL, : syslog/TCP, RFC3195, permitted sender lists,
filtering on any message part, :and fine grain output format control. It is
compatible with stock sysklogd :and can be used as a drop-in replacement.
Rsyslog is simple toset up, with : advanced features suitable for
enterprise-class, encryption-protected syslog : relay chains. centos7 [root@ ~]
#ps aux |grep rsyslogd root 606 0.0 0.1 216392 4380 ? Ssl 20:00 0:00
/usr/sbin/rsyslogd -n root3586 0.0 0.0 112664 976 pts/0 S+ 20:32 0:00 grep
--color=auto rsyslogd [root@ ~]#which rsyslogd /usr/sbin/rsyslogd [root@ ~]
#rpm -qf /usr/sbin/rsyslogd rsyslog-8.24.0-12.el7.x86_64 [root@ ~]#yum info
rsyslog-8.24.0-12.el7.x86_64 Loaded plugins: fastestmirror, langpacks Loading
mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors
.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com
Installed Packages Name : rsyslog Arch : x86_64 Version :8.24.0 Release : 12.
el7 Size :1.9 M Repo : installed From repo : anaconda Summary : Enhanced system
loggingand kernel message trapping daemon URL : http://www.rsyslog.com/ License
: (GPLv3+and ASL 2.0) Description : Rsyslog is an enhanced, multi-threaded
syslog daemon. It supports MySQL, : syslog/TCP, RFC3195, permitted sender
lists, filtering on any message part, :and fine grain output format control. It
is compatible with stock sysklogd :and can be used as a drop-in replacement.
Rsyslog is simple toset up, with : advanced features suitable for
enterprise-class, encryption-protected syslog : relay chains. rsyslog CentOS
6~7版本的包名 rsyslog特性: 多线程 支持UDP, TCP, SSL, TLS, RELP 支持MySQL, PGSQL, Oracle实现日志存储
强大的过滤器,可实现过滤记录日志信息中任意部分 自定义输出格式 ELK:elasticsearch, logstash,
kibana(日志分析系统,三款开源产品名称的首字母集合) Elasticsearch是个开源分布式搜索引擎
Logstash对日志进行收集、分析,并将其存储供以后使用 kibana 可以提供的日志分析友好的Web界面 非关系型分布式数据库
基于apache软件基金会jakarta项目组的项目lucene
主要内容

以下主要介绍的是rsyslog
术语,参见man logger facility:设施,从功能或程序上对日志进行归类 auth #验证 已弃用使用 authpriv #授权 cron
#计划任务 daemon #守护进程 ftp kern lpr #打印 mail news security(auth) #安全 user uucp
local0-local7 #自定义 syslog Priority 优先级别,从低到高排序 debug #适用于调试什么都记录 info #记录相对重要的
notice#重要通知 warn(warning) #警报快出错 err(error) #报错 crit(critical) #临界 alert #警觉的
emerg(panic)#彻底崩溃了 例:如果是info(它包括notice, warn(warning), err(error),
crit(critical), alert, emerg(panic)),只记录优先级高的,不记录优先级低的 参看帮助:man3 syslog
程序包、配置文件
程序包:rsyslog 主程序:/usr/sbin/rsyslogd CentOS 6:service
rsyslog{start|stop|restart|status} CentOS7:/usr/lib/systemd/system/rsyslog
.service 配置文件:/etc/rsyslog.conf,/etc/rsyslog.d/*.conf 库文件:/lib64/rsyslog/*.so
配置文件格式:由三部分组成 MODULES:相关模块配置 GLOBAL DIRECTIVES:全局配置 RULES:日志记录相关的规则配置
日志滚动配置文件:/etc/logrotate.d/syslog
RULES格式
RULES配置格式: facility.priority facility.priority... target facility: *:
所有的facility facility1,facility2,facility3,...:指定的facility列表 priority: *: 所有级别
none:没有级别,即不记录 PRIORITY:指定级别(含)以上的所有级别 =PRIORITY:仅记录指定级别的日志信息 target:
文件路径:通常在/var/log/,文件路径前的"-"表示异步写入 用户:将日志事件通知给指定的用户,* 表示登录的所有用户
日志服务器:@host,把日志送往至指定的远程服务器记录 管道:| COMMAND,转发给其它命令处理
rsyslog.conf详解
# rsyslog v5 configuration file # For more information see
/usr/share/doc/rsyslog-*/rsyslog_conf.html # If you experience problems, see
http://www.rsyslog.com/doc/troubleshoot.html #### MODULES #### # 模块 $ModLoad
imuxsock# provides support for local system logging (e.g. via logger command) #
支持本地系统日志记录(例如通过logger命令) $ModLoad imklog # provides kernel logging support
(previously done by rklogd) # 提供内核日志记录支持(以前由rklogd完成) #$ModLoad immark #
provides --MARK-- message capability # 提供了--MARK--消息功能 # Provides UDP syslog
reception # 提供UDP系统日志接收 #$ModLoad imudp #$UDPServerRun 514 # Provides TCP
syslog reception # 提供TCP系统日志接收 #$ModLoad imtcp #$InputTCPServerRun 514 ####
GLOBAL DIRECTIVES #### # 全局设置 # Use default timestamp format # 使用默认的时间戳格式
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat# File syncing
capability is disabled by default. This feature is usually not required, #
文件同步功能默认是关闭的。这个功能通常不是必需的 # not useful and an extreme performance hit
#$ActionFileEnableSync on # Include all config files in /etc/rsyslog.d/ #
将所有配置文件包含在/etc/rsyslog.d/中 $IncludeConfig /etc/rsyslog.d/*.conf #### 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级别的日志信息 排除mail authpriv cron # The authpriv file has
restricted access. # authpriv文件具有受限制的访问权限。 authpriv.* /var/log/secure # Log all
the mail messages in one place. # 将所有的邮件记录在一个地方。 mail.* -/var/log/maillog #
-/var/log/maillog 前面"-"是:代表异步写入,邮件量大占I/O在系统不忙时做 # Log cron stuff # 日志计划任务的东西
cron.* /var/log/cron # Everybody gets emergency messages # 每用户都得到紧急消息 *.emerg *
# Save news errors of level crit and higher in a special file. uucp,news.crit /
var/log/spooler # Save boot messages also to boot.log # 将启动信息保存到boot.log
只记录当前系统启动日志 local7.* /var/log/boot.log # ### begin forwarding rule ### # 开始转发规则
# The statement between the begin ... end define a SINGLE forwarding # rule.
They belong together, do NOT split them. If you create multiple # 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 remote host is
# down, messages are spooled to disk and sent when it is up again.
#$WorkDirectory /var/lib/rsyslog # where to place spool files
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$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, port optional #*.* @@remote-host:514 # ### end of the
forwarding rule ### 转发规则结束
举例:以sshd服务为例:
[root@centos6 ~]#vim /etc/ssh/sshd_config SyslogFacility AUTHPRIV #
/etc/ssh/sshd_config 文件36行处sshd定义AUTHPRIV -> facility # 而facility 在
/etc/rsyslog.conf 文件45行处 定义了级别 [root@centos6 ~]#vim /etc/rsyslog.conf
authpriv.* /var/log/secure # 所以/var/log/secure此文件里面定义了sshd的日志 sshd服务单独放单到一个文件中
先修改/etc/rsyslog.conf#约62行 [root@centos6 ~]#vim /etc/rsyslog.conf local7.* /var
/log/boot.log local0.* /var/log/sshd.log # 这里需确定local0没人用 之后修改此文件
/etc/ssh/sshd_config [root@centos6 ~]#vim /etc/ssh/sshd_config SyslogFacility
AUTHPRIV# SyslogFacility local0 (AUTHPRIV 修改为local0 即可 ) 最后重新启动rsyslog sshd服务即可
测试工具logger
给单独用户发消息 先修改/etc/rsyslog.conf #约62行 [root@centos6 ~]#vim /etc/rsyslog.conf
local7.* /var/log/boot.log local0.* zhang,root [root@centos6 ~]#logger -p
local0.info "This is a test log" # 执行此命令root和zhang都能收到This is a test log消息,前提是在线
# zhang,root换成"*" 是通知所有在线的用户
日志的远程存储
通常的日志格式: 事件产生的日期时间 主机 进程(pid):事件内容 如:/var/log/messages,cron,secure等
配置rsyslog成为日志服务器#### MODULES #### # Provides UDP syslog reception $ModLoadimudp
$UDPServerRun514 # Provides TCP syslog reception $ModLoadimtcp
$InputTCPServerRun514
UDP

现在准备两台虚拟机,为了区分一个是 centos 6一个是 centos 7
目的:把centos 6 日志 远程记录到 centos 7 中
centos 7 上操作打开 udp514端口 注意服务器的防火墙别屏蔽 iptables -vnL # 查看防火墙 iptables -F #
这里我们把它清空 [root@centos7 ~]#vim /etc/rsyslog.conf $ModLoad imudp $UDPServerRun 514
# 15~16行去掉注释 打开udp514端口 # systemctl restart rsyslog # 重新启动rsyslog # ss -ntlpu
|grep 514 #查看是否打开udp514端口 centos 6上操作 先修改/etc/rsyslog.conf #约42行 [root@centos6
~]#vim /etc/rsyslog.conf *.info;mail.none;authpriv.none;cron.none /var/log
/messages*.info;mail.none;authpriv.none;cron.none @172.18.2.232 #
/var/log/messages 路径修改为目标主机@172.18.2.232 # systemctl restart rsyslog #
重新启动rsyslog # 效果是本地存一份 远程主机172.18.2.232存一份 centos 7 先打开监控/var/log/messages这个文件
[root@centos7 ~]#tail -f /var/log/messages centos 6 测试日志 [root@centos6 ~]
#logger "This is a test log" 测试成功 [root@centos7 ~]#tail -f /var/log/messages Feb
1 15:27:29 centos6 root: This is a test log [root@centos6 ~]#tail -f
/var/log/messages Feb 1 15:27:29 centos6 root: This is a test log
TCP

只修改一处@172.18.2.232改为@@172.18.2.232即可
centos 6上操作 先修改/etc/rsyslog.conf #约42行 [root@centos6 ~]#vim /etc/rsyslog.conf
*.info;mail.none;authpriv.none;cron.none /var/log/messages
*.info;mail.none;authpriv.none;cron.none@@172.18.2.232 systemctl restart rsyslog
# 重新启动rsyslog [root@centos6 ~]#logger "This is a test log" 与UDP步骤一样
其他日志

路径 描述
/var/log/secure 系统安装日志,文本格式,应周期性分析 例:用户登录
/var/log/btmp 当前系统上,用户的失败尝试登录相关的日志信息,二进制格式,lastb命令进行查看
/var/log/wtmp 当前系统上,用户正常登录系统的相关日志信息,二进制格式,last命令可以查看
/var/log/lastlog 每一个用户最近一次的登录信息,二进制格式,lastlog命令可以查看
/var/log/dmesg 系统引导过程中的日志信息例:插入U盘等硬件信息 文本格式文本 查看工具查看或者专用命令dmesg查看
/var/log/messages 系统中大部分的信息
/var/log/anaconda anaconda的日志 在centos6 /var/log/anaconda.log在centos7
/var/log/anaconda/anaconda.log
常用命令

lastb命令查看用户登录失败信息
last命令查看用户登录成功信息 (也有系统启动多长时间)
lastlog命令查看每一个用户最近一次的登录信息
dmesg查看系统引导过程中的日志信息
查看系统启动多长时间 [root@centos7 ~]#uptime 16:10:59 up 4:34, 1 user, load average: 0.00
,0.01, 0.05 系统启动开始时间 [root@centos7 ~]#ll /var/log/boot.log -rw-------. 2 root
root27581 Feb 1 16:15 /var/log/boot.log 可以查看到启动信息(比如rsyslogd就可以看到开始启动时间) [root
@centos7 ~]#grep rsyslogd /var/log/messages Feb 1 16:14:57 centos7 rsyslogd:
[origin software="rsyslogd" swVersion="8.24.0" x-pid="600" x-info=
"http://www.rsyslog.com"] start
日志管理命令

Systemd统一管理所有Unit 的启动日志。带来的好处就是,可以只用journalctl一个命令,查看所有日志(内核日志和应用日志)。
日志的配置文件/etc/systemd/journald.conf

journalctl用法

查看所有日志(默认情况下,只保存本次启动的日志)
journalctl
查看内核日志(不显示应用日志)
journalctl -k
查看系统本次启动的日志
journalctl -b journalctl -b -0
查看上一次启动的日志(需更改设置)
journalctl -b -1
查看指定时间的日志
journalctl --since="2017-10-30 18:10:30" journalctl --since "20 min ago"
journalctl --since yesterday journalctl --since "2017-01-10" --until "2017-01-11
03:00" journalctl --since 09:00 --until "1 hour ago"
显示尾部的最新10行日志
journalctl -n
显示尾部指定行数的日志
journalctl -n 20
实时滚动显示最新日志
journalctl -f
查看指定服务的日志
journalctl /usr/lib/systemd/systemd
查看指定进程的日志
journalctl _PID=1
查看某个路径的脚本的日志
journalctl /usr/bin/bash
查看指定用户的日志
journalctl _UID=33 --since today
查看某个Unit 的日志
journalctl -u nginx.service journalctl -u nginx.service --since today
实时滚动显示某个Unit 的最新日志
journalctl -u nginx.service -f
合并显示多个Unit 的日志
journalctl -u nginx.service -u php-fpm.service --since today
查看指定优先级(及其以上级别)的日志,共有8级 0: emerg 1: alert 2: crit 3: err 4: warning 5: notice 6
: info7: debug journalctl -p err -b
日志默认分页输出,–no-pager 改为正常的标准输出
journalctl --no-pager
以JSON 格式(单行)输出
journalctl -b -u nginx.service -o json
以JSON 格式(多行)输出,可读性更好
journalctl -b -u nginx.serviceqq -o json-pretty
显示日志占据的硬盘空间
journalctl --disk-usage
指定日志文件占据的最大空间
journalctl --vacuum-size=1G
指定日志文件保存多久
journalctl --vacuum-time=1years
下面还有两篇文章

rsyslog将日志记录于MySQL中并web显示
<http://blog.csdn.net/qq_37187976/article/details/79231854>

logrotate日志转储(滚动) <http://blog.csdn.net/qq_37187976/article/details/79232786>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信