logrotate configuration and understanding under linux

=

=

=

 For Linux system security, log files are extremely important tools. System administrators can use the logrotate program to manage the latest events in the system. For Linux system security, log files are extremely important tools. System administrators can use the logrotate program to manage the latest events in the system. logrotate can also be used to backup log files, this article will introduce the following sections

 

Log file management:
1, logrotate configuration
2, default configuration logrotate
3, use include option to read other configuration files
4, use include option to override default configuration
5, configure dump parameters for the specified file
First, logrotate configuration

logrotate program is a log file management tool. Used to delete old log files and create new log files, we call it a "dump". We can dump according to the size of the log file or the number of days. This process is usually performed by the cron program.
The logrotate program can also be used to compress log files and send logs to a specified E-mail.

The configuration file for logrotate is /etc/logrotate.conf. The main parameters are as follows:

Parameter function
compress Compress the log after dumping through gzip
nocompress When compression is not required, use this parameter
copytruncate for the log file that is still open, back up the current log and truncate
nocopytruncate Backup the log file but not truncate
create mode owner group dump file, use the specified file mode to create a new log file
nocreate does not create a new log file
when delaycompress and compress are used together, the dumped log file will not be compressed until the next dump
nodelaycompress override the delaycompress option, turn storage and compression at the same time.
errors address The error message when storing is sent to the specified email address
ifempty Even if it is an empty file, it will be dumped. This is the default option of logrotate.
notifempty Do not dump if it is an empty file
mail address Send the dumped log file to the specified E-mail address
nomail Do not send the log file
when dumping olddir directory The dumped log file is placed in the specified directory, and must be in the same file system as the current log file
noolddir The stored log file and the current log file are placed in the same directory.
prerotate/endscript Commands that need to be executed before dumping can be placed in this pair, these two keywords must be in a separate line
postrotate/endscript The commands that need to be executed after dumping The command can be put into this pair, these two keywords must be on a separate line
daily specifies the dump cycle as daily
weekly specifies the dump cycle as weekly
monthly specifies the dump cycle as monthly
rotate count specifies the number of times the log file is dumped before deletion, 0 means no backup, 5 means keep 5 backups
tabootext [+] list Let logrotate not dump files with the specified extension, the default extensions are: .rpm-orig, .rpmsave, v, and ~ 
size size when log The file will be dumped when it reaches the specified size. Size can be specified in bytes (default) and KB (sizek) or MB (sizem).
二、缺省配置 logrotate

logrotate 缺省的配置募?/etc/logrotate.conf。
Red Hat Linux 缺省安装的文件内容是:

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# send errors to root
errors root
# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
#compress
1
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own lastlog or wtmp --we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}

/var/log/lastlog {
monthly
rotate 1
}

# system-specific logs may be configured here


缺省的配置一般放在logrotate.conf 文件的最开始处,影响整个系统。在本例中就是前面12行。

第三行weekly 指定所有的日志文件每周转储一次。
第五行 rotate 4 指定转储文件的保留 4份。
第七行 errors root 指定错误信息发送给root。
第九行create 指定 logrotate 自动建立新的日志文件,新的日志文件具有和
原来的文件一样的权限。
第11行 #compress 指定不压缩转储文件,如果需要压缩,去掉注释就可以了。

三、使用include 选项读取其他配置文件
include 选项允许系统管理员把分散到几个文件的转储信息,集中到一个
主要的配置文件。当 logrotate 从logrotate.conf 读到include 选项时,会从指定文件读入配置信息,就好像他们已经在/etc/logrotate.conf 中一样。

第13行 include /etc/logrotate.d 告诉 logrotate 读入存放在/etc/logrotate.d 目录中的日志转储参数,当系统中安装了RPM 软件包时,使用include 选项十分有用。RPM 软件包的日志转储参数一般存放在/etc/logrotate.d 目录。

include 选项十分重要,一些应用把日志转储参数存放在 /etc/logrotate.d 。

典型的应用有:apache, linuxconf, samba, cron 以及syslog。

这样,系统管理员只要管理一个 /etc/logrotate.conf 文件就可以了。


 

 


 

四、使用include 选项覆盖缺省配置

当 /etc/logrotate.conf 读入文件时,include 指定的文件中的转储参数将覆盖缺省的参数,如下例:

# linuxconf 的参数
/var/log/htmlaccess.log
{ errors jim
notifempty
nocompress
weekly
prerotate
/usr/bin/chattr -a /var/log/htmlaccess.log
endscript
postrotate
/usr/bin/chattr +a /var/log/htmlaccess.log
endscript
}
/var/log/netconf.log
{ nocompress
monthly
}

在这个例子中,当 /etc/logrotate.d/linuxconf 文件被读入时,下面的参数将覆盖/etc/logrotate.conf中缺省的参数。

Notifempty
errors jim

五、为指定的文件配置转储参数
经常需要为指定文件配置参数,一个常见的例子就是每月转储/var/log/wtmp。为特定文件而使用的参数格式是:

# 注释
/full/path/to/file
{
option(s)
}

下面的例子就是每月转储 /var/log/wtmp 一次:
#Use logrotate to rotate wtmp
/var/log/wtmp
{
monthly
rotate 1
}


 

六、其他需要注意的问题

1、尽管花括号的开头可以和其他文本放在同一行上,但是结尾的花括号必须单独成行。

2、使用 prerotate 和 postrotate 选项
下面的例子是典型的脚本 /etc/logrotate.d/syslog,这个脚本只是对
/var/log/messages 有效。

/var/log/messages

prerotate
/usr/bin/chattr -a /var/log/messages
endscript
postrotate
/usr/bin/kill -HUP syslogd
/usr/bin/chattr +a /var/log/messages
endscript
}

第一行指定脚本对 /var/log messages 有效
花括号外的/var/log messages


 

 


 

prerotate 命令指定转储以前的动作/usr/bin/chattr -a 去掉/var/log/messages文件的“只追加”属性 endscript 结束 prerotate 部分的脚本postrotate 指定转储后的动作

/usr/bin/killall -HUP syslogd 

用来重新初始化系统日志守护程序 syslogd

/usr/bin/chattr +a /var/log/messages 

重新为 /var/log/messages 文件指定“只追加”属性,这样防治程序员或用户覆盖此文件。

最后的 endscript 用于结束 postrotate 部分的脚本

3、logrotate 的运行分为三步:

判断系统的日志文件,建立转储计划以及参数,通过cron daemon 运行下面的代码是 Red Hat Linux 缺省的crontab 来每天运行logrotate。

#/etc/cron.daily/logrotate
#! /bin/sh

/usr/sbin/logrotate /etc/logrotate.conf

4、/var/log/messages 不能产生的原因:
这种情况很少见,但是如果你把/etc/services 中的 514/UDP 端口关掉的话,这个文件就不能产生了。

小结:本文通过对Red Hat 系统上典型的logrotate 配置例子的介绍,详细说明了logrotate 程序的应用方法。希望对所有Linux 系统管理员有所帮助。管理好,分析好日志文件是系统安全的第一步,在以后的文章里FreeLAMP还会介绍另外一个检查日志的好东东 logcheck。

 

=

=

=

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326179463&siteId=291194637