在 Redhat Linux 上配置 logrotate在 Redhat Linux 上配置 logrotate在 Redhat Linux 上配置 logrotate在 Redhat Linux 上配置 logrotate
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

在 Redhat Linux 上配置 logrotate

发表 admin at 2025年2月28日
类别
  • 未分类
标签

Logrotate 是一款专为管理生成大量日志文件的服务器的管理员而设计的实用程序。它可以帮助他们节省一些磁盘空间,并避免由于磁盘空间不足而导致系统无响应的潜在风险。

通常,避免此类问题的解决方案是为 /var 挂载点设置单独的分区或逻辑卷。但是,logrotate 也可能是解决此问题的可行解决方案,特别是在将所有日志移动到不同分区下时为时已晚的情况下。

在本教程中,我们将展示如何在 Red Hat Enterprise Linux 上配置 logrotate 服务,以便您可以重新控制日志文件。

在本教程中您将学习:

  • 如何在 RHEL 上使用 logrotate 实用程序

  • logrotate配置文件的存储位置

  • 如何设置自定义 logrotate 配置

  • 如何测试 logrotate 实现

什么是日志旋转?

Logrotate 使系统管理员能够系统地轮换和归档系统生成的任何日志文件,从而减少操作系统的磁盘空间需求。默认情况下,每天使用 cron 调度程序从位置 /etc/cron.daily/ 调用 logrotate 一次。

# ls /etc/cron.daily/
cups  logrotate  makewhatis.cron  mlocate.cron  prelink  readahead.cron  rhsmd  tmpwatch

配置日志轮转

Logrotate 的配置是通过编辑两个单独的配置文件来完成的:

  • /etc/logrotate.conf

  • 存储在 /etc/logrotate.d/ 中的服务特定配置文件

主 logrotate.conf 文件包含通用配置。这是默认的 logrotate 配置文件 logrotate.conf:

  weekly
  rotate 4
  create
  dateext
  include /etc/logrotate.d
  /var/log/wtmp {
      monthly
      create 0664 root utmp
          minsize 1M
      rotate 1
  }
  • 第 1 行 – 每周 配置选项确保每周轮换主配置文件和 /etc/logrotate.d/ 目录中定义的所有日志文件。

  • 第 2 行 – rotate 4 确保 logrotate 保留所有日志文件 4 周的备份

  • 第 3 行 – create 选项指示 logrotate 在每次轮换后创建新的空日志文件

  • 第 4 行 – dateext 在 logrotate 处理每个特定日志文件时以日期形式向所有轮换日志文件附加扩展名

  • 第 5 行 – 包含目录 /etc/logrotate.d 中的所有其他配置

  • 第 6 – 11 行包含特定的服务日志轮换配置

与 logrotate.conf 相反,目录 /etc/logrotate.d/ 包含 logrotate 使用的特定服务配置文件。在下一节中,我们将创建一个示例框架 logrotate 配置。

包括新的服务日志以进行 logrotate

在本节中,我们将在 logrotate 配置中添加新的日志文件。假设我们有一个名为 /var/log/linuxserver.log 的日志文件,位于我们的 /var/log 目录中,需要每天轮换。

首先,我们需要创建一个新的 logrotate 配置文件来容纳我们的新日志文件:

# vi /etc/logrotate.d/linuxserver

将以下文本插入到 /etc/logrotate.d/linuxserver 中:

/var/log/linuxserver.log {
        rotate 7
        daily
        compress
        delaycompress
        missingok
        notifempty
        create 660 linuxuser linuxuser }

该配置文件将每天运行,创建最多 7 个由 linuxuser 和具有 660 权限的 linuxuser 组拥有的存档,压缩所有日志并仅排除昨天和空日志文件。以下是一些选定的 logrotate 配置关键字。有关完整的教程,请查看 logrotate 手册页。

daily Log files are rotated every day.
weekly Log files are rotated if the current weekday is less than the weekday of the last rotation or if more than a week has passed since the last rotation. This is normally the same as rotating logs on the first day of the week, but if logrotate is not being run every night a log rotation will happen at the first valid opportunity.
monthly Log files are rotated the first time logrotate is run in a month (this is normally on the first day of the month).
notifempty Do not rotate the log if it is empty (this overrides the ifempty option).
nocompress Old versions of log files are not compressed.
delaycompress Postpone compression of the previous log file to the next rotation cycle. This only has effect when used in combination with compress. It can be used when some program cannot be told to close its logfile and thus might continue writing to the previous log file for some time.
compress Old versions of log files are compressed with gzip by default.
mail address When a log is rotated out of existence, it is mailed to address. If no mail should be generated by a particular log, the nomail directive may be used.
missingok If the log file is missing, go on to the next one without issuing an error message.

配置文件准备好后,只需将其复制到 logrotate 目录并更改所有者和权限:

# cp linuxserver /etc/logrotate.d/
# chmod 644 /etc/logrotate.d/linuxserver
# chown root.root /etc/logrotate.d/linuxserver

测试新的 Logrotate 配置

现在 logrotate 配置已经实现,请按照以下步骤进行测试。

  1. 创建一些示例日志文件(如果尚不存在):

    # echo "rotate my log file" > /var/log/linuxserver.log
    
  2. 日志文件就位后,使用 -f 选项强制 logrotate 轮换所有日志。

    # logrotate -f /etc/logrotate.conf
    
  3. 现在再次访问您的 /var/log/ 目录并确认您的日志文件已轮换并创建了新的日志文件。

结束语

正如前面已经提到的,避免系统被日志文件堵塞的最佳方法是为您的 /var/ 创建一个单独的分区/逻辑卷,甚至更好的 /var/log目录。然而,即使如此,logrotate 也可以通过压缩日志文件来帮助您节省一些磁盘空间。 Logrotate 还可以通过创建额外副本或通过电子邮件向您发送任何新轮换的日志文件来帮助您存档日志文件以供将来参考。

©2015-2025 Norria support@norria.com