Linux 系统日志及其归档

主要记录Linux 系统需要关注的日志文件,以及日志归档服务 rsyslogd

系统日志服务

rsyslogd 日志服务

rsyslogd reliable and extended syslogd 可靠 可扩展的系统日志服务

Rsyslogd是一个系统实用程序,提供对消息日志记录的支持。同时支持internet和unix域套接字使该实用程序能够支持本地和远程日志记录。

配置文件

more /etc/rsyslog.conf|grep -v '#'|grep -v ^$
$umask 0022
$WorkDirectory /var/lib/rsyslog
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$IncludeConfig /etc/rsyslog.d/*.conf
$OmitLocalLogging on
$IMJournalStateFile imjournal.state
*.info;mail.none;authpriv.none;cron.none;local0.none          /var/log/messages
authpriv.*          /var/log/secure
mail.*                                                  -/var/log/maillog
cron.*                                                  /var/log/cron
*.emerg                                                 :omusrmsg:*
uucp,news.crit                                          /var/log/spooler
local7.*                                                /var/log/boot.log
local0.*          /var/log/keepalived.log

从上述的配置文件里面可以看到我们需要关注的系统日志文件,一般都不动这个配置

感兴趣可以上官网看看:RSyslog Documentation - rsyslog

logrotate 日志归档

logrotate rotates, compresses, and mails system logs 归档 压缩 邮件发送系统日志

配置文件

/etc/logrotate.conf
/etc/logrotate.d/

来,上配置文件,如下是默认的配置:

more /etc/logrotate.conf 
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly                 # 每次归档以月为周期
    create 0664 root utmp   # 不存在时依据权限 0664 root:utmp 创建
        minsize 1M
    rotate 1                # 转储保留次数,为0即归档时删除旧的归档
}

/var/log/btmp {
    missingok               # 默认off ,表示如果文件不存在,停止所有文件的归档
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

 查看归档情况

more /var/lib/logrotate/logrotate.status

注意

/etc/logrotate.d/syslog 这里写着系统日志的归档方式

more /etc/logrotate.d/syslog
   sharedscripts
   postrotate
        /usr/bin/systemctl kill -s HUP rsyslog.service >/dev/null 2>&1 || true
   endscript

logrotate 在每次对应的日志文件轮转后运行postrotate 与 endscript 之间的命令或脚本

message 里面能看到归档后执行结果
rsyslogd: [origin software="rsyslogd" swVersion="8.24.0-38.el7" x-pid="1487" x-info="http://www.rsyslog.com"] rsyslogd was HUPed


系统日志简介

如下所有的文件默认指/var/log 目录下,如果不是将写全路径,主要是依据Centos 7版本

messages

从rsyslogd 的配置中可以看出来,message 文件包含了很多的系统日志,错误、警告、通知

一般,排查系统运行过程中出现的问题,会先从message入手

boot.log

系统的引导日志,系统开机看到刷刷刷的打印,内容就会记录在这里,记录了系统引导过程中的所有信息,包括硬件检测、内核加载、启动服务等

一般发现系统启动异常,比如启动慢,启动报错等问题,会从这里入手排查,看失败的任务有哪些,这都将会影响到系统启动

secure

安全日志文件,记录了系统中所有安全相关的信息,包括登录失败、系统入侵尝试、安全事件等

一般,我们需要检查secure日志里面登录失败记录,因为如果出现大量的不同用户登录失败的记录,就表示有人在尝试爆破服务器了,这时候需要特别关注,考虑加入黑名单或者更新ssh端口了

一些简单手段:

1、 /etc/hosts.deny  IP或者用户写进去,将无法通过ssh登录,慎用

man HOSTS_ACCESS
或者
man hosts.deny

2、最好是屏蔽root用户登录,用普通用户登录再提权,修改ssh端口

3、添加sudo 失败尝试锁定

more /etc/pam.d/system-auth-ac
修改如下内容,5次输错后,锁定300秒
auth        required      pam_tally2.so deny=5 unlock_time=300

man pam_tally2 
This module maintains a count of attempted accesses, 
can reset count on success, can deny access if too many attempts fail.

Add the following line to /etc/pam.d/login to lock the account after 4 failed logins. 
Root account will be locked as well. The accounts will be
       automatically unlocked after 20 minutes. The module does not have to be called
       in the account phase because the login calls pam_setcred(3)
       correctly.

现象以及解锁

锁定现象:
sudo su -
[sudo] password for username: 
Sorry, try again.
[sudo] password for username: 

Account locked due to 19 failed logins
Password: 
su: Authentication failure

手工解锁:
pam_tally2 -u username --reset

4、加强口令策略以及密码尝试限制

more /etc/pam.d/system-auth
# 限制重试次数,2次
password    requisite     pam_pwquality.so try_first_pass local_users_only retry=2 authtok_type=

# 限制密码口令长度10位以上 复杂度2种以上 口令历史 20次
password    sufficient    pam_unix.so remember=20 minlen=10 minclass=2 sha512 shadow nullok try_first_pass use_authtok

# 加载拒绝登录
password    required      pam_deny.so

5、限制用户 IP段 用户组登录,注意别把自己拒绝了,会无法访问的

more /etc/security/access.conf
+ : ALL : 192.168.0.0/16         # 允许这个段登录
- : ALL EXCEPT root myuser mygroup : ALL   # 拒绝所有 除了root/myuser/mygroup 这些用户或者用户组

dmesg

内核日志文件,记录了系统内核的所有输出信息,包括硬件检测、设备驱动加载、内核错误等。

感兴趣可以看看,这个比 boot.log 详细

maillog

邮件日志文件,记录了系统中所有邮件相关的信息,包括发送、接收、退回等

这个一般不看,平时也不用系统自带mail ,有需要的可以了解一下

kdump.log

有些系统启用了kdump 用来保存系统异常崩溃时的信息

lastlog

记录所有用户的登录情况,是一个data 文件,不能直接看,需要通过lastlog 命令查看

补充说明

utmp 将为我们提供用户登录终端、注销、系统事件和当前系统状态、系统启动时间(由正常运行时间使用)等的完整信息

wtmp 给出了 utmp 的历史数据

btmp 只记录失败的登录尝试

记录正确登入系统者的帐户信息 (wtmp) 与错误登入时所使用的帐户信息 (btmp)
 

后续有发现新的需要记录的日志文件,再追加上来

  • 19
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值