logrotate + 如何控制/var/log下的安全日志

logrotate + 如何控制/var/log下的安全日志

我们服务器中/var/log下的安全日志超过1G,如下

du -sh * | grep sec
0       secure
4.2G    secure-20210726
1.8G    secure-20210801
1.2G    secure-20210804

因此我们决定在 maxsize 达到 100M 后轮换安全日志

所以我们将以下内容添加到/etc/logrotate.conf

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
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

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


/var/log/secure {
    monthly
        minsize 1M
        maxsize 100M
    rotate 5
}

/var/log/secure然后我们删除了该行/etc/logrotate.d/syslog,以避免重复条目

最后我们刷新日志旋转为

logrotate  /etc/logrotate.conf

一段时间后,我们检查了 /var/log 下的日志安全情况:

du -sh * | grep sec
0       secure
4.2G    secure-20210726
1.8G    secure-20210801
1.2G    secure-20210804

但正如我们在上面看到的,没有任何改变

那么我的配置有什么问题吗?

答案1

你的配置没有任何问题。logrotate将轮换条件应用于当前日志文件,它不检查轮换文件的属性(secure-20210726等)。

只有当您有足够的文件达到轮换限制时,您才会看到轮换文件中的更改,此时最旧的文件将被删除。您可以启用压缩,并手动压缩现有文件。

答案2

好吧,看看时间戳,即使在您进行更改之前,文件的最后一次成功轮换是 2021 年 8 月 4 日,大约是今天加上或减去我们所在的时区,所以……您已经期望看到什么? logrotate 通常每天通过 cron 作业或 systemd 计时器启动一次,因此在此之前不会发生任何事情。

手动调用logrotate /etc/logrotate.conf几乎不会做任何事情。由于 logrotate 不是守护进程,因此您不需要显式“重新加载配置”,而且它可能会发现,由于 logrotate 经常在夜间运行,日志文件还不够旧,无法轮换。

根据您的配置,您将在大约一个月内看到轮换,除非secure增长到超过 100M。 (我之所以这么说,是因为就我个人而言,我很少发现大小选项有用 - 我更喜欢定期轮换,即使它会导致小的旧日志文件,并且如果发生使我的日志文件爆炸的事情,logrotate 不会保存无论如何,所以我只是将规则更改为daily并计算出我想要保留多少天。)

相关内容