概述
1、日志文件记录系统事件错误 系统 日志 linux,包括用户的登录信息、系统的启动信息、系统的安全信息、邮件相关信息、各种服务相关信息等
2、通过日志来检查错误发生的原因,或者受到攻击时攻击者留下的痕迹。
1. rsyslog系统日志管理 1.1 处理日志的程序:
rsyslog:系统专职日志程序,是一个程序,是一个进程,有配置文件
#ps aux |grep rsyslogd
(注:d deamon 程序,守护程序,一般不会退出)
用户 /进程号 /cpu /内存 /虚拟内存*2 /没有终端 /线程在sleep /2021年开的 /占cpu104.43秒 /命令
安装: yum install -y rsyslog
启动:systemctl start rsyslog.service
1.2 rsyslogd的主配置文件 — /etc/rsyslog.conf
# rsyslog 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 ####
# 必要的加载模块
# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark # provides --MARK-- message capability
发送到远程主机,需开启网络服务
# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514
# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514
#### GLOBAL DIRECTIVES ####
# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog
# 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/
$IncludeConfig /etc/rsyslog.d/*.conf
子配置文件
# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on
# File to store the position in the journal
$IMJournalStateFile imjournal.state
#### RULES ####
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages
## *.info 所有设备的基本信息
## ; 和
## mail.none none表示排除
# The authpriv file has restricted access.
authpriv.* /var/log/secure
# Log all the mail messages in one place.
mail.* -/var/log/maillog
# “-”符号,表示使用异步的方式记录,先放到缓存区,稍晚写到文件中
# Log cron stuff
cron.* /var/log/cron
# Everybody gets emergency messages
*.emerg :omusrmsg:*
#匹配任何类型,且消息等级是emerg(致命性,最高等级)的消息,会被发送到系统所有用户的登录终端
# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler
1) /etc/rsyslog.conf 配置文件格式
authpriv.* /var/log/secure
·#服务名称+[连接符号]+日志等级 日志记录位置
·#认证相关服务.所有日志等级 记录在/var/log/secure中
RULES:即规则,是一套生成日志,以及存储日志的策略。
规则由四部分组成::facility + 连接符 + level + path
设备名说明
authpriv
ssh、ftp 等登录信息的验证信息
cron
时间任务相关
kern
内核
lpr
打印
邮件
mark(syslog)-rsyslog
服务内部的信息,时间标识
news
新闻组
user
用户程序产生的相关信息
uucp
unix to nuix copy主机之间相关的通信
local 1-7
自定义的日志设备
不同设备类型对应多款app
level级别:记录信息的类型
等级介绍
debug
有调试信息的linux解压命令,日志通信最多
info
通知信息,属于一般信息
notice
普通信息,但是有一定的重要性
warnig
警告信息,还不会影响到服务或系统的运行
err
错误级别错误 系统 日志 linux嵌入式linux论坛,阻止某个功能或者模块不能正常工作的信息
crit
临界状况信息,阻止整个系统或者整个软件不能正常工作的信息
alert
警告状态信息,需要立即处理和解决,eg磁盘空间不足
emerg
紧急,致命,服务无法继续运行,eg:配置文件丢失
*
代表所有日志等级
none
忽略这个日志服务
用户名
eg:root,就会把日志发送给root用户(必须在线);”*“ 代表发送给所有在线用户。
如需把日志发送给多个在线用户,用户名之前用","隔开。
“~”,代表忽略或丢弃日志。
测试:更改日志保存路径
修改:日志存储路径
vim /etc/rsyslog.conf
# The authpriv file has restricted access.
#authpriv.* /var/log/secure
authpriv.* /var/log/anquan
重新加载rsyslog
[root@vm-150 ~]# systemctl restart rsyslog
[root@vm-150 ~]# cat /var/log/anquan
Dec 16 14:01:55 vm-150 polkitd[710]: Unregistered Authentication Agent for unix-process:125930:137224934 (system bus name :1.6006, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) (disconnected from bus)
换账号测试
[root@vm-150 ~]# su - Bess
上一次登录:三 12月 16 13:58:32 CST 2022pts/1 上
-bash-4.2$ su -
密码:
上一次登录:三 12月 16 13:59:20 CST 2022pts/1 上
日志已更新
[root@vm-150 ~]# cat /var/log/anquan
Dec 16 14:01:55 vm-150 polkitd[710]: Unregistered Authentication Agent for unix-process:125930:137224934 (system bus name :1.6006, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) (disconnected from bus)
Dec 16 14:02:12 vm-150 su: pam_unix(su-l:session): session opened for user Bess by root(uid=0)
Dec 16 14:02:21 vm-150 su: pam_unix(su-l:session): session opened for user root by root(uid=10001)
1.3 常见日志文件(系统、进程、应用程序):
除了系统默认的日志外,采用rpm方式安装的系统服务也会把日志记录在/var/log/目录下,各个服务使用字的日志管理文档来记录自身日志。 源码包安装的服务日志是在源码包指定目录中。
系统常用日志:
日志文件说明
/var/log/message
记录系统重要消息的日志,该日志文件记录 Linux 系统的绝大多数重要信息,如果系统出现问题,首先检查该日志文件 tailf 动态查看日志文件尾部
/var/log/secure
记录验证和授权方面的信息,只要涉及账户和密码的程序都会记录,比如系统登录、ssh 登录、su 切换用户、sudo 授权,添加用户、修改用户密码都会记录在这个日志文件中
/var/log/dmesg
记录系统在开机时内核自检的信总,也可以使用 dmesg 命令直接查看内核自检信息
/var/log/mailog
记录邮件信息的日志
/var/log/boot.log
系统启动日志
/var/log/cron
记录与系统定时任务相关的日志
/var/log/btmp
记录错误登录的日志,该文件为二进制文件,不能直接使用 Vi 查看,而要使用 lastb 命令查看。b-bad
/var/log/wtmp
永久记录所有用户的登录、注销信息,同时记录系统的后动、重启、关机事件,为二进制文件,要使用 last 命令查看。w-who
/var/tun/utmp
记录当前已经登录的用户的信息,该文件会随着用户的登录和注销而不断变化,只记录当前登录用户的信息,该文件不能用vi查看,而要使用 w、who、users 等命令查看。u-user
/var/log/lastlog
记录系统中所有用户最后一次的登录时间的日志,该文件为二进制文件,要使用 lastlog 命令查看
日志文件说明